Introduce BPF STRUCT_OPS
Subsystems (11)
v2 cover Introduce BPF STRUCT_OPS
This series introduces BPF STRUCT_OPS. It is an infra to allow implementing some specific kernel's function pointers in BPF. The first use case included in this series is to implement TCP congestion control algorithm in BPF (i.e. implement struct tcp_congestion_ops in BPF).
There has been attempt to move the TCP CC to the user space
(e.g. CCP in TCP). The common arguments are faster turn around,
get away from long-tail kernel versions in production...etc, which are legit points.
BPF has been the continuous effort to join both kernel and userspace upsides together (e.g. XDP to gain the performance advantage without bypassing the kernel). The recent BPF advancements (in particular BTF-aware verifier, BPF trampoline, BPF CO-RE...) made implementing kernel struct ops (e.g. tcp cc) possible in BPF.
The idea is to allow implementing tcp_congestion_ops in bpf. It allows a faster turnaround for testing algorithm in the production while leveraging the existing (and continue growing) BPF feature/framework instead of building one specifically for userspace TCP CC.
Please see individual patch for details.
The bpftool support will be posted in follow-up patches.
v2:
- Dropped cubic for now. They will be reposted
once there are more clarity in "jiffies" on both
bpf side (about the helper) and
tcp_cubic side (some of jiffies usages are being replaced by tp->tcp_mstamp)
- Remove unnecssary check on bitfield support from btf_struct_access() (Yonghong)
- BTF_TYPE_EMIT macro (Yonghong, Andrii)
- value_name's length check to avoid an unlikely
type match during truncation case (Yonghong)
- BUILD_BUG_ON to ensure no trampoline-image overrun
in the future (Yonghong)
- Simplify get_next_key() (Yonghong)
- Added comment to explain how to check mandatory
func ptr in net/ipv4/bpf_tcp_ca.c (Yonghong)
- Rename "__bpf_" to "bpf_struct_ops_" for value prefix (Andrii)
- Add comment to highlight the bpf_dctcp.c is not necessarily the same as tcp_dctcp.c. (Alexei, Eric)
- libbpf: Renmae "struct_ops" to ".struct_ops" for elf sec (Andrii)
- libbpf: Expose struct_ops as a bpf_map (Andrii)
- libbpf: Support multiple struct_ops in SEC(".struct_ops") (Andrii)
- libbpf: Add bpf_map__attach_struct_ops() (Andrii)
Martin KaFai Lau (11):
bpf: Save PTR_TO_BTF_ID register state when spilling to stack
bpf: Avoid storing modifier to info->btf_id
bpf: Add enum support to btf_ctx_access()
bpf: Support bitfield read access in btf_struct_access
bpf: Introduce BPF_PROG_TYPE_STRUCT_OPS
bpf: Introduce BPF_MAP_TYPE_STRUCT_OPS
bpf: tcp: Support tcp_congestion_ops in bpf
bpf: Add BPF_FUNC_tcp_send_ack helper
bpf: Synch uapi bpf.h to tools/
bpf: libbpf: Add STRUCT_OPS support
bpf: Add bpf_dctcp example
arch/x86/net/bpf_jit_comp.c | 11 +- include/linux/bpf.h | 79 ++- include/linux/bpf_types.h | 7 + include/linux/btf.h | 47 ++ include/linux/filter.h | 2 + include/net/tcp.h | 1 + include/uapi/linux/bpf.h | 19 +- kernel/bpf/Makefile | 2 +- kernel/bpf/bpf_struct_ops.c | 586 ++++++++++++++++ kernel/bpf/bpf_struct_ops_types.h | 9 + kernel/bpf/btf.c | 129 ++-- kernel/bpf/map_in_map.c | 3 +- kernel/bpf/syscall.c | 66 +- kernel/bpf/trampoline.c | 5 +- kernel/bpf/verifier.c | 140 +++- net/core/filter.c | 2 +- net/ipv4/Makefile | 4 + net/ipv4/bpf_tcp_ca.c | 248 +++++++ net/ipv4/tcp_cong.c | 14 +- net/ipv4/tcp_ipv4.c | 6 +- net/ipv4/tcp_minisocks.c | 4 +- net/ipv4/tcp_output.c | 4 +- tools/include/uapi/linux/bpf.h | 19 +- tools/lib/bpf/bpf.c | 10 +- tools/lib/bpf/bpf.h | 5 +- tools/lib/bpf/libbpf.c | 639 +++++++++++++++++- tools/lib/bpf/libbpf.h | 1 + tools/lib/bpf/libbpf.map | 1 + tools/lib/bpf/libbpf_probes.c | 2 + tools/testing/selftests/bpf/bpf_tcp_helpers.h | 228 +++++++ .../selftests/bpf/prog_tests/bpf_tcp_ca.c | 218 ++++++ tools/testing/selftests/bpf/progs/bpf_dctcp.c | 210 ++++++ 32 files changed, 2582 insertions(+), 139 deletions(-) create mode 100644 kernel/bpf/bpf_struct_ops.c create mode 100644 kernel/bpf/bpf_struct_ops_types.h create mode 100644 net/ipv4/bpf_tcp_ca.c create mode 100644 tools/testing/selftests/bpf/bpf_tcp_helpers.h create mode 100644 tools/testing/selftests/bpf/prog_tests/bpf_tcp_ca.c create mode 100644 tools/testing/selftests/bpf/progs/bpf_dctcp.c
Signature · 1 line
2.17.1
PATCH v2 1/11 bpf: Save PTR_TO_BTF_ID register state when spilling to stack
This patch makes the verifier save the PTR_TO_BTF_ID register state when spilling to the stack.
--- kernel/bpf/verifier.c | 1 + 1 file changed, 1 insertion(+)
kernel/bpf/verifier.c +1 -0
@@ -1915,6 +1915,7 @@ static bool is_spillable_regtype(enum bpf_reg_type type)case PTR_TO_TCP_SOCK :case PTR_TO_TCP_SOCK_OR_NULL :case PTR_TO_XDP_SOCK : +case PTR_TO_BTF_ID :return true;default :return false;
Signature · 1 line
2.17.1
PATCH v2 2/11 bpf: Avoid storing modifier to info->btf_id
In reply to Martin KaFai Lauinfo->btf_id expects the btf_id of a struct, so it should store the final result after skipping modifiers (if any).
It also takes this chanace to add a missing newline in one of the bpf_log() messages.
--- kernel/bpf/btf.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-)
kernel/bpf/btf.c +6 -3
@@ -3696,7 +3696,6 @@ bool btf_ctx_access(int off, int size, enum bpf_access_type type,/* this is a pointer to another type */ info->reg_type =PTR_TO_BTF_ID ; - info->btf_id = t->type;if (tgt_prog) { ret =btf_translate_to_vmlinux (log, btf, t, tgt_prog->type);
@@ -3707,10 +3706,14 @@ bool btf_ctx_access(int off, int size, enum bpf_access_type type,return false; } } + + info->btf_id = t->type; t =btf_type_by_id (btf, t->type);/* skip modifiers */ -while (btf_type_is_modifier (t)) +while (btf_type_is_modifier (t)) { + info->btf_id = t->type; t =btf_type_by_id (btf, t->type); + }if (!btf_type_is_struct (t)) { bpf_log(log, "func '%s' arg%d type %s is not a struct\n",
@@ -3736,7 +3739,7 @@ int btf_struct_access(struct bpf_verifier_log *log, again: tname =__btf_name_by_offset (btf_vmlinux, t->name_off);if (!btf_type_is_struct (t)) { -bpf_log (log,"Type '%s' is not a struct" , tname); +bpf_log (log,"Type '%s' is not a struct\n" , tname);return -EINVAL ; }
Signature · 1 line
2.17.1
PATCH v2 3/11 bpf: Add enum support to btf_ctx_access()
In reply to Martin KaFai LauIt allows bpf prog (e.g. tracing) to attach
to a kernel function that takes enum argument.
--- kernel/bpf/btf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/bpf/btf.c +1 -1
@@ -3676,7 +3676,7 @@ bool btf_ctx_access(int off, int size, enum bpf_access_type type,/* skip modifiers */ while (btf_type_is_modifier (t)) t =btf_type_by_id (btf, t->type); -if (btf_type_is_int (t)) +if (btf_type_is_int (t) ||btf_type_is_enum (t))/* accessing a scalar */ return true;if (!btf_type_is_ptr (t)) {
Signature · 1 line
2.17.1
PATCH v2 4/11 bpf: Support bitfield read access in btf_struct_access
In reply to Martin KaFai LauThis patch allows bitfield access as a scalar.
--- kernel/bpf/btf.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)
kernel/bpf/btf.c +6 -4
@@ -3744,10 +3744,6 @@ int btf_struct_access(struct bpf_verifier_log *log, }for_each_member (i, t, member) { -if (btf_member_bitfield_size (t, member)) -/* bitfields are not supported yet */ -continue ; -/* offset of the field in bytes */ moff =btf_member_bit_offset (t, member) /8 ;if (off + size <= moff)
@@ -3757,6 +3753,12 @@ int btf_struct_access(struct bpf_verifier_log *log,if (off < moff)continue ; +if (btf_member_bitfield_size (t, member)) { +if (off == moff && off + size <= t->size) +return SCALAR_VALUE ; +continue ; + } +/* type of the field */ mtype =btf_type_by_id (btf_vmlinux, member->type); mname =__btf_name_by_offset (btf_vmlinux, member->name_off);
Signature · 1 line
2.17.1
Re: [PATCH bpf-next v2 04/11] bpf: Support bitfield read access in btf_struct_access
Quoted 3 lines · Martin KaFai Lau
This patch allows bitfield access as a scalar.
Signed-off-by Martin KaFai Lau <[email protected]>
Re: [PATCH bpf-next v2 04/11] bpf: Support bitfield read access in btf_struct_access
In reply to Martin KaFai LauQuoted 24 lines · Martin KaFai Lau
This patch allows bitfield access as a scalar.
Signed-off-by Martin KaFai Lau <[email protected]>--- kernel/bpf/btf.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)kernel/bpf/btf.c +0
-4@@ -3744,10 +3744,6 @@ int btf_struct_access(struct bpf_verifier_log *log, }for_each_member (i, t, member) { -if (btf_member_bitfield_size (t, member)) -/* bitfields are not supported yet */ -continue ; -/* offset of the field in bytes */ moff =btf_member_bit_offset (t, member) /8 ;if (off + size <= moff)
kernel/bpf/btf.c int btf_struct_access(struct bpf_verifier_log *log, · line 3760
Quoted 8 lines
kernel/bpf/btf.c +5
-0@@ -3757,6 +3753,12 @@ int btf_struct_access(struct bpf_verifier_log *log,if (off < moff)continue ; +if (btf_member_bitfield_size (t, member)) { +if (off == moff && off + size <= t->size) +return SCALAR_VALUE ; +continue ; + }
Shouldn't this check be done before (off < moff) above?
Imagine this situation:
struct {
int :16;
int x:8;
};
Compiler will generate 4-byte load with offset 0, and then bit shifts to extract third byte. From kernel perspective, you'll see that off=0, but moff=2, which will get skipped.
So there are two problems, I think:
1. if member is bitfield, special handle that before (off < moff) case.
2. off == moff is too precise, I think it should be off <= moff, but also check that it covers entire bitfield, e.g.:
(off + size) * 8 >= btf_member_bit_offset(t, member) +btf_member_bitfield_size(t, member)
Make sense or am I missing anything?
Quoted 7 lines
kernel/bpf/btf.c +1
-1+/* type of the field */ mtype =btf_type_by_id (btf_vmlinux, member->type); mname =__btf_name_by_offset (btf_vmlinux, member->name_off); --2.17.1
Re: [PATCH bpf-next v2 04/11] bpf: Support bitfield read access in btf_struct_access
Quoted 36 lines · Andrii Nakryiko
Quoted 24 lines · Martin KaFai Lau
This patch allows bitfield access as a scalar.
Signed-off-by Martin KaFai Lau <[email protected]>--- kernel/bpf/btf.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)kernel/bpf/btf.c +0
-4@@ -3744,10 +3744,6 @@ int btf_struct_access(struct bpf_verifier_log *log, }for_each_member (i, t, member) { -if (btf_member_bitfield_size (t, member)) -/* bitfields are not supported yet */ -continue ; -/* offset of the field in bytes */ moff =btf_member_bit_offset (t, member) /8 ;if (off + size <= moff)kernel/bpf/btf.c int btf_struct_access(struct bpf_verifier_log *log, · line 3760
Quoted 8 lines
kernel/bpf/btf.c +5
-0@@ -3757,6 +3753,12 @@ int btf_struct_access(struct bpf_verifier_log *log,if (off < moff)continue ; +if (btf_member_bitfield_size (t, member)) { +if (off == moff && off + size <= t->size) +return SCALAR_VALUE ; +continue ; + }Shouldn't this check be done before (off < moff) above?
Quoted 6 lines
Imagine this situation:
struct { int :16; int x:8; };
Oh, yes, forgot the case where the first bitfield member may have no name, in which case, off will not match any moff.
btf_struct_access is used to check vmlinux btf types. I think in vmlinux we may not have such scenarios. So the above code should handle vmlinux use cases properly.
But I agree with Andrii that we probably want to handle anonymous bitfield member (which is ignored in debuginfo and BTF) properly.
Quoted 22 lines
Compiler will generate 4-byte load with offset 0, and then bit shifts to extract third byte. From kernel perspective, you'll see that off=0, but moff=2, which will get skipped.
So there are two problems, I think:
1. if member is bitfield, special handle that before (off < moff) case.
2. off == moff is too precise, I think it should beoff <= moff, but also check that it covers entire bitfield, e.g.:(off + size) * 8 >= btf_member_bit_offset(t, member) +
btf_member_bitfield_size(t, member)Make sense or am I missing anything?
Quoted 7 lines
kernel/bpf/btf.c +1
-1+/* type of the field */ mtype =btf_type_by_id (btf_vmlinux, member->type); mname =__btf_name_by_offset (btf_vmlinux, member->name_off); --2.17.1
PATCH v2 5/11 bpf: Introduce BPF_PROG_TYPE_STRUCT_OPS
In reply to Martin KaFai LauThis patch allows the kernel's struct ops (i.e. func ptr) to be implemented in BPF. The first use case in this series is the "struct tcp_congestion_ops" which will be introduced in a latter patch.
This patch introduces a new prog type BPF_PROG_TYPE_STRUCT_OPS. The BPF_PROG_TYPE_STRUCT_OPS prog is verified against a particular func ptr of a kernel struct. The attr->attach_btf_id is the btf id of a kernel struct. The attr->expected_attach_type is the member "index" of that kernel struct. The first member of a struct starts with member index 0. That will avoid ambiguity when a kernel struct has multiple func ptrs with the same func signature.
For example, a BPF_PROG_TYPE_STRUCT_OPS prog is written
to implement the "init" func ptr of the "struct tcp_congestion_ops". The attr->attach_btf_id is the btf id of the "struct tcp_congestion_ops" of the _running_ kernel. The attr->expected_attach_type is 3.
The ctx of BPF_PROG_TYPE_STRUCT_OPS is an array of u64 args saved by arch_prepare_bpf_trampoline that will be done in the next patch when introducing BPF_MAP_TYPE_STRUCT_OPS.
"struct bpf_struct_ops" is introduced as a common interface for the kernel struct that supports BPF_PROG_TYPE_STRUCT_OPS prog. The supporting kernel struct will need to implement an instance of the "struct bpf_struct_ops".
The supporting kernel struct also needs to implement a bpf_verifier_ops. During BPF_PROG_LOAD, bpf_struct_ops_find() will find the right bpf_verifier_ops by searching the attr->attach_btf_id.
A new "btf_struct_access" is also added to the bpf_verifier_ops such that the supporting kernel struct can optionally provide its own specific check on accessing the func arg (e.g. provide limited write access).
After btf_vmlinux is parsed, the new bpf_struct_ops_init() is called to initialize some values (e.g. the btf id of the supporting kernel struct) and it can only be done once the btf_vmlinux is available.
The R0 checks at BPF_EXIT is excluded for the BPF_PROG_TYPE_STRUCT_OPS prog if the return type of the prog->aux->attach_func_proto is "void".
--- include/linux/bpf.h | 30 +++++++ include/linux/bpf_types.h | 4 + include/linux/btf.h | 34 ++++++++ include/uapi/linux/bpf.h | 1 + kernel/bpf/Makefile | 2 +- kernel/bpf/bpf_struct_ops.c | 122 +++++++++++++++++++++++++++ kernel/bpf/bpf_struct_ops_types.h | 4 + kernel/bpf/btf.c | 88 ++++++++++++++------ kernel/bpf/syscall.c | 17 ++-- kernel/bpf/verifier.c | 134 +++++++++++++++++++++++------- 10 files changed, 372 insertions(+), 64 deletions(-) create mode 100644 kernel/bpf/bpf_struct_ops.c create mode 100644 kernel/bpf/bpf_struct_ops_types.h
include/linux/bpf.h +30 -0
@@ -349,6 +349,10 @@ struct bpf_verifier_ops {const struct bpf_insn *src, struct bpf_insn *dst, struct bpf_prog *prog, u32 *target_size); +int (*btf_struct_access)(struct bpf_verifier_log *log, +const struct btf_type *t,int off,int size, +enum bpf_access_type atype, +u32 *next_btf_id); };struct bpf_prog_offload_ops {
@@ -667,6 +671,32 @@ struct bpf_array_aux {struct work_struct work; }; +struct btf_type ; +struct btf_member ; + +#define BPF_STRUCT_OPS_MAX_NR_MEMBERS 64 +struct bpf_struct_ops { +const struct bpf_verifier_ops *verifier_ops; +int (*init)(struct btf *_btf_vmlinux); +int (*check_member)(const struct btf_type *t, +const struct btf_member *member); +const struct btf_type *type; +const char *name; +struct btf_func_model func_models[BPF_STRUCT_OPS_MAX_NR_MEMBERS ]; +u32 type_id; +}; + +#if defined(CONFIG_BPF_JIT ) +const struct bpf_struct_ops *bpf_struct_ops_find (u32 type_id); +void bpf_struct_ops_init (struct btf *_btf_vmlinux); +#else +static inline const struct bpf_struct_ops *bpf_struct_ops_find (u32 type_id) +{ +return NULL ; +} +static inline void bpf_struct_ops_init (struct btf *_btf_vmlinux) { } +#endif +struct bpf_array {struct bpf_map map;u32 elem_size;
include/linux/bpf_types.h +4 -0
@@ -65,6 +65,10 @@ BPF_PROG_TYPE(BPF_PROG_TYPE_LIRC_MODE2, lirc_mode2,BPF_PROG_TYPE (BPF_PROG_TYPE_SK_REUSEPORT , sk_reuseport, struct sk_reuseport_md, struct sk_reuseport_kern)#endif +#if defined(CONFIG_BPF_JIT ) +BPF_PROG_TYPE (BPF_PROG_TYPE_STRUCT_OPS , bpf_struct_ops, + void *, void *) +#endif BPF_MAP_TYPE(BPF_MAP_TYPE_ARRAY, array_map_ops) BPF_MAP_TYPE(BPF_MAP_TYPE_PERCPU_ARRAY, percpu_array_map_ops)
include/linux/btf.h +34 -0
@@ -53,6 +53,18 @@ bool btf_member_is_reg_int(const struct btf *btf, const struct btf_type *s, u32 expected_offset, u32 expected_size);int btf_find_spin_lock (const struct btf *btf,const struct btf_type *t);bool btf_type_is_void (const struct btf_type *t); +s32 btf_find_by_name_kind (const struct btf *btf,const char *name,u8 kind); +const struct btf_type *btf_type_skip_modifiers (const struct btf *btf, +u32 id,u32 *res_id); +const struct btf_type *btf_type_resolve_ptr (const struct btf *btf, +u32 id,u32 *res_id); +const struct btf_type *btf_type_resolve_func_ptr (const struct btf *btf, +u32 id,u32 *res_id); + +#define for_each_member (i, struct_type, member) \ + for (i = 0, member = btf_type_member(struct_type); \ + i < btf_type_vlen(struct_type); \ + i++, member++)static inline bool btf_type_is_ptr (const struct btf_type *t) {
@@ -84,6 +96,28 @@ static inline bool btf_type_is_func_proto(const struct btf_type *t)return BTF_INFO_KIND (t->info) ==BTF_KIND_FUNC_PROTO ; } +static inline u16 btf_type_vlen (const struct btf_type *t) +{ +return BTF_INFO_VLEN (t->info); +} + +static inline bool btf_type_kflag (const struct btf_type *t) +{ +return BTF_INFO_KFLAG (t->info); +} + +static inline u32 btf_member_bitfield_size (const struct btf_type *struct_type, +const struct btf_member *member) +{ +return btf_type_kflag (struct_type) ?BTF_MEMBER_BITFIELD_SIZE (member->offset) + :0 ; +} + +static inline const struct btf_member *btf_type_member (const struct btf_type *t) +{ +return (const struct btf_member *)(t +1 ); +} +#ifdef CONFIG_BPF_SYSCALL const struct btf_type *btf_type_by_id (const struct btf *btf,u32 type_id);const char *btf_name_by_offset (const struct btf *btf,u32 offset);
include/uapi/linux/bpf.h +1 -0
@@ -174,6 +174,7 @@ enum bpf_prog_type {BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE ,BPF_PROG_TYPE_CGROUP_SOCKOPT ,BPF_PROG_TYPE_TRACING , + BPF_PROG_TYPE_STRUCT_OPS, }; enum bpf_attach_type {
kernel/bpf/Makefile +1 -1
@@ -6,7 +6,7 @@ obj-$(CONFIG_BPF_SYSCALL) += syscall.o verifier.o inode.o helpers.o tnum.o obj-$(CONFIG_BPF_SYSCALL) += hashtab.o arraymap.o percpu_freelist.o bpf_lru_list.o lpm_trie.o map_in_map.o obj-$(CONFIG_BPF_SYSCALL) += local_storage.o queue_stack_maps.o obj-$(CONFIG_BPF_SYSCALL) += disasm.o -obj-$(CONFIG_BPF_JIT) += trampoline.o +obj-$(CONFIG_BPF_JIT) += trampoline.o bpf_struct_ops.o obj-$(CONFIG_BPF_SYSCALL) += btf.o obj-$(CONFIG_BPF_JIT) += dispatcher.o ifeq ($(CONFIG_NET),y)
kernel/bpf/bpf_struct_ops.c new file +122 -0
122 lines not on this page · raw message
kernel/bpf/bpf_struct_ops_types.h new file +4 -0
@@ -0,0 +1,4 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* internal file - do not include directly */ + +/* To be filled in a later patch */
kernel/bpf/btf.c +61 -27
124 lines not on this page · raw message
kernel/bpf/syscall.c +11 -6
@@ -1672,17 +1672,22 @@ bpf_prog_load_check_attach(enum bpf_prog_type prog_type,enum bpf_attach_type expected_attach_type, u32 btf_id, u32 prog_fd) { - switch (prog_type) { - case BPF_PROG_TYPE_TRACING: + if (btf_id) { if (btf_id > BTF_MAX_TYPE) return -EINVAL; -break ; -default : -if (btf_id || prog_fd) + +switch (prog_type) { +case BPF_PROG_TYPE_TRACING : +case BPF_PROG_TYPE_STRUCT_OPS : +break ; +default :return -EINVAL ; -break ; + } } +if (prog_fd && prog_type !=BPF_PROG_TYPE_TRACING ) +return -EINVAL ; +switch (prog_type) {case BPF_PROG_TYPE_CGROUP_SOCK :switch (expected_attach_type) {
kernel/bpf/verifier.c +104 -30
185 lines not on this page · raw message
Signature · 1 line
2.17.1
Re: [PATCH bpf-next v2 05/11] bpf: Introduce BPF_PROG_TYPE_STRUCT_OPS
Quoted 34 lines · Martin KaFai Lau
This patch allows the kernel's struct ops (i.e. func ptr) to be implemented in BPF. The first use case in this series is the "struct
tcp_congestion_ops" which will be introduced in a latter patch.This patch introduces a new prog type
BPF_PROG_TYPE_STRUCT_OPS. TheBPF_PROG_TYPE_STRUCT_OPSprog is verified against a particular func ptr of a kernel struct. Theattr->attach_btf_idis the btf id of a kernel struct. Theattr->expected_attach_typeis the member "index" of that kernel struct. The first member of a struct starts with member index 0. That will avoid ambiguity when a kernel struct has multiple func ptrs with the same func signature.For example, a
BPF_PROG_TYPE_STRUCT_OPSprog is written
to implement the "init" func ptr of the "structtcp_congestion_ops". Theattr->attach_btf_idis the btf id of the "structtcp_congestion_ops" of the _running_ kernel. Theattr->expected_attach_typeis 3.The ctx of
BPF_PROG_TYPE_STRUCT_OPSis an array of u64 args saved byarch_prepare_bpf_trampolinethat will be done in the next patch when introducingBPF_MAP_TYPE_STRUCT_OPS."struct
bpf_struct_ops" is introduced as a common interface for the kernel struct that supportsBPF_PROG_TYPE_STRUCT_OPSprog. The supporting kernel struct will need to implement an instance of the "structbpf_struct_ops".The supporting kernel struct also needs to implement a
bpf_verifier_ops. DuringBPF_PROG_LOAD,bpf_struct_ops_find()will find the rightbpf_verifier_opsby searching theattr->attach_btf_id.A new "
btf_struct_access" is also added to thebpf_verifier_opssuch that the supporting kernel struct can optionally provide its own specific check on accessing the func arg (e.g. provide limited write access).
Quoted 8 lines
After
btf_vmlinuxis parsed, the newbpf_struct_ops_init()is called to initialize some values (e.g. the btf id of the supporting kernel struct) and it can only be done once thebtf_vmlinuxis available.The R0 checks at
BPF_EXITis excluded for theBPF_PROG_TYPE_STRUCT_OPSprog if the return type of theprog->aux->attach_func_protois "void".Signed-off-by Martin KaFai Lau <[email protected]>
Re: [PATCH bpf-next v2 05/11] bpf: Introduce BPF_PROG_TYPE_STRUCT_OPS
In reply to Martin KaFai LauQuoted 43 lines · Martin KaFai Lau
This patch allows the kernel's struct ops (i.e. func ptr) to be implemented in BPF. The first use case in this series is the "struct
tcp_congestion_ops" which will be introduced in a latter patch.This patch introduces a new prog type
BPF_PROG_TYPE_STRUCT_OPS. TheBPF_PROG_TYPE_STRUCT_OPSprog is verified against a particular func ptr of a kernel struct. Theattr->attach_btf_idis the btf id of a kernel struct. Theattr->expected_attach_typeis the member "index" of that kernel struct. The first member of a struct starts with member index 0. That will avoid ambiguity when a kernel struct has multiple func ptrs with the same func signature.For example, a
BPF_PROG_TYPE_STRUCT_OPSprog is written
to implement the "init" func ptr of the "structtcp_congestion_ops". Theattr->attach_btf_idis the btf id of the "structtcp_congestion_ops" of the _running_ kernel. Theattr->expected_attach_typeis 3.The ctx of
BPF_PROG_TYPE_STRUCT_OPSis an array of u64 args saved byarch_prepare_bpf_trampolinethat will be done in the next patch when introducingBPF_MAP_TYPE_STRUCT_OPS."struct
bpf_struct_ops" is introduced as a common interface for the kernel struct that supportsBPF_PROG_TYPE_STRUCT_OPSprog. The supporting kernel struct will need to implement an instance of the "structbpf_struct_ops".The supporting kernel struct also needs to implement a
bpf_verifier_ops. DuringBPF_PROG_LOAD,bpf_struct_ops_find()will find the rightbpf_verifier_opsby searching theattr->attach_btf_id.A new "
btf_struct_access" is also added to thebpf_verifier_opssuch that the supporting kernel struct can optionally provide its own specific check on accessing the func arg (e.g. provide limited write access).After
btf_vmlinuxis parsed, the newbpf_struct_ops_init()is called to initialize some values (e.g. the btf id of the supporting kernel struct) and it can only be done once thebtf_vmlinuxis available.The R0 checks at
BPF_EXITis excluded for theBPF_PROG_TYPE_STRUCT_OPSprog if the return type of theprog->aux->attach_func_protois "void".Signed-off-by Martin KaFai Lau <[email protected]>
Quoted 15 lines
--- include/linux/bpf.h | 30 +++++++ include/linux/bpf_types.h | 4 + include/linux/btf.h | 34 ++++++++ include/uapi/linux/bpf.h | 1 + kernel/bpf/Makefile | 2 +- kernel/bpf/bpf_struct_ops.c | 122 +++++++++++++++++++++++++++ kernel/bpf/bpf_struct_ops_types.h | 4 + kernel/bpf/btf.c | 88 ++++++++++++++------ kernel/bpf/syscall.c | 17 ++-- kernel/bpf/verifier.c | 134 +++++++++++++++++++++++------- 10 files changed, 372 insertions(+), 64 deletions(-) create mode 100644 kernel/bpf/bpf_struct_ops.c create mode 100644 kernel/bpf/bpf_struct_ops_types.h
All looks good, apart from the concern with partially-initialized bpf_struct_ops.
[...]
kernel/bpf/bpf_struct_ops.c line 38
Quoted 4 lines
kernel/bpf/bpf_struct_ops.c new file +4
-0+const struct bpf_prog_ops bpf_struct_ops_prog_ops = { +}; + +void bpf_struct_ops_init (struct btf *_btf_vmlinux)
this is always get passed vmlinux's btf, so why not call it short and sweet "btf"? _btf_vmlinux is kind of ugly and verbose.
Quoted 9 lines
kernel/bpf/bpf_struct_ops.c new file +9
-0+{ +const struct btf_member *member; +struct bpf_struct_ops *st_ops; +struct bpf_verifier_log log = {}; +const struct btf_type *t; +const char *mname; +s32 type_id; +u32 i, j; +
[...]
kernel/bpf/verifier.c static void print_verification_stats(struct bpf_verifier_env *env) · line 9537
Quoted 11 lines
kernel/bpf/verifier.c +11
-0+static int check_struct_ops_btf_id (struct bpf_verifier_env *env) +{ +const struct btf_type *t, *func_proto; +const struct bpf_struct_ops *st_ops; +const struct btf_member *member; +struct bpf_prog *prog = env->prog; +u32 btf_id, member_idx; +const char *mname; + + btf_id = prog->aux->attach_btf_id; + st_ops =bpf_struct_ops_find (btf_id);
if struct_ops initialization fails, type will be NULL and type_id will be 0, which we rely on here to not get partially-initialized bpf_struct_ops, right? Small comment mentioning this would be helpful.
Quoted 6 lines
kernel/bpf/verifier.c +6
-0+if (!st_ops) { +verbose (env,"attach_btf_id %u is not a supported struct\n" , + btf_id); +return -ENOTSUPP ; + } +
[...]
kernel/bpf/verifier.c static int check_attach_btf_id(struct bpf_verifier_env *env) · line 9596
Quoted 10 lines
static int
check_attach_btf_id(struct bpf_verifier_env *env){ struct bpf_prog *prog = env->prog;kernel/bpf/verifier.c +3
-0@@ -9520,6 +9591,9 @@ static int check_attach_btf_id(struct bpf_verifier_env *env)long addr;u64 key; +if (prog->type ==BPF_PROG_TYPE_STRUCT_OPS ) +return check_struct_ops_btf_id (env); +
There is a btf_id == 0 check below, you need to check that for STRUCT_OPS as well, otherwise you can get partially-initialized bpf_struct_ops struct in check_struct_ops_btf_id.
Quoted 6 lines
kernel/bpf/verifier.c +0
-1if (prog->type !=BPF_PROG_TYPE_TRACING )return 0 ; --2.17.1
Re: [PATCH bpf-next v2 05/11] bpf: Introduce BPF_PROG_TYPE_STRUCT_OPS
Quoted 118 lines · Andrii Nakryiko
Quoted 43 lines · Martin KaFai Lau
This patch allows the kernel's struct ops (i.e. func ptr) to be implemented in BPF. The first use case in this series is the "struct
tcp_congestion_ops" which will be introduced in a latter patch.This patch introduces a new prog type
BPF_PROG_TYPE_STRUCT_OPS. TheBPF_PROG_TYPE_STRUCT_OPSprog is verified against a particular func ptr of a kernel struct. Theattr->attach_btf_idis the btf id of a kernel struct. Theattr->expected_attach_typeis the member "index" of that kernel struct. The first member of a struct starts with member index 0. That will avoid ambiguity when a kernel struct has multiple func ptrs with the same func signature.For example, a
BPF_PROG_TYPE_STRUCT_OPSprog is written
to implement the "init" func ptr of the "structtcp_congestion_ops". Theattr->attach_btf_idis the btf id of the "structtcp_congestion_ops" of the _running_ kernel. Theattr->expected_attach_typeis 3.The ctx of
BPF_PROG_TYPE_STRUCT_OPSis an array of u64 args saved byarch_prepare_bpf_trampolinethat will be done in the next patch when introducingBPF_MAP_TYPE_STRUCT_OPS."struct
bpf_struct_ops" is introduced as a common interface for the kernel struct that supportsBPF_PROG_TYPE_STRUCT_OPSprog. The supporting kernel struct will need to implement an instance of the "structbpf_struct_ops".The supporting kernel struct also needs to implement a
bpf_verifier_ops. DuringBPF_PROG_LOAD,bpf_struct_ops_find()will find the rightbpf_verifier_opsby searching theattr->attach_btf_id.A new "
btf_struct_access" is also added to thebpf_verifier_opssuch that the supporting kernel struct can optionally provide its own specific check on accessing the func arg (e.g. provide limited write access).After
btf_vmlinuxis parsed, the newbpf_struct_ops_init()is called to initialize some values (e.g. the btf id of the supporting kernel struct) and it can only be done once thebtf_vmlinuxis available.The R0 checks at
BPF_EXITis excluded for theBPF_PROG_TYPE_STRUCT_OPSprog if the return type of theprog->aux->attach_func_protois "void".Signed-off-by Martin KaFai Lau <[email protected]>Quoted 15 lines
--- include/linux/bpf.h | 30 +++++++ include/linux/bpf_types.h | 4 + include/linux/btf.h | 34 ++++++++ include/uapi/linux/bpf.h | 1 + kernel/bpf/Makefile | 2 +- kernel/bpf/bpf_struct_ops.c | 122 +++++++++++++++++++++++++++ kernel/bpf/bpf_struct_ops_types.h | 4 + kernel/bpf/btf.c | 88 ++++++++++++++------ kernel/bpf/syscall.c | 17 ++-- kernel/bpf/verifier.c | 134 +++++++++++++++++++++++------- 10 files changed, 372 insertions(+), 64 deletions(-) create mode 100644 kernel/bpf/bpf_struct_ops.c create mode 100644 kernel/bpf/bpf_struct_ops_types.hAll looks good, apart from the concern with partially-initialized
bpf_struct_ops.[...]
kernel/bpf/bpf_struct_ops.c line 38
Quoted 4 lines
kernel/bpf/bpf_struct_ops.c new file +4
-0+const struct bpf_prog_ops bpf_struct_ops_prog_ops = { +}; + +void bpf_struct_ops_init (struct btf *_btf_vmlinux)this is always get passed vmlinux's btf, so why not call it short and sweet "btf"?
_btf_vmlinuxis kind of ugly and verbose.Quoted 9 lines
kernel/bpf/bpf_struct_ops.c new file +9
-0+{ +const struct btf_member *member; +struct bpf_struct_ops *st_ops; +struct bpf_verifier_log log = {}; +const struct btf_type *t; +const char *mname; +s32 type_id; +u32 i, j; +[...]
kernel/bpf/verifier.c static void print_verification_stats(struct bpf_verifier_env *env) · line 9537
Quoted 11 lines
kernel/bpf/verifier.c +11
-0+static int check_struct_ops_btf_id (struct bpf_verifier_env *env) +{ +const struct btf_type *t, *func_proto; +const struct bpf_struct_ops *st_ops; +const struct btf_member *member; +struct bpf_prog *prog = env->prog; +u32 btf_id, member_idx; +const char *mname; + + btf_id = prog->aux->attach_btf_id; + st_ops =bpf_struct_ops_find (btf_id);if
struct_opsinitialization fails, type will be NULL andtype_idwill be 0, which we rely on here to not get partially-initializedbpf_struct_ops, right? Small comment mentioning this would be helpful.Quoted 6 lines
kernel/bpf/verifier.c +6
-0+if (!st_ops) { +verbose (env,"attach_btf_id %u is not a supported struct\n" , + btf_id); +return -ENOTSUPP ; + } +[...]
Quoted 7 lines
static int
check_attach_btf_id(struct bpf_verifier_env *env){ struct bpf_prog *prog = env->prog;kernel/bpf/verifier.c +0
-0@@ -9520,6 +9591,9 @@ static int check_attach_btf_id(struct bpf_verifier_env *env)long addr;u64 key;
Quoted 7 lines
kernel/bpf/verifier.c static int check_attach_btf_id(struct bpf_verifier_env *env) · line 9596
Quoted 3 lines
kernel/bpf/verifier.c +3
-0+if (prog->type ==BPF_PROG_TYPE_STRUCT_OPS ) +return check_struct_ops_btf_id (env); +There is a
btf_id== 0 check below, you need to check that forSTRUCT_OPSas well, otherwise you can get partially-initializedbpf_struct_opsstruct incheck_struct_ops_btf_id.
This btf_id == 0 check is done at the beginning of bpf_struct_ops_find(). Hence, bpf_struct_ops_find() won't try to search if btf_id is 0.
st_ops fields is only set when everything passed, so individual st_ops will not be partially initialized.
Quoted 7 lines
Quoted 7 lines
kernel/bpf/verifier.c +0
-1if (prog->type !=BPF_PROG_TYPE_TRACING )return 0 ; --2.17.1
Andrii Nakryiko
Acked-byRe: [PATCH bpf-next v2 05/11] bpf: Introduce BPF_PROG_TYPE_STRUCT_OPS
Quoted 127 lines · Martin Lau
Quoted 118 lines · Andrii Nakryiko
Quoted 43 lines · Martin KaFai Lau
This patch allows the kernel's struct ops (i.e. func ptr) to be implemented in BPF. The first use case in this series is the "struct
tcp_congestion_ops" which will be introduced in a latter patch.This patch introduces a new prog type
BPF_PROG_TYPE_STRUCT_OPS. TheBPF_PROG_TYPE_STRUCT_OPSprog is verified against a particular func ptr of a kernel struct. Theattr->attach_btf_idis the btf id of a kernel struct. Theattr->expected_attach_typeis the member "index" of that kernel struct. The first member of a struct starts with member index 0. That will avoid ambiguity when a kernel struct has multiple func ptrs with the same func signature.For example, a
BPF_PROG_TYPE_STRUCT_OPSprog is written
to implement the "init" func ptr of the "structtcp_congestion_ops". Theattr->attach_btf_idis the btf id of the "structtcp_congestion_ops" of the _running_ kernel. Theattr->expected_attach_typeis 3.The ctx of
BPF_PROG_TYPE_STRUCT_OPSis an array of u64 args saved byarch_prepare_bpf_trampolinethat will be done in the next patch when introducingBPF_MAP_TYPE_STRUCT_OPS."struct
bpf_struct_ops" is introduced as a common interface for the kernel struct that supportsBPF_PROG_TYPE_STRUCT_OPSprog. The supporting kernel struct will need to implement an instance of the "structbpf_struct_ops".The supporting kernel struct also needs to implement a
bpf_verifier_ops. DuringBPF_PROG_LOAD,bpf_struct_ops_find()will find the rightbpf_verifier_opsby searching theattr->attach_btf_id.A new "
btf_struct_access" is also added to thebpf_verifier_opssuch that the supporting kernel struct can optionally provide its own specific check on accessing the func arg (e.g. provide limited write access).After
btf_vmlinuxis parsed, the newbpf_struct_ops_init()is called to initialize some values (e.g. the btf id of the supporting kernel struct) and it can only be done once thebtf_vmlinuxis available.The R0 checks at
BPF_EXITis excluded for theBPF_PROG_TYPE_STRUCT_OPSprog if the return type of theprog->aux->attach_func_protois "void".Signed-off-by Martin KaFai Lau <[email protected]>Quoted 15 lines
--- include/linux/bpf.h | 30 +++++++ include/linux/bpf_types.h | 4 + include/linux/btf.h | 34 ++++++++ include/uapi/linux/bpf.h | 1 + kernel/bpf/Makefile | 2 +- kernel/bpf/bpf_struct_ops.c | 122 +++++++++++++++++++++++++++ kernel/bpf/bpf_struct_ops_types.h | 4 + kernel/bpf/btf.c | 88 ++++++++++++++------ kernel/bpf/syscall.c | 17 ++-- kernel/bpf/verifier.c | 134 +++++++++++++++++++++++------- 10 files changed, 372 insertions(+), 64 deletions(-) create mode 100644 kernel/bpf/bpf_struct_ops.c create mode 100644 kernel/bpf/bpf_struct_ops_types.hAll looks good, apart from the concern with partially-initialized
bpf_struct_ops.[...]
kernel/bpf/bpf_struct_ops.c line 38
Quoted 4 lines
kernel/bpf/bpf_struct_ops.c new file +4
-0+const struct bpf_prog_ops bpf_struct_ops_prog_ops = { +}; + +void bpf_struct_ops_init (struct btf *_btf_vmlinux)this is always get passed vmlinux's btf, so why not call it short and sweet "btf"?
_btf_vmlinuxis kind of ugly and verbose.Quoted 9 lines
kernel/bpf/bpf_struct_ops.c new file +9
-0+{ +const struct btf_member *member; +struct bpf_struct_ops *st_ops; +struct bpf_verifier_log log = {}; +const struct btf_type *t; +const char *mname; +s32 type_id; +u32 i, j; +[...]
kernel/bpf/verifier.c static void print_verification_stats(struct bpf_verifier_env *env) · line 9537
Quoted 11 lines
kernel/bpf/verifier.c +11
-0+static int check_struct_ops_btf_id (struct bpf_verifier_env *env) +{ +const struct btf_type *t, *func_proto; +const struct bpf_struct_ops *st_ops; +const struct btf_member *member; +struct bpf_prog *prog = env->prog; +u32 btf_id, member_idx; +const char *mname; + + btf_id = prog->aux->attach_btf_id; + st_ops =bpf_struct_ops_find (btf_id);if
struct_opsinitialization fails, type will be NULL andtype_idwill be 0, which we rely on here to not get partially-initializedbpf_struct_ops, right? Small comment mentioning this would be helpful.Quoted 6 lines
kernel/bpf/verifier.c +6
-0+if (!st_ops) { +verbose (env,"attach_btf_id %u is not a supported struct\n" , + btf_id); +return -ENOTSUPP ; + } +[...]
Quoted 7 lines
static int
check_attach_btf_id(struct bpf_verifier_env *env){ struct bpf_prog *prog = env->prog;kernel/bpf/verifier.c +0
-0@@ -9520,6 +9591,9 @@ static int check_attach_btf_id(struct bpf_verifier_env *env)long addr;u64 key;Quoted 7 lines
kernel/bpf/verifier.c static int check_attach_btf_id(struct bpf_verifier_env *env) · line 9596
Quoted 3 lines
kernel/bpf/verifier.c +3
-0+if (prog->type ==BPF_PROG_TYPE_STRUCT_OPS ) +return check_struct_ops_btf_id (env); +There is a
btf_id== 0 check below, you need to check that forSTRUCT_OPSas well, otherwise you can get partially-initializedbpf_struct_opsstruct incheck_struct_ops_btf_id.
Quoted 3 lines
This
btf_id== 0 check is done at the beginning ofbpf_struct_ops_find(). Hence,bpf_struct_ops_find()won't try to search ifbtf_idis 0.
Ah right, I missed that check. Then yeah, it's not a concern. I still don't like _btf_vmlinux name, but that's just a nit.
Quoted 11 lines
st_opsfields is only set when everything passed, so individualst_opswill not be partially initialized.Quoted 7 lines
Quoted 7 lines
kernel/bpf/verifier.c +0
-1if (prog->type !=BPF_PROG_TYPE_TRACING )return 0 ; --2.17.1
kbuild test robot
build botRe: [PATCH bpf-next v2 05/11] bpf: Introduce BPF_PROG_TYPE_STRUCT_OPS
In reply to Martin KaFai LauI love your patch! Yet something to improve:
Hi Martin,
I love your patch! Yet something to improve:
[auto build test ERROR on bpf-next/master]
[cannot apply to bpf/master net/master v5.5-rc3 next-20191219]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system. BTW, we also suggest to use '--base' option to specify the base tree in git format-patch, please see https://stackoverflow.com/a/37406982]
url: https://github.com/0day-ci/linux/commits/Martin-KaFai-Lau/Introduce-BPF-STRUCT_OPS/20191224-085617 base: https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master config: arm64-defconfig (attached as .config) compiler: aarch64-linux-gcc (GCC) 7.5.0
reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
GCC_VERSION=7.5.0 make.cross ARCH=arm64
If you fix the issue, kindly add following tag
All errors (new ones prefixed by >>):
kernel/bpf/bpf_struct_ops.c: In function 'bpf_struct_ops_init':
>> kernel/bpf/bpf_struct_ops.c:86:8: error: implicit declaration of function 'btf_distill_func_proto'; did you mean 'btf_type_is_func_proto'? [-Werror=implicit-function-declaration]
btf_distill_func_proto(&log, _btf_vmlinux,
^~~~~~~~~~~~~~~~~~~~~~
btf_type_is_func_protocc1: some warnings being treated as errors
vim +86 kernel/bpf/bpf_struct_ops.c
37
38 void bpf_struct_ops_init(struct btf *_btf_vmlinux)
39 {
40 const struct btf_member *member;
41 struct bpf_struct_ops *st_ops;
42 struct bpf_verifier_log log = {};
43 const struct btf_type *t;
44 const char *mname;
45 s32 type_id;
46 u32 i, j;
47
48 for (i = 0; i < ARRAY_SIZE(bpf_struct_ops); i++) {
49 st_ops = bpf_struct_ops[i];
50
51 type_id = btf_find_by_name_kind(_btf_vmlinux, st_ops->name,
52 BTF_KIND_STRUCT);
53 if (type_id < 0) {
54 pr_warn("Cannot find struct %s in btf_vmlinux\n",
55 st_ops->name);
56 continue;
57 }
58 t = btf_type_by_id(_btf_vmlinux, type_id);
59 if (btf_type_vlen(t) > BPF_STRUCT_OPS_MAX_NR_MEMBERS) {
60 pr_warn("Cannot support #%u members in struct %s\n",
61 btf_type_vlen(t), st_ops->name);
62 continue;
63 }
64
65 for_each_member(j, t, member) {
66 const struct btf_type *func_proto;
67
68 mname = btf_name_by_offset(_btf_vmlinux,
69 member->name_off);
70 if (!*mname) {
71 pr_warn("anon member in struct %s is not supported\n",
72 st_ops->name);
73 break;
74 }
75
76 if (btf_member_bitfield_size(t, member)) {
77 pr_warn("bit field member %s in struct %s is not supported\n",
78 mname, st_ops->name);
79 break;
80 }
81
82 func_proto = btf_type_resolve_func_ptr(_btf_vmlinux,
83 member->type,
84 NULL);
85 if (func_proto &&
> 86 btf_distill_func_proto(&log, _btf_vmlinux,
87 func_proto, mname,
88 &st_ops->func_models[j])) {
89 pr_warn("Error in parsing func ptr %s in struct %s\n",
90 mname, st_ops->name);
91 break;
92 }
93 }
94
95 if (j == btf_type_vlen(t)) {
96 if (st_ops->init(_btf_vmlinux)) {
97 pr_warn("Error in init bpf_struct_ops %s\n",
98 st_ops->name);
99 } else { 100 st_ops->type_id = type_id;
101 st_ops->type = t;
102 }
103 }
104 }
105 }
106
---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org Intel Corporation
PATCH v2 6/11 bpf: Introduce BPF_MAP_TYPE_STRUCT_OPS
In reply to Martin KaFai LauThe patch introduces BPF_MAP_TYPE_STRUCT_OPS. The map value is a kernel struct with its func ptr implemented in bpf prog. This new map is the interface to register/unregister/introspect a bpf implemented kernel struct.
The kernel struct is actually embedded inside another new struct (or called the "value" struct in the code). For example, "struct tcp_congestion_ops" is embbeded in:
struct bpf_struct_ops_tcp_congestion_ops {
refcount_t refcnt;
enum bpf_struct_ops_state state;
struct tcp_congestion_ops data; /* <-- kernel subsystem struct here */
}The map value is "struct bpf_struct_ops_tcp_congestion_ops". The "bpftool map dump" will then be able to show the
state ("inuse"/"tobefree") and the number of subsystem's refcnt (e.g. number of tcp_sock in the tcp_congestion_ops case). This "value" struct is created automatically by a macro. Having a separate "value" struct will also make extending "struct bpf_struct_ops_XYZ" easier (e.g. adding "void (*init)(void)" to "struct bpf_struct_ops_XYZ" to do some initialization works before registering the struct_ops to the kernel subsystem). The libbpf will take care of finding and populating the "struct bpf_struct_ops_XYZ" from "struct XYZ".
Register a struct_ops to a kernel subsystem:
1. Load all needed BPF_PROG_TYPE_STRUCT_OPS prog(s)
2. Create a BPF_MAP_TYPE_STRUCT_OPS with attr->btf_vmlinux_value_type_id set to the btf id "struct bpf_struct_ops_tcp_congestion_ops" of the running kernel.
Instead of reusing the attr->btf_value_type_id, btf_vmlinux_value_type_id s added such that attr->btf_fd can still be used as the "user" btf which could store other useful sysadmin/debug info that may be introduced in the furture,
e.g. creation-date/compiler-details/map-creator...etc.
3. Create a "struct bpf_struct_ops_tcp_congestion_ops" object as described in the running kernel btf. Populate the value of this object. The function ptr should be populated with the prog fds.
4. Call BPF_MAP_UPDATE with the object created in (3) as
the map value. The key is always "0".
During BPF_MAP_UPDATE, the code that saves the kernel-func-ptr's args as an array of u64 is generated. BPF_MAP_UPDATE also allows the specific struct_ops to do some final checks in "st_ops->init_member()" (e.g. ensure all mandatory func ptrs are implemented).
If everything looks good, it will register this kernel struct to the kernel subsystem. The map will not allow further update from this point.
Unregister a struct_ops from the kernel subsystem:BPF_MAP_DELETE with key "0".
Introspect a struct_ops:BPF_MAP_LOOKUP_ELEM with key "0". The map value returned will have the prog _id_ populated as the func ptr.
The map value state (enum bpf_struct_ops_state) will transit from:
INIT (map created) =>
INUSE (map updated, i.e. reg) =>
TOBEFREE (map value deleted, i.e. unreg)
The kernel subsystem needs to call bpf_struct_ops_get() and bpf_struct_ops_put() to manage the "refcnt" in the
"struct bpf_struct_ops_XYZ". This patch uses a separate refcnt for the purose of tracking the subsystem usage. Another approach is to reuse the map->refcnt and then "show" (i.e. during map_lookup) the subsystem's usage by doing map->refcnt - map->usercnt to filter out the map-fd/pinned-map usage. However, that will also tie down the future semantics of map->refcnt and map->usercnt.
The very first subsystem's refcnt (during reg()) holds one count to map->refcnt. When the very last subsystem's refcnt is gone, it will also release the map->refcnt. All bpf_prog will be freed when the map->refcnt reaches 0 (i.e. during map_free()).
Here is how the bpftool map command will look like:
[root@arch-fb-vm1 bpf]# bpftool map show
6: struct_ops name dctcp flags 0x0
key 4B value 256B max_entries 1 memlock 4096B btf_id 6
[root@arch-fb-vm1 bpf]# bpftool map dump id 6
[{
"value": {
"refcnt": {
"refs": {
"counter": 1
}
},
"state": 1,
"data": {
"list": {
"next": 0,
"prev": 0
},
"key": 0,
"flags": 2,
"init": 24,
"release": 0,
"ssthresh": 25,
"cong_avoid": 30,
"set_state": 27,
"cwnd_event": 28,
"in_ack_event": 26,
"undo_cwnd": 29,
"pkts_acked": 0,
"min_tso_segs": 0,
"sndbuf_expand": 0,
"cong_control": 0,
"get_info": 0,
"name": [98,112,102,95,100,99,116,99,112,0,0,0,0,0,0,0
],
"owner": 0
}
}
}]
Misc Notes:
* bpf_struct_ops_map_sys_lookup_elem() is added for syscall lookup. It does an inplace update on "*value" instead returning a pointer to syscall.c. Otherwise, it needs a separate copy of "zero" value for the BPF_STRUCT_OPS_STATE_INIT to avoid races.
* The bpf_struct_ops_map_delete_elem() is also called without preempt_disable() from map_delete_elem(). It is because the "->unreg()" may requires sleepable context, e.g. the "tcp_unregister_congestion_control()".
* "const" is added to some of the existing "struct btf_func_model *" function arg to avoid a compiler warning caused by this patch.
--- arch/x86/net/bpf_jit_comp.c | 11 +- include/linux/bpf.h | 49 +++- include/linux/bpf_types.h | 3 + include/linux/btf.h | 13 + include/uapi/linux/bpf.h | 7 +- kernel/bpf/bpf_struct_ops.c | 468 +++++++++++++++++++++++++++++++++++- kernel/bpf/btf.c | 20 +- kernel/bpf/map_in_map.c | 3 +- kernel/bpf/syscall.c | 49 ++-- kernel/bpf/trampoline.c | 5 +- kernel/bpf/verifier.c | 5 + 11 files changed, 593 insertions(+), 40 deletions(-)
arch/x86/net/bpf_jit_comp.c +6 -5
@@ -1328,7 +1328,7 @@ xadd: if (is_imm8(insn->off))return proglen; } -static void save_regs (struct btf_func_model *m,u8 **prog,int nr_args, +static void save_regs (const struct btf_func_model *m,u8 **prog,int nr_args,int stack_size) {int i;
@@ -1344,7 +1344,7 @@ static void save_regs(struct btf_func_model *m, u8 **prog, int nr_args, -(stack_size - i * 8)); } -static void restore_regs (struct btf_func_model *m,u8 **prog,int nr_args, +static void restore_regs (const struct btf_func_model *m,u8 **prog,int nr_args,int stack_size) {int i;
@@ -1361,7 +1361,7 @@ static void restore_regs(struct btf_func_model *m, u8 **prog, int nr_args, -(stack_size - i * 8)); } -static int invoke_bpf (struct btf_func_model *m,u8 **pprog, +static int invoke_bpf (const struct btf_func_model *m,u8 **pprog,struct bpf_prog **progs,int prog_cnt,int stack_size) {u8 *prog = *pprog;
@@ -1456,7 +1456,8 @@ static int invoke_bpf(struct btf_func_model *m, u8 **pprog,* add rsp, 8 // skip eth_type_trans's frame * ret // return to its caller */ -int arch_prepare_bpf_trampoline (void *image,struct btf_func_model *m,u32 flags, +int arch_prepare_bpf_trampoline (void *image, +const struct btf_func_model *m,u32 flags,struct bpf_prog **fentry_progs,int fentry_cnt,struct bpf_prog **fexit_progs,int fexit_cnt,void *orig_call)
@@ -1529,7 +1530,7 @@ int arch_prepare_bpf_trampoline(void *image, struct btf_func_model *m, u32 flags*/ if (WARN_ON_ONCE (prog - (u8 *)image >PAGE_SIZE /2 -BPF_INSN_SAFETY ))return -EFAULT ; -return 0 ; +return (void *)prog - image; }static int emit_cond_near_jump (u8 **pprog,void *func,void *ip,u8 jmp_cond)
include/linux/bpf.h +47 -2
100 lines not on this page · raw message
include/linux/bpf_types.h +3 -0
@@ -109,3 +109,6 @@ BPF_MAP_TYPE(BPF_MAP_TYPE_REUSEPORT_SOCKARRAY, reuseport_array_ops)#endif BPF_MAP_TYPE(BPF_MAP_TYPE_QUEUE, queue_map_ops) BPF_MAP_TYPE(BPF_MAP_TYPE_STACK, stack_map_ops) +#if defined(CONFIG_BPF_JIT ) +BPF_MAP_TYPE(BPF_MAP_TYPE_STRUCT_OPS, bpf_struct_ops_map_ops) +#endif
include/linux/btf.h +13 -0
@@ -7,6 +7,8 @@#include <linux/types.h> #include <uapi/linux/btf.h> +#define BTF_TYPE_EMIT (type) ((void)(type *)0) +struct btf ;struct btf_member ;struct btf_type ;
@@ -60,6 +62,10 @@ const struct btf_type *btf_type_resolve_ptr(const struct btf *btf, u32 id, u32 *res_id);const struct btf_type *btf_type_resolve_func_ptr (const struct btf *btf,u32 id,u32 *res_id); +const struct btf_type * +btf_resolve_size (const struct btf *btf,const struct btf_type *type, +u32 *type_size,const struct btf_type **elem_type, +u32 *total_nelems);#define for_each_member (i, struct_type, member) \ for (i = 0, member = btf_type_member(struct_type); \
@@ -106,6 +112,13 @@ static inline bool btf_type_kflag(const struct btf_type *t)return BTF_INFO_KFLAG (t->info); } +static inline u32 btf_member_bit_offset (const struct btf_type *struct_type, +const struct btf_member *member) +{ +return btf_type_kflag (struct_type) ?BTF_MEMBER_BIT_OFFSET (member->offset) + : member->offset; +} +static inline u32 btf_member_bitfield_size (const struct btf_type *struct_type,const struct btf_member *member) {
include/uapi/linux/bpf.h +6 -1
@@ -136,6 +136,7 @@ enum bpf_map_type {BPF_MAP_TYPE_STACK ,BPF_MAP_TYPE_SK_STORAGE ,BPF_MAP_TYPE_DEVMAP_HASH , + BPF_MAP_TYPE_STRUCT_OPS, };/* Note that tracing related programs such as
@@ -398,6 +399,10 @@ union bpf_attr {__u32 btf_fd;/* fd pointing to a BTF type data */ __u32 btf_key_type_id;/* BTF type_id of the key */ __u32 btf_value_type_id;/* BTF type_id of the value */ +__u32 btf_vmlinux_value_type_id;/* BTF type_id of a kernel- +* struct stored as the +* map value +*/ };struct {/* anonymous struct used by BPF_MAP_*_ELEM commands */
@@ -3350,7 +3355,7 @@ struct bpf_map_info {__u32 map_flags;char name[BPF_OBJ_NAME_LEN ];__u32 ifindex; - __u32 :32 ; +__u32 btf_vmlinux_value_type_id;__u64 netns_dev;__u64 netns_ino;__u32 btf_id;
kernel/bpf/bpf_struct_ops.c +466 -2
508 lines not on this page · raw message
kernel/bpf/btf.c +9 -11
@@ -500,13 +500,6 @@ static const char *btf_int_encoding_str(u8 encoding)return "UNKN" ; } -static u32 btf_member_bit_offset (const struct btf_type *struct_type, -const struct btf_member *member) -{ -return btf_type_kflag (struct_type) ?BTF_MEMBER_BIT_OFFSET (member->offset) - : member->offset; -} -static u32 btf_type_int (const struct btf_type *t) {return *(u32 *)(t +1 );
@@ -1089,7 +1082,7 @@ static const struct resolve_vertex *env_stack_peak(struct btf_verifier_env *env)* *elem_type: same as return type ("struct X") * *total_nelems: 1 */ -static const struct btf_type * +const struct btf_type *btf_resolve_size (const struct btf *btf,const struct btf_type *type,u32 *type_size,const struct btf_type **elem_type,u32 *total_nelems)
@@ -1143,8 +1136,10 @@ btf_resolve_size(const struct btf *btf, const struct btf_type *type,return ERR_PTR (-EINVAL ); *type_size = nelems * size; - *total_nelems = nelems; - *elem_type = type; +if (total_nelems) + *total_nelems = nelems; +if (elem_type) + *elem_type = type;return array_type ? : type; }
@@ -1858,7 +1853,10 @@ static void btf_modifier_seq_show(const struct btf *btf, u32 type_id, void *data, u8 bits_offset, struct seq_file *m) { - t =btf_type_id_resolve (btf, &type_id); + if (btf->resolved_ids) + t =btf_type_id_resolve (btf, &type_id); + else + t =btf_type_skip_modifiers (btf, type_id,NULL );btf_type_ops (t)->seq_show (btf, t, type_id, data, bits_offset, m); }
kernel/bpf/map_in_map.c +2 -1
@@ -22,7 +22,8 @@ struct bpf_map *bpf_map_meta_alloc(int inner_map_ufd)*/ if (inner_map->map_type ==BPF_MAP_TYPE_PROG_ARRAY || inner_map->map_type ==BPF_MAP_TYPE_CGROUP_STORAGE || - inner_map->map_type ==BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE ) { + inner_map->map_type ==BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE || + inner_map->map_type ==BPF_MAP_TYPE_STRUCT_OPS ) {fdput (f);return ERR_PTR (-ENOTSUPP ); }
kernel/bpf/syscall.c +33 -16
97 lines not on this page · raw message
kernel/bpf/trampoline.c +3 -2
@@ -110,7 +110,7 @@ static int bpf_trampoline_update(struct bpf_trampoline *tr) fentry, fentry_cnt, fexit, fexit_cnt, tr->func.addr); -if (err) +if (err <0 ) goto out;if (tr->selector)
@@ -244,7 +244,8 @@ void notrace __bpf_prog_exit(struct bpf_prog *prog, u64 start) }int __weak -arch_prepare_bpf_trampoline (void *image,struct btf_func_model *m,u32 flags, +arch_prepare_bpf_trampoline (void *image, +const struct btf_func_model *m,u32 flags,struct bpf_prog **fentry_progs,int fentry_cnt,struct bpf_prog **fexit_progs,int fexit_cnt,void *orig_call)
kernel/bpf/verifier.c +5 -0
@@ -8149,6 +8149,11 @@ static int check_map_prog_compatibility(struct bpf_verifier_env *env,return -EINVAL ; } +if (map->map_type ==BPF_MAP_TYPE_STRUCT_OPS ) { +verbose (env,"bpf_struct_ops map cannot be used in prog\n" ); +return -EINVAL ; + } +return 0 ; }
Signature · 1 line
2.17.1
Re: [PATCH bpf-next v2 06/11] bpf: Introduce BPF_MAP_TYPE_STRUCT_OPS
Quoted 80 lines · Martin KaFai Lau
The patch introduces
BPF_MAP_TYPE_STRUCT_OPS. The map value is a kernel struct with its func ptr implemented in bpf prog. This new map is the interface to register/unregister/introspect a bpf implemented kernel struct.The kernel struct is actually embedded inside another new struct (or called the "value" struct in the code). For example, "struct
tcp_congestion_ops" is embbeded in:struct bpf_struct_ops_tcp_congestion_ops { refcount_t refcnt; enum bpf_struct_ops_state state; struct tcp_congestion_ops data; /* <-- kernel subsystem struct here */ }The map value is "struct
bpf_struct_ops_tcp_congestion_ops". The "bpftool map dump" will then be able to show the
state ("inuse"/"tobefree") and the number of subsystem's refcnt (e.g. number oftcp_sockin thetcp_congestion_opscase). This "value" struct is created automatically by a macro. Having a separate "value" struct will also make extending "structbpf_struct_ops_XYZ" easier (e.g. adding "void (*init)(void)" to "structbpf_struct_ops_XYZ" to do some initialization works before registering thestruct_opsto the kernel subsystem). The libbpf will take care of finding and populating the "structbpf_struct_ops_XYZ" from "struct XYZ".Register a
struct_opsto a kernel subsystem:
1. Load all neededBPF_PROG_TYPE_STRUCT_OPSprog(s)
2. Create aBPF_MAP_TYPE_STRUCT_OPSwithattr->btf_vmlinux_value_type_idset to the btf id "structbpf_struct_ops_tcp_congestion_ops" of the running kernel.
Instead of reusing theattr->btf_value_type_id,btf_vmlinux_value_type_ids added such thatattr->btf_fdcan still be used as the "user" btf which could store other useful sysadmin/debug info that may be introduced in the furture,
e.g. creation-date/compiler-details/map-creator...etc.
3. Create a "structbpf_struct_ops_tcp_congestion_ops" object as described in the running kernel btf. Populate the value of this object. The function ptr should be populated with the prog fds.
4. CallBPF_MAP_UPDATEwith the object created in (3) as
the map value. The key is always "0".During
BPF_MAP_UPDATE, the code that saves the kernel-func-ptr's args as an array of u64 is generated.BPF_MAP_UPDATEalso allows the specificstruct_opsto do some final checks in "st_ops->init_member()" (e.g. ensure all mandatory func ptrs are implemented).
If everything looks good, it will register this kernel struct to the kernel subsystem. The map will not allow further update from this point.Unregister a
struct_opsfrom the kernel subsystem:BPF_MAP_DELETEwith key "0".Introspect a
struct_ops:BPF_MAP_LOOKUP_ELEMwith key "0". The map value returned will have the prog _id_ populated as the func ptr.The map value state (enum
bpf_struct_ops_state) will transit from:
INIT (map created) =>
INUSE (map updated, i.e. reg) =>
TOBEFREE (map value deleted, i.e. unreg)The kernel subsystem needs to call
bpf_struct_ops_get()andbpf_struct_ops_put()to manage the "refcnt" in the
"structbpf_struct_ops_XYZ". This patch uses a separate refcnt for the purose of tracking the subsystem usage. Another approach is to reuse themap->refcntand then "show" (i.e. duringmap_lookup) the subsystem's usage by doingmap->refcnt-map->usercntto filter out the map-fd/pinned-map usage. However, that will also tie down the future semantics ofmap->refcntandmap->usercnt.The very first subsystem's refcnt (during
reg()) holds one count tomap->refcnt. When the very last subsystem's refcnt is gone, it will also release themap->refcnt. Allbpf_progwill be freed when themap->refcntreaches 0 (i.e. duringmap_free()).Here is how the bpftool map command will look like:
[root@arch-fb-vm1 bpf]# bpftool map show
6:struct_opsname dctcp flags0x0
key 4B value 256Bmax_entries1 memlock 4096Bbtf_id6
[root@arch-fb-vm1 bpf]# bpftool map dump id 6
Quoted 8 lines
[{
"value": {
"refcnt": {
"refs": {
"counter": 1
}
},
"state": 1,The bpftool dump with "state" 1 is a little bit cryptic. Since this is common for all struct_ops maps, can we make it explicit, e.g., as enum values, like INIT/INUSE/TOBEFREE?
Quoted 57 lines
"data": { "list": { "next": 0, "prev": 0 }, "key": 0, "flags": 2, "init": 24, "release": 0, "ssthresh": 25, "cong_avoid": 30, "set_state": 27, "cwnd_event": 28, "in_ack_event": 26, "undo_cwnd": 29, "pkts_acked": 0, "min_tso_segs": 0, "sndbuf_expand": 0, "cong_control": 0, "get_info": 0, "name": [98,112,102,95,100,99,116,99,112,0,0,0,0,0,0,0 ], "owner": 0 } } }]
Misc Notes:
*bpf_struct_ops_map_sys_lookup_elem()is added for syscall lookup. It does an inplace update on "*value" instead returning a pointer tosyscall.c. Otherwise, it needs a separate copy of "zero" value for theBPF_STRUCT_OPS_STATE_INITto avoid races.* The
bpf_struct_ops_map_delete_elem()is also called withoutpreempt_disable()frommap_delete_elem(). It is because the "->unreg()" may requires sleepable context, e.g. the "tcp_unregister_congestion_control()".* "const" is added to some of the existing "struct
btf_func_model*" function arg to avoid a compiler warning caused by this patch.Signed-off-by Martin KaFai Lau <[email protected]>--- arch/x86/net/bpf_jit_comp.c | 11 +- include/linux/bpf.h | 49 +++- include/linux/bpf_types.h | 3 + include/linux/btf.h | 13 + include/uapi/linux/bpf.h | 7 +- kernel/bpf/bpf_struct_ops.c | 468 +++++++++++++++++++++++++++++++++++- kernel/bpf/btf.c | 20 +- kernel/bpf/map_in_map.c | 3 +- kernel/bpf/syscall.c | 49 ++-- kernel/bpf/trampoline.c | 5 +- kernel/bpf/verifier.c | 5 + 11 files changed, 593 insertions(+), 40 deletions(-)
[...]
Quoted 116 lines
include/uapi/linux/bpf.h +6
-1@@ -136,6 +136,7 @@ enum bpf_map_type {BPF_MAP_TYPE_STACK ,BPF_MAP_TYPE_SK_STORAGE ,BPF_MAP_TYPE_DEVMAP_HASH , + BPF_MAP_TYPE_STRUCT_OPS, };/* Note that tracing related programs such as @@ -398,6 +399,10 @@ union bpf_attr {__u32 btf_fd;/* fd pointing to a BTF type data */ __u32 btf_key_type_id;/* BTF type_id of the key */ __u32 btf_value_type_id;/* BTF type_id of the value */ +__u32 btf_vmlinux_value_type_id;/* BTF type_id of a kernel- +* struct stored as the +* map value +*/ };struct {/* anonymous struct used by BPF_MAP_*_ELEM commands */ @@ -3350,7 +3355,7 @@ struct bpf_map_info {__u32 map_flags;char name[BPF_OBJ_NAME_LEN ];__u32 ifindex; - __u32 :32 ; +__u32 btf_vmlinux_value_type_id;__u64 netns_dev;__u64 netns_ino;__u32 btf_id;kernel/bpf/bpf_struct_ops.c +62
-1@@ -10,8 +10,66 @@#include <linux/seq_file.h> #include <linux/refcount.h> +enum bpf_struct_ops_state { +BPF_STRUCT_OPS_STATE_INIT , +BPF_STRUCT_OPS_STATE_INUSE , +BPF_STRUCT_OPS_STATE_TOBEFREE , +}; + +#define BPF_STRUCT_OPS_COMMON_VALUE \ + refcount_t refcnt; \ + enum bpf_struct_ops_state state + +struct bpf_struct_ops_value { +BPF_STRUCT_OPS_COMMON_VALUE ; +char data[0 ]____cacheline_aligned_in_smp ; +}; + +struct bpf_struct_ops_map { +struct bpf_map map; +const struct bpf_struct_ops *st_ops; +/* protect map_update */ +spinlock_t lock; +/* progs has all the bpf_prog that is populated +* to the func ptr of the kernel's struct +* (in kvalue.data). +*/ +struct bpf_prog **progs; +/* image is a page that has all the trampolines +* that stores the func args before calling the bpf_prog. +* A PAGE_SIZE "image" is enough to store all trampoline for +* "progs[]". +*/ +void *image; +/* uvalue->data stores the kernel struct +* (e.g. tcp_congestion_ops) that is more useful +* to userspace than the kvalue. For example, +* the bpf_prog's id is stored instead of the kernel +* address of a func ptr. +*/ +struct bpf_struct_ops_value *uvalue; +/* kvalue.data stores the actual kernel's struct +* (e.g. tcp_congestion_ops) that will be +* registered to the kernel subsystem. +*/ +struct bpf_struct_ops_value kvalue; +}; + +#define VALUE_PREFIX "bpf_struct_ops_" +#define VALUE_PREFIX_LEN (sizeof(VALUE_PREFIX) - 1) + +/* bpf_struct_ops_##_name (e.g. bpf_struct_ops_tcp_congestion_ops) is +* the map's value exposed to the userspace and its btf-type-id is +* stored at the map->btf_vmlinux_value_type_id. +* +*/ #define BPF_STRUCT_OPS_TYPE (_name) \ -extern struct bpf_struct_ops bpf_##_name; +extern struct bpf_struct_ops bpf_##_name; \ + \ +struct bpf_struct_ops_##_name { \ + BPF_STRUCT_OPS_COMMON_VALUE; \ + struct _name data ____cacheline_aligned_in_smp; \ +};#include "bpf_struct_ops_types.h" #undef BPF_STRUCT_OPS_TYPE@@ -35,19 +93,51 @@ const struct bpf_verifier_ops bpf_struct_ops_verifier_ops = {const struct bpf_prog_ops bpf_struct_ops_prog_ops = { }; +static const struct btf_type *module_type; +void bpf_struct_ops_init (struct btf *_btf_vmlinux) { +s32 type_id, value_id, module_id;const struct btf_member *member;struct bpf_struct_ops *st_ops;struct bpf_verifier_log log = {};
kernel/bpf/bpf_struct_ops.c const struct bpf_verifier_ops bpf_struct_ops_verifier_ops = { · line 112
Quoted 10 lines
kernel/bpf/bpf_struct_ops.c +5
-1const struct btf_type *t; +char value_name[128 ];const char *mname; -s32 type_id;u32 i, j; +/* Ensure BTF type is emitted for "struct bpf_struct_ops_##_name" */ +#define BPF_STRUCT_OPS_TYPE (_name) BTF_TYPE_EMIT(struct bpf_struct_ops_##_name); +#include "bpf_struct_ops_types.h" +#undef BPF_STRUCT_OPS_TYPE
This looks great!
Quoted 45 lines
kernel/bpf/bpf_struct_ops.c +28
-0+ + module_id =btf_find_by_name_kind (_btf_vmlinux,"module" , +BTF_KIND_STRUCT ); +if (module_id <0 ) { +pr_warn ("Cannot find struct module in btf_vmlinux\n" ); +return ; + } + module_type =btf_type_by_id (_btf_vmlinux, module_id); +for (i =0 ; i <ARRAY_SIZE (bpf_struct_ops); i++) { st_ops = bpf_struct_ops[i]; +if (strlen (st_ops->name) +VALUE_PREFIX_LEN >= +sizeof (value_name)) { +pr_warn ("struct_ops name %s is too long\n" , + st_ops->name); +continue ; + } +sprintf (value_name,"%s%s" ,VALUE_PREFIX , st_ops->name); + + value_id =btf_find_by_name_kind (_btf_vmlinux, value_name, +BTF_KIND_STRUCT ); +if (value_id <0 ) { +pr_warn ("Cannot find struct %s in btf_vmlinux\n" , + value_name); +continue ; + } + type_id =btf_find_by_name_kind (_btf_vmlinux, st_ops->name,BTF_KIND_STRUCT );if (type_id <0 ) {@@ -99,6 +189,9 @@ void bpf_struct_ops_init(struct btf *_btf_vmlinux) }else { st_ops->type_id = type_id; st_ops->type = t; + st_ops->value_id = value_id; + st_ops->value_type = +btf_type_by_id (_btf_vmlinux, value_id); } } }@@ -106,6 +199,22 @@ void bpf_struct_ops_init(struct btf *_btf_vmlinux)extern struct btf *btf_vmlinux;
[...]
Quoted 107 lines
kernel/bpf/bpf_struct_ops.c +107
-0+ +static int bpf_struct_ops_map_update_elem (struct bpf_map *map,void *key, +void *value,u64 flags) +{ +struct bpf_struct_ops_map *st_map = (struct bpf_struct_ops_map *)map; +const struct bpf_struct_ops *st_ops = st_map->st_ops; +struct bpf_struct_ops_value *uvalue, *kvalue; +const struct btf_member *member; +const struct btf_type *t = st_ops->type; +void *udata, *kdata; +int prog_fd, err =0 ; +void *image; +u32 i; + +if (flags) +return -EINVAL ; + +if (*(u32 *)key !=0 ) +return -E2BIG ; + + uvalue = (struct bpf_struct_ops_value *)value; +if (uvalue->state ||refcount_read (&uvalue->refcnt)) +return -EINVAL ; + + uvalue = (struct bpf_struct_ops_value *)st_map->uvalue; + kvalue = (struct bpf_struct_ops_value *)&st_map->kvalue; + +spin_lock (&st_map->lock); + +if (kvalue->state !=BPF_STRUCT_OPS_STATE_INIT ) { + err = -EBUSY ; + goto unlock; + } + +memcpy (uvalue, value, map->value_size); + + udata = &uvalue->data; + kdata = &kvalue->data; + image = st_map->image; + +for_each_member (i, t, member) { +const struct btf_type *mtype, *ptype; +struct bpf_prog *prog; +u32 moff; + + moff =btf_member_bit_offset (t, member) /8 ; + mtype =btf_type_by_id (btf_vmlinux, member->type); + ptype =btf_type_resolve_ptr (btf_vmlinux, member->type,NULL ); +if (ptype == module_type) { + *(void **)(kdata + moff) =BPF_MODULE_OWNER ; +continue ; + } + + err = st_ops->init_member (t, member, kdata, udata); +if (err <0 ) + goto reset_unlock; + +/* The ->init_member() has handled this member */ +if (err >0 ) +continue ; + +/* If st_ops->init_member does not handle it, +* we will only handle func ptrs and zero-ed members +* here. Reject everything else. +*/ + +/* All non func ptr member must be 0 */ +if (!btf_type_resolve_func_ptr (btf_vmlinux, member->type, +NULL )) { +u32 msize; + + mtype =btf_resolve_size (btf_vmlinux, mtype, + &msize,NULL ,NULL ); +if (IS_ERR (mtype)) { + err =PTR_ERR (mtype); + goto reset_unlock; + } + +if (memchr_inv (udata + moff,0 , msize)) { + err = -EINVAL ; + goto reset_unlock; + } + +continue ; + } + + prog_fd = (int )(*(unsigned long *)(udata + moff)); +/* Similar check as the attr->attach_prog_fd */ +if (!prog_fd) +continue ; + + prog =bpf_prog_get (prog_fd); +if (IS_ERR (prog)) { + err =PTR_ERR (prog); + goto reset_unlock; + } + st_map->progs[i] = prog; + +if (prog->type !=BPF_PROG_TYPE_STRUCT_OPS || + prog->aux->attach_btf_id != st_ops->type_id || + prog->expected_attach_type != i) { + err = -EINVAL ; + goto reset_unlock; + } + + err = arch_prepare_bpf_trampoline(image, + &st_ops->func_models[i], 0,
kernel/bpf/bpf_struct_ops.c const struct bpf_struct_ops *bpf_struct_ops_find(u32 type_id) · line 405
Quoted 10 lines
kernel/bpf/bpf_struct_ops.c +10
-0+ &prog, 1, NULL, 0, NULL); +if (err <0 ) + goto reset_unlock; + + *(void **)(kdata + moff) = image; + image += err; + +/* put prog_id to udata */ + *(unsigned long *)(udata + moff) = prog->aux->id; + }
Should we check whether user indeed provided module member or not before declaring success?
Quoted 24 lines
kernel/bpf/bpf_struct_ops.c +24
-0+ +refcount_set (&kvalue->refcnt,1 ); +bpf_map_inc (map); + + err = st_ops->reg (kdata); +if (!err) { +/* Pair with smp_load_acquire() during lookup */ +smp_store_release (&kvalue->state,BPF_STRUCT_OPS_STATE_INUSE ); + goto unlock; + } + +/* Error during st_ops->reg() */ +bpf_map_put (map); + +reset_unlock: +bpf_struct_ops_map_put_progs (st_map); +memset (uvalue,0 , map->value_size); +memset (kvalue,0 , map->value_size); + +unlock: +spin_unlock (&st_map->lock); +return err; +} +
[...]
Quoted 44 lines
kernel/bpf/bpf_struct_ops.c +44
-0+ +static struct bpf_map *bpf_struct_ops_map_alloc (union bpf_attr *attr) +{ +const struct bpf_struct_ops *st_ops; +size_t map_total_size, st_map_size; +struct bpf_struct_ops_map *st_map; +const struct btf_type *t, *vt; +struct bpf_map_memory mem; +struct bpf_map *map; +int err; + +if (!capable (CAP_SYS_ADMIN )) +return ERR_PTR (-EPERM ); + + st_ops =bpf_struct_ops_find_value (attr->btf_vmlinux_value_type_id); +if (!st_ops) +return ERR_PTR (-ENOTSUPP ); + + vt = st_ops->value_type; +if (attr->value_size != vt->size) +return ERR_PTR (-EINVAL ); + + t = st_ops->type; + + st_map_size =sizeof (*st_map) + +/* kvalue stores the +* struct bpf_struct_ops_tcp_congestions_ops +*/ + (vt->size -sizeof (struct bpf_struct_ops_value )); + map_total_size = st_map_size + +/* uvalue */ +sizeof (vt->size) + +/* struct bpf_progs **progs */ +btf_type_vlen (t) *sizeof (struct bpf_prog *); + err =bpf_map_charge_init (&mem, map_total_size); +if (err <0 ) +return ERR_PTR (err); + + st_map =bpf_map_area_alloc (st_map_size,NUMA_NO_NODE ); +if (!st_map) { +bpf_map_charge_finish (&mem); +return ERR_PTR (-ENOMEM ); + } + st_map->st_ops = st_ops;
kernel/bpf/bpf_struct_ops.c const struct bpf_struct_ops *bpf_struct_ops_find(u32 type_id) · line 534
Quoted 10 lines
kernel/bpf/bpf_struct_ops.c +10
-0+ map = &st_map->map; + + st_map->uvalue =bpf_map_area_alloc (vt->size,NUMA_NO_NODE ); + st_map->progs = +bpf_map_area_alloc (btf_type_vlen (t) *sizeof (struct bpf_prog *), +NUMA_NO_NODE ); +/* Each trampoline costs < 64 bytes. Ensure one page +* is enough for max number of func ptrs. +*/ +BUILD_BUG_ON (PAGE_SIZE /64 <BPF_STRUCT_OPS_MAX_NR_MEMBERS );
This maybe true for x86 now, but it may not hold up for future other architectures. Not sure whether we should get the value for arch call backs, or we just bail out during map update if we ever grow exceeds one page.
Quoted 26 lines
kernel/bpf/bpf_struct_ops.c +26
-0+ st_map->image =bpf_jit_alloc_exec (PAGE_SIZE ); +if (!st_map->uvalue || !st_map->progs || !st_map->image) { +bpf_struct_ops_map_free (map); +bpf_map_charge_finish (&mem); +return ERR_PTR (-ENOMEM ); + } + +spin_lock_init (&st_map->lock); +set_vm_flush_reset_perms (st_map->image); +set_memory_x ((long )st_map->image,1 ); +bpf_map_init_from_attr (map, attr); +bpf_map_charge_move (&map->memory, &mem); + +return map; +} + +const struct bpf_map_ops bpf_struct_ops_map_ops = { + .map_alloc_check = bpf_struct_ops_map_alloc_check, + .map_alloc = bpf_struct_ops_map_alloc, + .map_free = bpf_struct_ops_map_free, + .map_get_next_key = bpf_struct_ops_map_get_next_key, + .map_lookup_elem = bpf_struct_ops_map_lookup_elem, + .map_delete_elem = bpf_struct_ops_map_delete_elem, + .map_update_elem = bpf_struct_ops_map_update_elem, + .map_seq_show_elem = bpf_struct_ops_map_seq_show_elem, +};
[...]
Re: [PATCH bpf-next v2 06/11] bpf: Introduce BPF_MAP_TYPE_STRUCT_OPS
Quoted 93 lines · Yonghong Song
Quoted 80 lines · Martin KaFai Lau
The patch introduces
BPF_MAP_TYPE_STRUCT_OPS. The map value is a kernel struct with its func ptr implemented in bpf prog. This new map is the interface to register/unregister/introspect a bpf implemented kernel struct.The kernel struct is actually embedded inside another new struct (or called the "value" struct in the code). For example, "struct
tcp_congestion_ops" is embbeded in:struct bpf_struct_ops_tcp_congestion_ops { refcount_t refcnt; enum bpf_struct_ops_state state; struct tcp_congestion_ops data; /* <-- kernel subsystem struct here */ }The map value is "struct
bpf_struct_ops_tcp_congestion_ops". The "bpftool map dump" will then be able to show the
state ("inuse"/"tobefree") and the number of subsystem's refcnt (e.g. number oftcp_sockin thetcp_congestion_opscase). This "value" struct is created automatically by a macro. Having a separate "value" struct will also make extending "structbpf_struct_ops_XYZ" easier (e.g. adding "void (*init)(void)" to "structbpf_struct_ops_XYZ" to do some initialization works before registering thestruct_opsto the kernel subsystem). The libbpf will take care of finding and populating the "structbpf_struct_ops_XYZ" from "struct XYZ".Register a
struct_opsto a kernel subsystem:
1. Load all neededBPF_PROG_TYPE_STRUCT_OPSprog(s)
2. Create aBPF_MAP_TYPE_STRUCT_OPSwithattr->btf_vmlinux_value_type_idset to the btf id "structbpf_struct_ops_tcp_congestion_ops" of the running kernel.
Instead of reusing theattr->btf_value_type_id,btf_vmlinux_value_type_ids added such thatattr->btf_fdcan still be used as the "user" btf which could store other useful sysadmin/debug info that may be introduced in the furture,
e.g. creation-date/compiler-details/map-creator...etc.
3. Create a "structbpf_struct_ops_tcp_congestion_ops" object as described in the running kernel btf. Populate the value of this object. The function ptr should be populated with the prog fds.
4. CallBPF_MAP_UPDATEwith the object created in (3) as
the map value. The key is always "0".During
BPF_MAP_UPDATE, the code that saves the kernel-func-ptr's args as an array of u64 is generated.BPF_MAP_UPDATEalso allows the specificstruct_opsto do some final checks in "st_ops->init_member()" (e.g. ensure all mandatory func ptrs are implemented).
If everything looks good, it will register this kernel struct to the kernel subsystem. The map will not allow further update from this point.Unregister a
struct_opsfrom the kernel subsystem:BPF_MAP_DELETEwith key "0".Introspect a
struct_ops:BPF_MAP_LOOKUP_ELEMwith key "0". The map value returned will have the prog _id_ populated as the func ptr.The map value state (enum
bpf_struct_ops_state) will transit from:
INIT (map created) =>
INUSE (map updated, i.e. reg) =>
TOBEFREE (map value deleted, i.e. unreg)The kernel subsystem needs to call
bpf_struct_ops_get()andbpf_struct_ops_put()to manage the "refcnt" in the
"structbpf_struct_ops_XYZ". This patch uses a separate refcnt for the purose of tracking the subsystem usage. Another approach is to reuse themap->refcntand then "show" (i.e. duringmap_lookup) the subsystem's usage by doingmap->refcnt-map->usercntto filter out the map-fd/pinned-map usage. However, that will also tie down the future semantics ofmap->refcntandmap->usercnt.The very first subsystem's refcnt (during
reg()) holds one count tomap->refcnt. When the very last subsystem's refcnt is gone, it will also release themap->refcnt. Allbpf_progwill be freed when themap->refcntreaches 0 (i.e. duringmap_free()).Here is how the bpftool map command will look like:
[root@arch-fb-vm1 bpf]# bpftool map show
6:struct_opsname dctcp flags0x0
key 4B value 256Bmax_entries1 memlock 4096Bbtf_id6
[root@arch-fb-vm1 bpf]# bpftool map dump id 6Quoted 8 lines
[{ "value": { "refcnt": { "refs": { "counter": 1 } }, "state": 1,
Quoted 3 lines
The bpftool dump with "state" 1 is a little bit cryptic. Since this is common for all
struct_opsmaps, can we make it explicit, e.g., as enum values, like INIT/INUSE/TOBEFREE?
This can (and probably should) be done generically in bpftool for any field of enum type. Not blocking this patch set, though.
Quoted 15 lines
Quoted 15 lines
"data": { "list": { "next": 0, "prev": 0 }, "key": 0, "flags": 2, "init": 24, "release": 0, "ssthresh": 25, "cong_avoid": 30, "set_state": 27, "cwnd_event": 28, "in_ack_event": 26,
Quoted 8 lines
Quoted 8 lines
"undo_cwnd": 29, "pkts_acked": 0, "min_tso_segs": 0, "sndbuf_expand": 0, "cong_control": 0, "get_info": 0, "name": [98,112,102,95,100,99,116,99,112,0,0,0,0,0,0,0 ],
Same here, bpftool should be smart enough to figure out that this is a string, not just an array of bytes.
Quoted 6 lines
Quoted 6 lines
"owner": 0 } } }]
[...]
Re: [PATCH bpf-next v2 06/11] bpf: Introduce BPF_MAP_TYPE_STRUCT_OPS
Quoted 125 lines · Andrii Nakryiko
Quoted 93 lines · Yonghong Song
Quoted 80 lines · Martin KaFai Lau
The patch introduces
BPF_MAP_TYPE_STRUCT_OPS. The map value is a kernel struct with its func ptr implemented in bpf prog. This new map is the interface to register/unregister/introspect a bpf implemented kernel struct.The kernel struct is actually embedded inside another new struct (or called the "value" struct in the code). For example, "struct
tcp_congestion_ops" is embbeded in:struct bpf_struct_ops_tcp_congestion_ops { refcount_t refcnt; enum bpf_struct_ops_state state; struct tcp_congestion_ops data; /* <-- kernel subsystem struct here */ }The map value is "struct
bpf_struct_ops_tcp_congestion_ops". The "bpftool map dump" will then be able to show the
state ("inuse"/"tobefree") and the number of subsystem's refcnt (e.g. number oftcp_sockin thetcp_congestion_opscase). This "value" struct is created automatically by a macro. Having a separate "value" struct will also make extending "structbpf_struct_ops_XYZ" easier (e.g. adding "void (*init)(void)" to "structbpf_struct_ops_XYZ" to do some initialization works before registering thestruct_opsto the kernel subsystem). The libbpf will take care of finding and populating the "structbpf_struct_ops_XYZ" from "struct XYZ".Register a
struct_opsto a kernel subsystem:
1. Load all neededBPF_PROG_TYPE_STRUCT_OPSprog(s)
2. Create aBPF_MAP_TYPE_STRUCT_OPSwithattr->btf_vmlinux_value_type_idset to the btf id "structbpf_struct_ops_tcp_congestion_ops" of the running kernel.
Instead of reusing theattr->btf_value_type_id,btf_vmlinux_value_type_ids added such thatattr->btf_fdcan still be used as the "user" btf which could store other useful sysadmin/debug info that may be introduced in the furture,
e.g. creation-date/compiler-details/map-creator...etc.
3. Create a "structbpf_struct_ops_tcp_congestion_ops" object as described in the running kernel btf. Populate the value of this object. The function ptr should be populated with the prog fds.
4. CallBPF_MAP_UPDATEwith the object created in (3) as
the map value. The key is always "0".During
BPF_MAP_UPDATE, the code that saves the kernel-func-ptr's args as an array of u64 is generated.BPF_MAP_UPDATEalso allows the specificstruct_opsto do some final checks in "st_ops->init_member()" (e.g. ensure all mandatory func ptrs are implemented).
If everything looks good, it will register this kernel struct to the kernel subsystem. The map will not allow further update from this point.Unregister a
struct_opsfrom the kernel subsystem:BPF_MAP_DELETEwith key "0".Introspect a
struct_ops:BPF_MAP_LOOKUP_ELEMwith key "0". The map value returned will have the prog _id_ populated as the func ptr.The map value state (enum
bpf_struct_ops_state) will transit from:
INIT (map created) =>
INUSE (map updated, i.e. reg) =>
TOBEFREE (map value deleted, i.e. unreg)The kernel subsystem needs to call
bpf_struct_ops_get()andbpf_struct_ops_put()to manage the "refcnt" in the
"structbpf_struct_ops_XYZ". This patch uses a separate refcnt for the purose of tracking the subsystem usage. Another approach is to reuse themap->refcntand then "show" (i.e. duringmap_lookup) the subsystem's usage by doingmap->refcnt-map->usercntto filter out the map-fd/pinned-map usage. However, that will also tie down the future semantics ofmap->refcntandmap->usercnt.The very first subsystem's refcnt (during
reg()) holds one count tomap->refcnt. When the very last subsystem's refcnt is gone, it will also release themap->refcnt. Allbpf_progwill be freed when themap->refcntreaches 0 (i.e. duringmap_free()).Here is how the bpftool map command will look like:
[root@arch-fb-vm1 bpf]# bpftool map show
6:struct_opsname dctcp flags0x0
key 4B value 256Bmax_entries1 memlock 4096Bbtf_id6
[root@arch-fb-vm1 bpf]# bpftool map dump id 6Quoted 8 lines
[{ "value": { "refcnt": { "refs": { "counter": 1 } }, "state": 1,Quoted 3 lines
The bpftool dump with "state" 1 is a little bit cryptic. Since this is common for all
struct_opsmaps, can we make it explicit, e.g., as enum values, like INIT/INUSE/TOBEFREE?This can (and probably should) be done generically in bpftool for any field of enum type. Not blocking this patch set, though.
Quoted 15 lines
Quoted 15 lines
"data": { "list": { "next": 0, "prev": 0 }, "key": 0, "flags": 2, "init": 24, "release": 0, "ssthresh": 25, "cong_avoid": 30, "set_state": 27, "cwnd_event": 28, "in_ack_event": 26,Quoted 8 lines
Quoted 8 lines
"undo_cwnd": 29, "pkts_acked": 0, "min_tso_segs": 0, "sndbuf_expand": 0, "cong_control": 0, "get_info": 0, "name": [98,112,102,95,100,99,116,99,112,0,0,0,0,0,0,0 ],
Quoted 2 lines
Same here, bpftool should be smart enough to figure out that this is a string, not just an array of bytes.
Agree on both above that bpftool can print better strings. Those are generic improvements to bpftool and not specific to a particular map type.
Quoted 9 lines
Quoted 7 lines
Quoted 7 lines
"owner": 0 } } }]
[...]
Re: [PATCH bpf-next v2 06/11] bpf: Introduce BPF_MAP_TYPE_STRUCT_OPS
In reply to Yonghong SongOn Mon, Dec 23, 2019 at 11:57:48AM -0800, Yonghong Song wrote:
[ ... ]
Quoted 111 lines
Quoted 111 lines
kernel/bpf/bpf_struct_ops.c +111
-0+static int bpf_struct_ops_map_update_elem (struct bpf_map *map,void *key, +void *value,u64 flags) +{ +struct bpf_struct_ops_map *st_map = (struct bpf_struct_ops_map *)map; +const struct bpf_struct_ops *st_ops = st_map->st_ops; +struct bpf_struct_ops_value *uvalue, *kvalue; +const struct btf_member *member; +const struct btf_type *t = st_ops->type; +void *udata, *kdata; +int prog_fd, err =0 ; +void *image; +u32 i; + +if (flags) +return -EINVAL ; + +if (*(u32 *)key !=0 ) +return -E2BIG ; + + uvalue = (struct bpf_struct_ops_value *)value; +if (uvalue->state ||refcount_read (&uvalue->refcnt)) +return -EINVAL ; + + uvalue = (struct bpf_struct_ops_value *)st_map->uvalue; + kvalue = (struct bpf_struct_ops_value *)&st_map->kvalue; + +spin_lock (&st_map->lock); + +if (kvalue->state !=BPF_STRUCT_OPS_STATE_INIT ) { + err = -EBUSY ; + goto unlock; + } + +memcpy (uvalue, value, map->value_size); + + udata = &uvalue->data; + kdata = &kvalue->data; + image = st_map->image; + +for_each_member (i, t, member) { +const struct btf_type *mtype, *ptype; +struct bpf_prog *prog; +u32 moff; + + moff =btf_member_bit_offset (t, member) /8 ; + mtype =btf_type_by_id (btf_vmlinux, member->type); + ptype =btf_type_resolve_ptr (btf_vmlinux, member->type,NULL ); +if (ptype == module_type) { + *(void **)(kdata + moff) =BPF_MODULE_OWNER ; +continue ; + } + + err = st_ops->init_member (t, member, kdata, udata); +if (err <0 ) + goto reset_unlock; + +/* The ->init_member() has handled this member */ +if (err >0 ) +continue ; + +/* If st_ops->init_member does not handle it, +* we will only handle func ptrs and zero-ed members +* here. Reject everything else. +*/ + +/* All non func ptr member must be 0 */ +if (!btf_type_resolve_func_ptr (btf_vmlinux, member->type, +NULL )) { +u32 msize; + + mtype =btf_resolve_size (btf_vmlinux, mtype, + &msize,NULL ,NULL ); +if (IS_ERR (mtype)) { + err =PTR_ERR (mtype); + goto reset_unlock; + } + +if (memchr_inv (udata + moff,0 , msize)) { + err = -EINVAL ; + goto reset_unlock; + } + +continue ; + } + + prog_fd = (int )(*(unsigned long *)(udata + moff)); +/* Similar check as the attr->attach_prog_fd */ +if (!prog_fd) +continue ; + + prog =bpf_prog_get (prog_fd); +if (IS_ERR (prog)) { + err =PTR_ERR (prog); + goto reset_unlock; + } + st_map->progs[i] = prog; + +if (prog->type !=BPF_PROG_TYPE_STRUCT_OPS || + prog->aux->attach_btf_id != st_ops->type_id || + prog->expected_attach_type != i) { + err = -EINVAL ; + goto reset_unlock; + } + + err =arch_prepare_bpf_trampoline (image, + &st_ops->func_models[i],0 , + &prog,1 ,NULL ,0 ,NULL ); +if (err <0 ) + goto reset_unlock; + + *(void **)(kdata + moff) = image;
Quoted 8 lines
kernel/bpf/bpf_struct_ops.c const struct bpf_struct_ops *bpf_struct_ops_find(u32 type_id) · line 405
Quoted 5 lines
kernel/bpf/bpf_struct_ops.c +5
-0+ image += err; + +/* put prog_id to udata */ + *(unsigned long *)(udata + moff) = prog->aux->id; + }Should we check whether user indeed provided
modulemember or not before declaring success?
hmm.... By 'module', you meant the "if (ptype == module_type)" logic at the beginning? Yes, it should check user passed 0 also. will fix.
Quoted 50 lines
Quoted 50 lines
kernel/bpf/bpf_struct_ops.c +50
-0+static struct bpf_map *bpf_struct_ops_map_alloc (union bpf_attr *attr) +{ +const struct bpf_struct_ops *st_ops; +size_t map_total_size, st_map_size; +struct bpf_struct_ops_map *st_map; +const struct btf_type *t, *vt; +struct bpf_map_memory mem; +struct bpf_map *map; +int err; + +if (!capable (CAP_SYS_ADMIN )) +return ERR_PTR (-EPERM ); + + st_ops =bpf_struct_ops_find_value (attr->btf_vmlinux_value_type_id); +if (!st_ops) +return ERR_PTR (-ENOTSUPP ); + + vt = st_ops->value_type; +if (attr->value_size != vt->size) +return ERR_PTR (-EINVAL ); + + t = st_ops->type; + + st_map_size =sizeof (*st_map) + +/* kvalue stores the +* struct bpf_struct_ops_tcp_congestions_ops +*/ + (vt->size -sizeof (struct bpf_struct_ops_value )); + map_total_size = st_map_size + +/* uvalue */ +sizeof (vt->size) + +/* struct bpf_progs **progs */ +btf_type_vlen (t) *sizeof (struct bpf_prog *); + err =bpf_map_charge_init (&mem, map_total_size); +if (err <0 ) +return ERR_PTR (err); + + st_map =bpf_map_area_alloc (st_map_size,NUMA_NO_NODE ); +if (!st_map) { +bpf_map_charge_finish (&mem); +return ERR_PTR (-ENOMEM ); + } + st_map->st_ops = st_ops; + map = &st_map->map; + + st_map->uvalue =bpf_map_area_alloc (vt->size,NUMA_NO_NODE ); + st_map->progs = +bpf_map_area_alloc (btf_type_vlen (t) *sizeof (struct bpf_prog *), +NUMA_NO_NODE ); +/* Each trampoline costs < 64 bytes. Ensure one page
Quoted 8 lines
kernel/bpf/bpf_struct_ops.c const struct bpf_struct_ops *bpf_struct_ops_find(u32 type_id) · line 534
Quoted 3 lines
kernel/bpf/bpf_struct_ops.c +3
-0+* is enough for max number of func ptrs. +*/ +BUILD_BUG_ON (PAGE_SIZE /64 <BPF_STRUCT_OPS_MAX_NR_MEMBERS );This maybe true for x86 now, but it may not hold up for future other architectures. Not sure whether we should get the value for arch call backs, or we just bail out during map update if we ever grow exceeds one page.
I will reuse the existing WARN_ON_ONCE() check in arch_prepare_bpf_trampoline(). Need to parameterize the end-of-image (i.e. the current PAGE_SIZE / 2 assumption).
Re: [PATCH bpf-next v2 06/11] bpf: Introduce BPF_MAP_TYPE_STRUCT_OPS
In reply to Martin KaFai LauQuoted 128 lines · Martin KaFai Lau
The patch introduces
BPF_MAP_TYPE_STRUCT_OPS. The map value is a kernel struct with its func ptr implemented in bpf prog. This new map is the interface to register/unregister/introspect a bpf implemented kernel struct.The kernel struct is actually embedded inside another new struct (or called the "value" struct in the code). For example, "struct
tcp_congestion_ops" is embbeded in:struct bpf_struct_ops_tcp_congestion_ops { refcount_t refcnt; enum bpf_struct_ops_state state; struct tcp_congestion_ops data; /* <-- kernel subsystem struct here */ }The map value is "struct
bpf_struct_ops_tcp_congestion_ops". The "bpftool map dump" will then be able to show the
state ("inuse"/"tobefree") and the number of subsystem's refcnt (e.g. number oftcp_sockin thetcp_congestion_opscase). This "value" struct is created automatically by a macro. Having a separate "value" struct will also make extending "structbpf_struct_ops_XYZ" easier (e.g. adding "void (*init)(void)" to "structbpf_struct_ops_XYZ" to do some initialization works before registering thestruct_opsto the kernel subsystem). The libbpf will take care of finding and populating the "structbpf_struct_ops_XYZ" from "struct XYZ".Register a
struct_opsto a kernel subsystem:
1. Load all neededBPF_PROG_TYPE_STRUCT_OPSprog(s)
2. Create aBPF_MAP_TYPE_STRUCT_OPSwithattr->btf_vmlinux_value_type_idset to the btf id "structbpf_struct_ops_tcp_congestion_ops" of the running kernel.
Instead of reusing theattr->btf_value_type_id,btf_vmlinux_value_type_ids added such thatattr->btf_fdcan still be used as the "user" btf which could store other useful sysadmin/debug info that may be introduced in the furture,
e.g. creation-date/compiler-details/map-creator...etc.
3. Create a "structbpf_struct_ops_tcp_congestion_ops" object as described in the running kernel btf. Populate the value of this object. The function ptr should be populated with the prog fds.
4. CallBPF_MAP_UPDATEwith the object created in (3) as
the map value. The key is always "0".During
BPF_MAP_UPDATE, the code that saves the kernel-func-ptr's args as an array of u64 is generated.BPF_MAP_UPDATEalso allows the specificstruct_opsto do some final checks in "st_ops->init_member()" (e.g. ensure all mandatory func ptrs are implemented).
If everything looks good, it will register this kernel struct to the kernel subsystem. The map will not allow further update from this point.Unregister a
struct_opsfrom the kernel subsystem:BPF_MAP_DELETEwith key "0".Introspect a
struct_ops:BPF_MAP_LOOKUP_ELEMwith key "0". The map value returned will have the prog _id_ populated as the func ptr.The map value state (enum
bpf_struct_ops_state) will transit from:
INIT (map created) =>
INUSE (map updated, i.e. reg) =>
TOBEFREE (map value deleted, i.e. unreg)The kernel subsystem needs to call
bpf_struct_ops_get()andbpf_struct_ops_put()to manage the "refcnt" in the
"structbpf_struct_ops_XYZ". This patch uses a separate refcnt for the purose of tracking the subsystem usage. Another approach is to reuse themap->refcntand then "show" (i.e. duringmap_lookup) the subsystem's usage by doingmap->refcnt-map->usercntto filter out the map-fd/pinned-map usage. However, that will also tie down the future semantics ofmap->refcntandmap->usercnt.The very first subsystem's refcnt (during
reg()) holds one count tomap->refcnt. When the very last subsystem's refcnt is gone, it will also release themap->refcnt. Allbpf_progwill be freed when themap->refcntreaches 0 (i.e. duringmap_free()).Here is how the bpftool map command will look like:
[root@arch-fb-vm1 bpf]# bpftool map show
6:struct_opsname dctcp flags0x0
key 4B value 256Bmax_entries1 memlock 4096Bbtf_id6
[root@arch-fb-vm1 bpf]# bpftool map dump id 6[{ "value": { "refcnt": { "refs": { "counter": 1 } }, "state": 1, "data": { "list": { "next": 0, "prev": 0 }, "key": 0, "flags": 2, "init": 24, "release": 0, "ssthresh": 25, "cong_avoid": 30, "set_state": 27, "cwnd_event": 28, "in_ack_event": 26, "undo_cwnd": 29, "pkts_acked": 0, "min_tso_segs": 0, "sndbuf_expand": 0, "cong_control": 0, "get_info": 0, "name": [98,112,102,95,100,99,116,99,112,0,0,0,0,0,0,0 ], "owner": 0 } } }]
Misc Notes:
*bpf_struct_ops_map_sys_lookup_elem()is added for syscall lookup. It does an inplace update on "*value" instead returning a pointer tosyscall.c. Otherwise, it needs a separate copy of "zero" value for theBPF_STRUCT_OPS_STATE_INITto avoid races.* The
bpf_struct_ops_map_delete_elem()is also called withoutpreempt_disable()frommap_delete_elem(). It is because the "->unreg()" may requires sleepable context, e.g. the "tcp_unregister_congestion_control()".
Quoted 5 lines
* "const" is added to some of the existing "struct
btf_func_model*" function arg to avoid a compiler warning caused by this patch.Signed-off-by Martin KaFai Lau <[email protected]>---
LGTM! Few questions below to improve my understanding.
Quoted 13 lines
arch/x86/net/bpf_jit_comp.c | 11 +- include/linux/bpf.h | 49 +++- include/linux/bpf_types.h | 3 + include/linux/btf.h | 13 + include/uapi/linux/bpf.h | 7 +- kernel/bpf/bpf_struct_ops.c | 468 +++++++++++++++++++++++++++++++++++- kernel/bpf/btf.c | 20 +- kernel/bpf/map_in_map.c | 3 +- kernel/bpf/syscall.c | 49 ++-- kernel/bpf/trampoline.c | 5 +- kernel/bpf/verifier.c | 5 + 11 files changed, 593 insertions(+), 40 deletions(-)
[...]
kernel/bpf/bpf_struct_ops.c const struct bpf_struct_ops *bpf_struct_ops_find(u32 type_id) · line 367
Quoted 13 lines
kernel/bpf/bpf_struct_ops.c +13
-0+/* All non func ptr member must be 0 */ +if (!btf_type_resolve_func_ptr (btf_vmlinux, member->type, +NULL )) { +u32 msize; + + mtype =btf_resolve_size (btf_vmlinux, mtype, + &msize,NULL ,NULL ); +if (IS_ERR (mtype)) { + err =PTR_ERR (mtype); + goto reset_unlock; + } + +if (memchr_inv (udata + moff,0 , msize)) {
just double-checking: we are ok with having non-zeroed padding in a struct, is that right?
Quoted 7 lines
kernel/bpf/bpf_struct_ops.c +7
-0+ err = -EINVAL ; + goto reset_unlock; + } + +continue ; + } +
[...]
kernel/bpf/bpf_struct_ops.c const struct bpf_struct_ops *bpf_struct_ops_find(u32 type_id) · line 401
Quoted 9 lines
kernel/bpf/bpf_struct_ops.c +9
-0+ + err =arch_prepare_bpf_trampoline (image, + &st_ops->func_models[i],0 , + &prog,1 ,NULL ,0 ,NULL ); +if (err <0 ) + goto reset_unlock; + + *(void **)(kdata + moff) = image; + image += err;
are there any alignment requirements on image pointer for trampoline?
Quoted 5 lines
kernel/bpf/bpf_struct_ops.c +5
-0+ +/* put prog_id to udata */ + *(unsigned long *)(udata + moff) = prog->aux->id; + } +
[...]
Re: [PATCH bpf-next v2 06/11] bpf: Introduce BPF_MAP_TYPE_STRUCT_OPS
Quoted 164 lines · Andrii Nakryiko
Quoted 128 lines · Martin KaFai Lau
The patch introduces
BPF_MAP_TYPE_STRUCT_OPS. The map value is a kernel struct with its func ptr implemented in bpf prog. This new map is the interface to register/unregister/introspect a bpf implemented kernel struct.The kernel struct is actually embedded inside another new struct (or called the "value" struct in the code). For example, "struct
tcp_congestion_ops" is embbeded in:struct bpf_struct_ops_tcp_congestion_ops { refcount_t refcnt; enum bpf_struct_ops_state state; struct tcp_congestion_ops data; /* <-- kernel subsystem struct here */ }The map value is "struct
bpf_struct_ops_tcp_congestion_ops". The "bpftool map dump" will then be able to show the
state ("inuse"/"tobefree") and the number of subsystem's refcnt (e.g. number oftcp_sockin thetcp_congestion_opscase). This "value" struct is created automatically by a macro. Having a separate "value" struct will also make extending "structbpf_struct_ops_XYZ" easier (e.g. adding "void (*init)(void)" to "structbpf_struct_ops_XYZ" to do some initialization works before registering thestruct_opsto the kernel subsystem). The libbpf will take care of finding and populating the "structbpf_struct_ops_XYZ" from "struct XYZ".Register a
struct_opsto a kernel subsystem:
1. Load all neededBPF_PROG_TYPE_STRUCT_OPSprog(s)
2. Create aBPF_MAP_TYPE_STRUCT_OPSwithattr->btf_vmlinux_value_type_idset to the btf id "structbpf_struct_ops_tcp_congestion_ops" of the running kernel.
Instead of reusing theattr->btf_value_type_id,btf_vmlinux_value_type_ids added such thatattr->btf_fdcan still be used as the "user" btf which could store other useful sysadmin/debug info that may be introduced in the furture,
e.g. creation-date/compiler-details/map-creator...etc.
3. Create a "structbpf_struct_ops_tcp_congestion_ops" object as described in the running kernel btf. Populate the value of this object. The function ptr should be populated with the prog fds.
4. CallBPF_MAP_UPDATEwith the object created in (3) as
the map value. The key is always "0".During
BPF_MAP_UPDATE, the code that saves the kernel-func-ptr's args as an array of u64 is generated.BPF_MAP_UPDATEalso allows the specificstruct_opsto do some final checks in "st_ops->init_member()" (e.g. ensure all mandatory func ptrs are implemented).
If everything looks good, it will register this kernel struct to the kernel subsystem. The map will not allow further update from this point.Unregister a
struct_opsfrom the kernel subsystem:BPF_MAP_DELETEwith key "0".Introspect a
struct_ops:BPF_MAP_LOOKUP_ELEMwith key "0". The map value returned will have the prog _id_ populated as the func ptr.The map value state (enum
bpf_struct_ops_state) will transit from:
INIT (map created) =>
INUSE (map updated, i.e. reg) =>
TOBEFREE (map value deleted, i.e. unreg)The kernel subsystem needs to call
bpf_struct_ops_get()andbpf_struct_ops_put()to manage the "refcnt" in the
"structbpf_struct_ops_XYZ". This patch uses a separate refcnt for the purose of tracking the subsystem usage. Another approach is to reuse themap->refcntand then "show" (i.e. duringmap_lookup) the subsystem's usage by doingmap->refcnt-map->usercntto filter out the map-fd/pinned-map usage. However, that will also tie down the future semantics ofmap->refcntandmap->usercnt.The very first subsystem's refcnt (during
reg()) holds one count tomap->refcnt. When the very last subsystem's refcnt is gone, it will also release themap->refcnt. Allbpf_progwill be freed when themap->refcntreaches 0 (i.e. duringmap_free()).Here is how the bpftool map command will look like:
[root@arch-fb-vm1 bpf]# bpftool map show
6:struct_opsname dctcp flags0x0
key 4B value 256Bmax_entries1 memlock 4096Bbtf_id6
[root@arch-fb-vm1 bpf]# bpftool map dump id 6[{ "value": { "refcnt": { "refs": { "counter": 1 } }, "state": 1, "data": { "list": { "next": 0, "prev": 0 }, "key": 0, "flags": 2, "init": 24, "release": 0, "ssthresh": 25, "cong_avoid": 30, "set_state": 27, "cwnd_event": 28, "in_ack_event": 26, "undo_cwnd": 29, "pkts_acked": 0, "min_tso_segs": 0, "sndbuf_expand": 0, "cong_control": 0, "get_info": 0, "name": [98,112,102,95,100,99,116,99,112,0,0,0,0,0,0,0 ], "owner": 0 } } }]
Misc Notes:
*bpf_struct_ops_map_sys_lookup_elem()is added for syscall lookup. It does an inplace update on "*value" instead returning a pointer tosyscall.c. Otherwise, it needs a separate copy of "zero" value for theBPF_STRUCT_OPS_STATE_INITto avoid races.* The
bpf_struct_ops_map_delete_elem()is also called withoutpreempt_disable()frommap_delete_elem(). It is because the "->unreg()" may requires sleepable context, e.g. the "tcp_unregister_congestion_control()".Quoted 5 lines
* "const" is added to some of the existing "struct
btf_func_model*" function arg to avoid a compiler warning caused by this patch.Signed-off-by Martin KaFai Lau <[email protected]>---
LGTM! Few questions below to improve my understanding.
Acked-by Andrii Nakryiko <[email protected]>Quoted 13 lines
arch/x86/net/bpf_jit_comp.c | 11 +- include/linux/bpf.h | 49 +++- include/linux/bpf_types.h | 3 + include/linux/btf.h | 13 + include/uapi/linux/bpf.h | 7 +- kernel/bpf/bpf_struct_ops.c | 468 +++++++++++++++++++++++++++++++++++- kernel/bpf/btf.c | 20 +- kernel/bpf/map_in_map.c | 3 +- kernel/bpf/syscall.c | 49 ++-- kernel/bpf/trampoline.c | 5 +- kernel/bpf/verifier.c | 5 + 11 files changed, 593 insertions(+), 40 deletions(-)[...]
Quoted 9 lines
kernel/bpf/bpf_struct_ops.c +9
-0+/* All non func ptr member must be 0 */ +if (!btf_type_resolve_func_ptr (btf_vmlinux, member->type, +NULL )) { +u32 msize; + + mtype =btf_resolve_size (btf_vmlinux, mtype, + &msize,NULL ,NULL ); +if (IS_ERR (mtype)) { + err =PTR_ERR (mtype);
Quoted 8 lines
kernel/bpf/bpf_struct_ops.c const struct bpf_struct_ops *bpf_struct_ops_find(u32 type_id) · line 367
Quoted 4 lines
kernel/bpf/bpf_struct_ops.c +4
-0+ goto reset_unlock; + } + +if (memchr_inv (udata + moff,0 , msize)) {just double-checking: we are ok with having non-zeroed padding in a struct, is that right?
Sorry for the delay.
You meant the end-padding of the kernel side struct (i.e. kdata (or kvalue)) could be non-zero? The btf's struct size (i.e. vt->size) should include the padding and the whole vt->size is init to 0.
or you meant the user passed in udata (or uvalue)?
Quoted 11 lines
Quoted 8 lines
kernel/bpf/bpf_struct_ops.c +7
-0+ err = -EINVAL ; + goto reset_unlock; + } + +continue ; + } +[...]
Quoted 11 lines
kernel/bpf/bpf_struct_ops.c const struct bpf_struct_ops *bpf_struct_ops_find(u32 type_id) · line 401
Quoted 9 lines
kernel/bpf/bpf_struct_ops.c +9
-0+ + err =arch_prepare_bpf_trampoline (image, + &st_ops->func_models[i],0 , + &prog,1 ,NULL ,0 ,NULL ); +if (err <0 ) + goto reset_unlock; + + *(void **)(kdata + moff) = image; + image += err;are there any alignment requirements on image pointer for trampoline?
Not that I know of from reading arch_prepare_bpf_trampoline() which also can generate codes to call multiple bpf_prog.
Quoted 8 lines
Quoted 6 lines
kernel/bpf/bpf_struct_ops.c +5
-0+ +/* put prog_id to udata */ + *(unsigned long *)(udata + moff) = prog->aux->id; + } +[...]
Re: [PATCH bpf-next v2 06/11] bpf: Introduce BPF_MAP_TYPE_STRUCT_OPS
Quoted 174 lines · Martin Lau
Quoted 164 lines · Andrii Nakryiko
Quoted 128 lines · Martin KaFai Lau
The patch introduces
BPF_MAP_TYPE_STRUCT_OPS. The map value is a kernel struct with its func ptr implemented in bpf prog. This new map is the interface to register/unregister/introspect a bpf implemented kernel struct.The kernel struct is actually embedded inside another new struct (or called the "value" struct in the code). For example, "struct
tcp_congestion_ops" is embbeded in:struct bpf_struct_ops_tcp_congestion_ops { refcount_t refcnt; enum bpf_struct_ops_state state; struct tcp_congestion_ops data; /* <-- kernel subsystem struct here */ }The map value is "struct
bpf_struct_ops_tcp_congestion_ops". The "bpftool map dump" will then be able to show the
state ("inuse"/"tobefree") and the number of subsystem's refcnt (e.g. number oftcp_sockin thetcp_congestion_opscase). This "value" struct is created automatically by a macro. Having a separate "value" struct will also make extending "structbpf_struct_ops_XYZ" easier (e.g. adding "void (*init)(void)" to "structbpf_struct_ops_XYZ" to do some initialization works before registering thestruct_opsto the kernel subsystem). The libbpf will take care of finding and populating the "structbpf_struct_ops_XYZ" from "struct XYZ".Register a
struct_opsto a kernel subsystem:
1. Load all neededBPF_PROG_TYPE_STRUCT_OPSprog(s)
2. Create aBPF_MAP_TYPE_STRUCT_OPSwithattr->btf_vmlinux_value_type_idset to the btf id "structbpf_struct_ops_tcp_congestion_ops" of the running kernel.
Instead of reusing theattr->btf_value_type_id,btf_vmlinux_value_type_ids added such thatattr->btf_fdcan still be used as the "user" btf which could store other useful sysadmin/debug info that may be introduced in the furture,
e.g. creation-date/compiler-details/map-creator...etc.
3. Create a "structbpf_struct_ops_tcp_congestion_ops" object as described in the running kernel btf. Populate the value of this object. The function ptr should be populated with the prog fds.
4. CallBPF_MAP_UPDATEwith the object created in (3) as
the map value. The key is always "0".During
BPF_MAP_UPDATE, the code that saves the kernel-func-ptr's args as an array of u64 is generated.BPF_MAP_UPDATEalso allows the specificstruct_opsto do some final checks in "st_ops->init_member()" (e.g. ensure all mandatory func ptrs are implemented).
If everything looks good, it will register this kernel struct to the kernel subsystem. The map will not allow further update from this point.Unregister a
struct_opsfrom the kernel subsystem:BPF_MAP_DELETEwith key "0".Introspect a
struct_ops:BPF_MAP_LOOKUP_ELEMwith key "0". The map value returned will have the prog _id_ populated as the func ptr.The map value state (enum
bpf_struct_ops_state) will transit from:
INIT (map created) =>
INUSE (map updated, i.e. reg) =>
TOBEFREE (map value deleted, i.e. unreg)The kernel subsystem needs to call
bpf_struct_ops_get()andbpf_struct_ops_put()to manage the "refcnt" in the
"structbpf_struct_ops_XYZ". This patch uses a separate refcnt for the purose of tracking the subsystem usage. Another approach is to reuse themap->refcntand then "show" (i.e. duringmap_lookup) the subsystem's usage by doingmap->refcnt-map->usercntto filter out the map-fd/pinned-map usage. However, that will also tie down the future semantics ofmap->refcntandmap->usercnt.The very first subsystem's refcnt (during
reg()) holds one count tomap->refcnt. When the very last subsystem's refcnt is gone, it will also release themap->refcnt. Allbpf_progwill be freed when themap->refcntreaches 0 (i.e. duringmap_free()).Here is how the bpftool map command will look like:
[root@arch-fb-vm1 bpf]# bpftool map show
6:struct_opsname dctcp flags0x0
key 4B value 256Bmax_entries1 memlock 4096Bbtf_id6
[root@arch-fb-vm1 bpf]# bpftool map dump id 6[{ "value": { "refcnt": { "refs": { "counter": 1 } }, "state": 1, "data": { "list": { "next": 0, "prev": 0 }, "key": 0, "flags": 2, "init": 24, "release": 0, "ssthresh": 25, "cong_avoid": 30, "set_state": 27, "cwnd_event": 28, "in_ack_event": 26, "undo_cwnd": 29, "pkts_acked": 0, "min_tso_segs": 0, "sndbuf_expand": 0, "cong_control": 0, "get_info": 0, "name": [98,112,102,95,100,99,116,99,112,0,0,0,0,0,0,0 ], "owner": 0 } } }]
Misc Notes:
*bpf_struct_ops_map_sys_lookup_elem()is added for syscall lookup. It does an inplace update on "*value" instead returning a pointer tosyscall.c. Otherwise, it needs a separate copy of "zero" value for theBPF_STRUCT_OPS_STATE_INITto avoid races.* The
bpf_struct_ops_map_delete_elem()is also called withoutpreempt_disable()frommap_delete_elem(). It is because the "->unreg()" may requires sleepable context, e.g. the "tcp_unregister_congestion_control()".Quoted 5 lines
* "const" is added to some of the existing "struct
btf_func_model*" function arg to avoid a compiler warning caused by this patch.Signed-off-by Martin KaFai Lau <[email protected]>---
LGTM! Few questions below to improve my understanding.
Acked-by Andrii Nakryiko <[email protected]>Quoted 13 lines
arch/x86/net/bpf_jit_comp.c | 11 +- include/linux/bpf.h | 49 +++- include/linux/bpf_types.h | 3 + include/linux/btf.h | 13 + include/uapi/linux/bpf.h | 7 +- kernel/bpf/bpf_struct_ops.c | 468 +++++++++++++++++++++++++++++++++++- kernel/bpf/btf.c | 20 +- kernel/bpf/map_in_map.c | 3 +- kernel/bpf/syscall.c | 49 ++-- kernel/bpf/trampoline.c | 5 +- kernel/bpf/verifier.c | 5 + 11 files changed, 593 insertions(+), 40 deletions(-)[...]
Quoted 9 lines
kernel/bpf/bpf_struct_ops.c +9
-0+/* All non func ptr member must be 0 */ +if (!btf_type_resolve_func_ptr (btf_vmlinux, member->type, +NULL )) { +u32 msize; + + mtype =btf_resolve_size (btf_vmlinux, mtype, + &msize,NULL ,NULL ); +if (IS_ERR (mtype)) { + err =PTR_ERR (mtype);Quoted 8 lines
kernel/bpf/bpf_struct_ops.c const struct bpf_struct_ops *bpf_struct_ops_find(u32 type_id) · line 367
Quoted 4 lines
kernel/bpf/bpf_struct_ops.c +4
-0+ goto reset_unlock; + } + +if (memchr_inv (udata + moff,0 , msize)) {just double-checking: we are ok with having non-zeroed padding in a struct, is that right?
Quoted 7 lines
Sorry for the delay.
You meant the end-padding of the kernel side struct (i.e. kdata (or kvalue)) could be non-zero? The btf's struct size (i.e.
vt->size) should include the padding and the wholevt->sizeis init to 0.or you meant the user passed in udata (or uvalue)?
The latter, udata. You check member-by-member, but if there is padding between fields or at the end of a struct, nothing is currently checking it for zeroes. So probably safer to check those paddings inbetween as well.
Quoted 23 lines
Quoted 12 lines
Quoted 9 lines
kernel/bpf/bpf_struct_ops.c +7
-0+ err = -EINVAL ; + goto reset_unlock; + } + +continue ; + } +[...]
Quoted 11 lines
kernel/bpf/bpf_struct_ops.c const struct bpf_struct_ops *bpf_struct_ops_find(u32 type_id) · line 401
Quoted 9 lines
kernel/bpf/bpf_struct_ops.c +9
-0+ + err =arch_prepare_bpf_trampoline (image, + &st_ops->func_models[i],0 , + &prog,1 ,NULL ,0 ,NULL ); +if (err <0 ) + goto reset_unlock; + + *(void **)(kdata + moff) = image; + image += err;are there any alignment requirements on image pointer for trampoline?
Quoted 2 lines
Not that I know of from reading
arch_prepare_bpf_trampoline()which also can generate codes to call multiplebpf_prog.
Yeah, I think you are right, at least for x86. Variable-sized x86 instructions pretty much rule out any alignment. It might not be the case for fixed-sized instructions architectures, but we should cross that bridge when we get to it.
Quoted 9 lines
Quoted 9 lines
Quoted 7 lines
kernel/bpf/bpf_struct_ops.c +5
-0+ +/* put prog_id to udata */ + *(unsigned long *)(udata + moff) = prog->aux->id; + } +[...]
Re: [PATCH bpf-next v2 06/11] bpf: Introduce BPF_MAP_TYPE_STRUCT_OPS
Quoted 181 lines · Andrii Nakryiko
Quoted 180 lines · Martin Lau
Quoted 164 lines · Andrii Nakryiko
Quoted 128 lines · Martin KaFai Lau
The patch introduces
BPF_MAP_TYPE_STRUCT_OPS. The map value is a kernel struct with its func ptr implemented in bpf prog. This new map is the interface to register/unregister/introspect a bpf implemented kernel struct.The kernel struct is actually embedded inside another new struct (or called the "value" struct in the code). For example, "struct
tcp_congestion_ops" is embbeded in:struct bpf_struct_ops_tcp_congestion_ops { refcount_t refcnt; enum bpf_struct_ops_state state; struct tcp_congestion_ops data; /* <-- kernel subsystem struct here */ }The map value is "struct
bpf_struct_ops_tcp_congestion_ops". The "bpftool map dump" will then be able to show the
state ("inuse"/"tobefree") and the number of subsystem's refcnt (e.g. number oftcp_sockin thetcp_congestion_opscase). This "value" struct is created automatically by a macro. Having a separate "value" struct will also make extending "structbpf_struct_ops_XYZ" easier (e.g. adding "void (*init)(void)" to "structbpf_struct_ops_XYZ" to do some initialization works before registering thestruct_opsto the kernel subsystem). The libbpf will take care of finding and populating the "structbpf_struct_ops_XYZ" from "struct XYZ".Register a
struct_opsto a kernel subsystem:
1. Load all neededBPF_PROG_TYPE_STRUCT_OPSprog(s)
2. Create aBPF_MAP_TYPE_STRUCT_OPSwithattr->btf_vmlinux_value_type_idset to the btf id "structbpf_struct_ops_tcp_congestion_ops" of the running kernel.
Instead of reusing theattr->btf_value_type_id,btf_vmlinux_value_type_ids added such thatattr->btf_fdcan still be used as the "user" btf which could store other useful sysadmin/debug info that may be introduced in the furture,
e.g. creation-date/compiler-details/map-creator...etc.
3. Create a "structbpf_struct_ops_tcp_congestion_ops" object as described in the running kernel btf. Populate the value of this object. The function ptr should be populated with the prog fds.
4. CallBPF_MAP_UPDATEwith the object created in (3) as
the map value. The key is always "0".During
BPF_MAP_UPDATE, the code that saves the kernel-func-ptr's args as an array of u64 is generated.BPF_MAP_UPDATEalso allows the specificstruct_opsto do some final checks in "st_ops->init_member()" (e.g. ensure all mandatory func ptrs are implemented).
If everything looks good, it will register this kernel struct to the kernel subsystem. The map will not allow further update from this point.Unregister a
struct_opsfrom the kernel subsystem:BPF_MAP_DELETEwith key "0".Introspect a
struct_ops:BPF_MAP_LOOKUP_ELEMwith key "0". The map value returned will have the prog _id_ populated as the func ptr.The map value state (enum
bpf_struct_ops_state) will transit from:
INIT (map created) =>
INUSE (map updated, i.e. reg) =>
TOBEFREE (map value deleted, i.e. unreg)The kernel subsystem needs to call
bpf_struct_ops_get()andbpf_struct_ops_put()to manage the "refcnt" in the
"structbpf_struct_ops_XYZ". This patch uses a separate refcnt for the purose of tracking the subsystem usage. Another approach is to reuse themap->refcntand then "show" (i.e. duringmap_lookup) the subsystem's usage by doingmap->refcnt-map->usercntto filter out the map-fd/pinned-map usage. However, that will also tie down the future semantics ofmap->refcntandmap->usercnt.The very first subsystem's refcnt (during
reg()) holds one count tomap->refcnt. When the very last subsystem's refcnt is gone, it will also release themap->refcnt. Allbpf_progwill be freed when themap->refcntreaches 0 (i.e. duringmap_free()).Here is how the bpftool map command will look like:
[root@arch-fb-vm1 bpf]# bpftool map show
6:struct_opsname dctcp flags0x0
key 4B value 256Bmax_entries1 memlock 4096Bbtf_id6
[root@arch-fb-vm1 bpf]# bpftool map dump id 6[{ "value": { "refcnt": { "refs": { "counter": 1 } }, "state": 1, "data": { "list": { "next": 0, "prev": 0 }, "key": 0, "flags": 2, "init": 24, "release": 0, "ssthresh": 25, "cong_avoid": 30, "set_state": 27, "cwnd_event": 28, "in_ack_event": 26, "undo_cwnd": 29, "pkts_acked": 0, "min_tso_segs": 0, "sndbuf_expand": 0, "cong_control": 0, "get_info": 0, "name": [98,112,102,95,100,99,116,99,112,0,0,0,0,0,0,0 ], "owner": 0 } } }]
Misc Notes:
*bpf_struct_ops_map_sys_lookup_elem()is added for syscall lookup. It does an inplace update on "*value" instead returning a pointer tosyscall.c. Otherwise, it needs a separate copy of "zero" value for theBPF_STRUCT_OPS_STATE_INITto avoid races.* The
bpf_struct_ops_map_delete_elem()is also called withoutpreempt_disable()frommap_delete_elem(). It is because the "->unreg()" may requires sleepable context, e.g. the "tcp_unregister_congestion_control()".Quoted 5 lines
* "const" is added to some of the existing "struct
btf_func_model*" function arg to avoid a compiler warning caused by this patch.Signed-off-by Martin KaFai Lau <[email protected]>---
LGTM! Few questions below to improve my understanding.
Acked-by Andrii Nakryiko <[email protected]>Quoted 13 lines
arch/x86/net/bpf_jit_comp.c | 11 +- include/linux/bpf.h | 49 +++- include/linux/bpf_types.h | 3 + include/linux/btf.h | 13 + include/uapi/linux/bpf.h | 7 +- kernel/bpf/bpf_struct_ops.c | 468 +++++++++++++++++++++++++++++++++++- kernel/bpf/btf.c | 20 +- kernel/bpf/map_in_map.c | 3 +- kernel/bpf/syscall.c | 49 ++-- kernel/bpf/trampoline.c | 5 +- kernel/bpf/verifier.c | 5 + 11 files changed, 593 insertions(+), 40 deletions(-)[...]
Quoted 9 lines
kernel/bpf/bpf_struct_ops.c +9
-0+/* All non func ptr member must be 0 */ +if (!btf_type_resolve_func_ptr (btf_vmlinux, member->type, +NULL )) { +u32 msize; + + mtype =btf_resolve_size (btf_vmlinux, mtype, + &msize,NULL ,NULL ); +if (IS_ERR (mtype)) { + err =PTR_ERR (mtype);Quoted 8 lines
kernel/bpf/bpf_struct_ops.c const struct bpf_struct_ops *bpf_struct_ops_find(u32 type_id) · line 367
Quoted 4 lines
kernel/bpf/bpf_struct_ops.c +4
-0+ goto reset_unlock; + } + +if (memchr_inv (udata + moff,0 , msize)) {just double-checking: we are ok with having non-zeroed padding in a struct, is that right?
Sorry for the delay.
You meant the end-padding of the kernel side struct (i.e. kdata (or kvalue)) could be non-zero? The btf's struct size (i.e.
vt->size) should include the padding and the wholevt->sizeis init to 0.
Quoted 6 lines
Quoted 1 line
or you meant the user passed in udata (or uvalue)?
The latter, udata. You check member-by-member, but if there is padding between fields or at the end of a struct, nothing is currently checking it for zeroes. So probably safer to check those paddings inbetween as well.
Agree. Will do.
kbuild test robot
build botRe: [PATCH bpf-next v2 06/11] bpf: Introduce BPF_MAP_TYPE_STRUCT_OPS
In reply to Martin KaFai LauI love your patch! Yet something to improve:
Hi Martin,
I love your patch! Yet something to improve:
[auto build test ERROR on bpf-next/master]
[cannot apply to bpf/master net/master v5.5-rc3 next-20191219]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system. BTW, we also suggest to use '--base' option to specify the base tree in git format-patch, please see https://stackoverflow.com/a/37406982]
url: https://github.com/0day-ci/linux/commits/Martin-KaFai-Lau/Introduce-BPF-STRUCT_OPS/20191224-085617 base: https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master config: arm64-defconfig (attached as .config) compiler: aarch64-linux-gcc (GCC) 7.5.0
reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
GCC_VERSION=7.5.0 make.cross ARCH=arm64
If you fix the issue, kindly add following tag
All error/warnings (new ones prefixed by >>):
kernel/bpf/bpf_struct_ops.c: In function 'bpf_struct_ops_init':
kernel/bpf/bpf_struct_ops.c:176:8: error: implicit declaration of function 'btf_distill_func_proto'; did you mean 'btf_type_is_func_proto'? [-Werror=implicit-function-declaration]
btf_distill_func_proto(&log, _btf_vmlinux,
^~~~~~~~~~~~~~~~~~~~~~
btf_type_is_func_proto
kernel/bpf/bpf_struct_ops.c: In function 'bpf_struct_ops_map_update_elem':
>> kernel/bpf/bpf_struct_ops.c:408:2: error: implicit declaration of function 'bpf_map_inc'; did you mean 'bpf_map_put'? [-Werror=implicit-function-declaration]
bpf_map_inc(map);
^~~~~~~~~~~
bpf_map_put
kernel/bpf/bpf_struct_ops.c: In function 'bpf_struct_ops_map_free':
>> kernel/bpf/bpf_struct_ops.c:468:2: error: implicit declaration of function 'bpf_map_area_free'; did you mean 'bpf_prog_free'? [-Werror=implicit-function-declaration]
bpf_map_area_free(st_map->progs);
^~~~~~~~~~~~~~~~~
bpf_prog_free
kernel/bpf/bpf_struct_ops.c: In function 'bpf_struct_ops_map_alloc':
kernel/bpf/bpf_struct_ops.c:515:8: error: implicit declaration of function 'bpf_map_charge_init'; did you mean 'bpf_prog_change_xdp'? [-Werror=implicit-function-declaration]
err = bpf_map_charge_init(&mem, map_total_size);
^~~~~~~~~~~~~~~~~~~
bpf_prog_change_xdp
>> kernel/bpf/bpf_struct_ops.c:519:11: error: implicit declaration of function 'bpf_map_area_alloc'; did you mean 'bpf_prog_alloc'? [-Werror=implicit-function-declaration]
st_map = bpf_map_area_alloc(st_map_size, NUMA_NO_NODE);
^~~~~~~~~~~~~~~~~~
bpf_prog_alloc
>> kernel/bpf/bpf_struct_ops.c:519:9: warning: assignment makes pointer from integer without a cast [-Wint-conversion]
st_map = bpf_map_area_alloc(st_map_size, NUMA_NO_NODE);
^
>> kernel/bpf/bpf_struct_ops.c:521:3: error: implicit declaration of function 'bpf_map_charge_finish'; did you mean 'bpf_map_flags_to_cap'? [-Werror=implicit-function-declaration]
bpf_map_charge_finish(&mem);
^~~~~~~~~~~~~~~~~~~~~
bpf_map_flags_to_cap
kernel/bpf/bpf_struct_ops.c:527:17: warning: assignment makes pointer from integer without a cast [-Wint-conversion]
st_map->uvalue = bpf_map_area_alloc(vt->size, NUMA_NO_NODE);
^
kernel/bpf/bpf_struct_ops.c:528:16: warning: assignment makes pointer from integer without a cast [-Wint-conversion]
st_map->progs =
^
kernel/bpf/bpf_struct_ops.c:545:2: error: implicit declaration of function 'bpf_map_init_from_attr'; did you mean 'bpf_jit_get_func_addr'? [-Werror=implicit-function-declaration]
bpf_map_init_from_attr(map, attr);
^~~~~~~~~~~~~~~~~~~~~~
bpf_jit_get_func_addr
kernel/bpf/bpf_struct_ops.c:546:2: error: implicit declaration of function 'bpf_map_charge_move'; did you mean 'bpf_prog_change_xdp'? [-Werror=implicit-function-declaration]
bpf_map_charge_move(&map->memory, &mem);
^~~~~~~~~~~~~~~~~~~
bpf_prog_change_xdpcc1: some warnings being treated as errors
vim +408 kernel/bpf/bpf_struct_ops.c
289
290 static int bpf_struct_ops_map_update_elem(struct bpf_map *map, void *key,
291 void *value, u64 flags)
292 {
293 struct bpf_struct_ops_map *st_map = (struct bpf_struct_ops_map *)map;
294 const struct bpf_struct_ops *st_ops = st_map->st_ops;
295 struct bpf_struct_ops_value *uvalue, *kvalue;
296 const struct btf_member *member;
297 const struct btf_type *t = st_ops->type;
298 void *udata, *kdata;
299 int prog_fd, err = 0;
300 void *image;
301 u32 i;
302
303 if (flags)
304 return -EINVAL;
305
306 if (*(u32 *)key != 0)
307 return -E2BIG;
308
309 uvalue = (struct bpf_struct_ops_value *)value;
310 if (uvalue->state || refcount_read(&uvalue->refcnt))
311 return -EINVAL;
312
313 uvalue = (struct bpf_struct_ops_value *)st_map->uvalue;
314 kvalue = (struct bpf_struct_ops_value *)&st_map->kvalue;
315
316 spin_lock(&st_map->lock);
317
318 if (kvalue->state != BPF_STRUCT_OPS_STATE_INIT) {
319 err = -EBUSY;
320 goto unlock;
321 }
322
323 memcpy(uvalue, value, map->value_size);
324
325 udata = &uvalue->data;
326 kdata = &kvalue->data;
327 image = st_map->image;
328
329 for_each_member(i, t, member) {
330 const struct btf_type *mtype, *ptype;
331 struct bpf_prog *prog;
332 u32 moff;
333
334 moff = btf_member_bit_offset(t, member) / 8;
335 mtype = btf_type_by_id(btf_vmlinux, member->type);
336 ptype = btf_type_resolve_ptr(btf_vmlinux, member->type, NULL);
337 if (ptype == module_type) {
338 *(void **)(kdata + moff) = BPF_MODULE_OWNER;
339 continue;
340 }
341
342 err = st_ops->init_member(t, member, kdata, udata);
343 if (err < 0)
344 goto reset_unlock;
345
346 /* The ->init_member() has handled this member */
347 if (err > 0)
348 continue;
349
350 /* If st_ops->init_member does not handle it,
351 * we will only handle func ptrs and zero-ed members
352 * here. Reject everything else.
353 */
354
355 /* All non func ptr member must be 0 */
356 if (!btf_type_resolve_func_ptr(btf_vmlinux, member->type,
357 NULL)) {
358 u32 msize;
359
360 mtype = btf_resolve_size(btf_vmlinux, mtype,
361 &msize, NULL, NULL);
362 if (IS_ERR(mtype)) {
363 err = PTR_ERR(mtype);
364 goto reset_unlock;
365 }
366
367 if (memchr_inv(udata + moff, 0, msize)) {
368 err = -EINVAL;
369 goto reset_unlock;
370 }
371
372 continue;
373 }
374
375 prog_fd = (int)(*(unsigned long *)(udata + moff));
376 /* Similar check as the attr->attach_prog_fd */
377 if (!prog_fd)
378 continue;
379
380 prog = bpf_prog_get(prog_fd);
381 if (IS_ERR(prog)) {
382 err = PTR_ERR(prog);
383 goto reset_unlock;
384 }
385 st_map->progs[i] = prog;
386
387 if (prog->type != BPF_PROG_TYPE_STRUCT_OPS ||
388 prog->aux->attach_btf_id != st_ops->type_id ||
389 prog->expected_attach_type != i) {
390 err = -EINVAL;
391 goto reset_unlock;
392 }
393
394 err = arch_prepare_bpf_trampoline(image,
395 &st_ops->func_models[i], 0,
396 &prog, 1, NULL, 0, NULL);
397 if (err < 0)
398 goto reset_unlock;
399
400 *(void **)(kdata + moff) = image;
401 image += err;
402
403 /* put prog_id to udata */
404 *(unsigned long *)(udata + moff) = prog->aux->id;
405 }
406
407 refcount_set(&kvalue->refcnt, 1);
> 408 bpf_map_inc(map);
409
410 err = st_ops->reg(kdata);
411 if (!err) {
412 /* Pair with smp_load_acquire() during lookup */
413 smp_store_release(&kvalue->state, BPF_STRUCT_OPS_STATE_INUSE);
414 goto unlock;
415 }
416
417 /* Error during st_ops->reg() */
418 bpf_map_put(map);
419
420 reset_unlock:
421 bpf_struct_ops_map_put_progs(st_map);
422 memset(uvalue, 0, map->value_size);
423 memset(kvalue, 0, map->value_size);
424
425 unlock:
426 spin_unlock(&st_map->lock);
427 return err;
428 }
429
430 static int bpf_struct_ops_map_delete_elem(struct bpf_map *map, void *key)
431 {
432 enum bpf_struct_ops_state prev_state;
433 struct bpf_struct_ops_map *st_map;
434
435 st_map = (struct bpf_struct_ops_map *)map;
436 prev_state = cmpxchg(&st_map->kvalue.state,
437 BPF_STRUCT_OPS_STATE_INUSE,
438 BPF_STRUCT_OPS_STATE_TOBEFREE);
439 if (prev_state == BPF_STRUCT_OPS_STATE_INUSE) {
440 st_map->st_ops->unreg(&st_map->kvalue.data);
441 if (refcount_dec_and_test(&st_map->kvalue.refcnt))
442 bpf_map_put(map);
443 }
444
445 return 0;
446 }
447
448 static void bpf_struct_ops_map_seq_show_elem(struct bpf_map *map, void *key,
449 struct seq_file *m)
450 {
451 void *value;
452
453 value = bpf_struct_ops_map_lookup_elem(map, key);
454 if (!value)
455 return;
456
457 btf_type_seq_show(btf_vmlinux, map->btf_vmlinux_value_type_id,
458 value, m);
459 seq_puts(m, "\n");
460 }
461
462 static void bpf_struct_ops_map_free(struct bpf_map *map)
463 {
464 struct bpf_struct_ops_map *st_map = (struct bpf_struct_ops_map *)map;
465
466 if (st_map->progs)
467 bpf_struct_ops_map_put_progs(st_map);
> 468 bpf_map_area_free(st_map->progs);
469 bpf_jit_free_exec(st_map->image);
470 bpf_map_area_free(st_map->uvalue);
471 bpf_map_area_free(st_map);
472 }
473
474 static int bpf_struct_ops_map_alloc_check(union bpf_attr *attr)
475 {
476 if (attr->key_size != sizeof(unsigned int) || attr->max_entries != 1 ||
477 attr->map_flags || !attr->btf_vmlinux_value_type_id)
478 return -EINVAL;
479 return 0;
480 }
481
482 static struct bpf_map *bpf_struct_ops_map_alloc(union bpf_attr *attr)
483 {
484 const struct bpf_struct_ops *st_ops;
485 size_t map_total_size, st_map_size;
486 struct bpf_struct_ops_map *st_map;
487 const struct btf_type *t, *vt;
488 struct bpf_map_memory mem;
489 struct bpf_map *map;
490 int err;
491
492 if (!capable(CAP_SYS_ADMIN))
493 return ERR_PTR(-EPERM);
494
495 st_ops = bpf_struct_ops_find_value(attr->btf_vmlinux_value_type_id);
496 if (!st_ops)
497 return ERR_PTR(-ENOTSUPP);
498
499 vt = st_ops->value_type;
500 if (attr->value_size != vt->size)
501 return ERR_PTR(-EINVAL);
502
503 t = st_ops->type;
504
505 st_map_size = sizeof(*st_map) +
506 /* kvalue stores the
507 * struct bpf_struct_ops_tcp_congestions_ops
508 */
509 (vt->size - sizeof(struct bpf_struct_ops_value));
510 map_total_size = st_map_size +
511 /* uvalue */
512 sizeof(vt->size) +
513 /* struct bpf_progs **progs */
514 btf_type_vlen(t) * sizeof(struct bpf_prog *);
> 515 err = bpf_map_charge_init(&mem, map_total_size);
516 if (err < 0)
517 return ERR_PTR(err);
518
> 519 st_map = bpf_map_area_alloc(st_map_size, NUMA_NO_NODE);
520 if (!st_map) {
> 521 bpf_map_charge_finish(&mem); 522 return ERR_PTR(-ENOMEM);
523 }
524 st_map->st_ops = st_ops;
525 map = &st_map->map;
526
527 st_map->uvalue = bpf_map_area_alloc(vt->size, NUMA_NO_NODE);
528 st_map->progs =
529 bpf_map_area_alloc(btf_type_vlen(t) * sizeof(struct bpf_prog *),
530 NUMA_NO_NODE);
531 /* Each trampoline costs < 64 bytes. Ensure one page
532 * is enough for max number of func ptrs.
533 */
534 BUILD_BUG_ON(PAGE_SIZE / 64 < BPF_STRUCT_OPS_MAX_NR_MEMBERS);
535 st_map->image = bpf_jit_alloc_exec(PAGE_SIZE);
536 if (!st_map->uvalue || !st_map->progs || !st_map->image) {
537 bpf_struct_ops_map_free(map);
538 bpf_map_charge_finish(&mem);
539 return ERR_PTR(-ENOMEM);
540 }
541
542 spin_lock_init(&st_map->lock);
543 set_vm_flush_reset_perms(st_map->image);
544 set_memory_x((long)st_map->image, 1);
545 bpf_map_init_from_attr(map, attr);
546 bpf_map_charge_move(&map->memory, &mem);
547
548 return map;
549 }
550
---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org Intel Corporation
PATCH v2 7/11 bpf: tcp: Support tcp_congestion_ops in bpf
In reply to Martin KaFai LauThis patch makes "struct tcp_congestion_ops" to be the first user of BPF STRUCT_OPS. It allows implementing a tcp_congestion_ops in bpf.
The BPF implemented tcp_congestion_ops can be used like regular kernel tcp-cc through sysctl and setsockopt. e.g. [root@arch-fb-vm1 bpf]# sysctl -a | egrep congestion net.ipv4.tcp_allowed_congestion_control = reno cubic bpf_cubic net.ipv4.tcp_available_congestion_control = reno bic cubic bpf_cubic net.ipv4.tcp_congestion_control = bpf_cubic
There has been attempt to move the TCP CC to the user space
(e.g. CCP in TCP). The common arguments are faster turn around,
get away from long-tail kernel versions in production...etc, which are legit points.
BPF has been the continuous effort to join both kernel and userspace upsides together (e.g. XDP to gain the performance advantage without bypassing the kernel). The recent BPF advancements (in particular BTF-aware verifier, BPF trampoline, BPF CO-RE...) made implementing kernel struct ops (e.g. tcp cc) possible in BPF. It allows a faster turnaround for testing algorithm in the production while leveraging the existing (and continue growing) BPF feature/framework instead of building one specifically for userspace TCP CC.
This patch allows write access to a few fields in tcp-sock (in bpf_tcp_ca_btf_struct_access()).
The optional "get_info" is unsupported now. It can be added later. One possible way is to output the info with a btf-id to describe the content.
--- include/linux/filter.h | 2 + include/net/tcp.h | 1 + kernel/bpf/bpf_struct_ops_types.h | 7 +- net/core/filter.c | 2 +- net/ipv4/Makefile | 4 + net/ipv4/bpf_tcp_ca.c | 226 ++++++++++++++++++++++++++++++ net/ipv4/tcp_cong.c | 14 +- net/ipv4/tcp_ipv4.c | 6 +- net/ipv4/tcp_minisocks.c | 4 +- net/ipv4/tcp_output.c | 4 +- 10 files changed, 255 insertions(+), 15 deletions(-) create mode 100644 net/ipv4/bpf_tcp_ca.c
include/linux/filter.h +2 -0
@@ -843,6 +843,8 @@ int bpf_prog_create(struct bpf_prog **pfp, struct sock_fprog_kern *fprog);int bpf_prog_create_from_user (struct bpf_prog **pfp,struct sock_fprog *fprog,bpf_aux_classic_check_t trans,bool save_orig);void bpf_prog_destroy (struct bpf_prog *fp); +const struct bpf_func_proto * +bpf_base_func_proto (enum bpf_func_id func_id);int sk_attach_filter (struct sock_fprog *fprog,struct sock *sk);int sk_attach_bpf (u32 ufd,struct sock *sk);
include/net/tcp.h +1 -0
@@ -1007,6 +1007,7 @@ enum tcp_ca_ack_event_flags {#define TCP_CONG_NON_RESTRICTED 0x1/* Requires ECN/ECT set on all packets */ #define TCP_CONG_NEEDS_ECN 0x2 +#define TCP_CONG_MASK (TCP_CONG_NON_RESTRICTED | TCP_CONG_NEEDS_ECN)union tcp_cc_info ;
kernel/bpf/bpf_struct_ops_types.h +6 -1
@@ -1,4 +1,9 @@/* SPDX-License-Identifier: GPL-2.0 */ /* internal file - do not include directly */ -/* To be filled in a later patch */ +#ifdef CONFIG_BPF_JIT +#ifdef CONFIG_INET +#include <net/tcp.h> +BPF_STRUCT_OPS_TYPE(tcp_congestion_ops) +#endif +#endif
net/core/filter.c +1 -1
@@ -5934,7 +5934,7 @@ bool bpf_helper_changes_pkt_data(void *func)return false; } -static const struct bpf_func_proto * +const struct bpf_func_proto *bpf_base_func_proto (enum bpf_func_id func_id) {switch (func_id) {
net/ipv4/Makefile +4 -0
@@ -65,3 +65,7 @@ obj-$(CONFIG_NETLABEL) += cipso_ipv4.o obj-$(CONFIG_XFRM) += xfrm4_policy.o xfrm4_state.o xfrm4_input.o \ xfrm4_output.o xfrm4_protocol.o + +ifeq ($(CONFIG_BPF_SYSCALL),y) +obj-$(CONFIG_BPF_JIT) += bpf_tcp_ca.o +endif
net/ipv4/bpf_tcp_ca.c new file +226 -0
226 lines not on this page · raw message
net/ipv4/tcp_cong.c +7 -7
@@ -162,7 +162,7 @@ void tcp_assign_congestion_control(struct sock *sk)rcu_read_lock (); ca =rcu_dereference (net->ipv4.tcp_congestion_control); -if (unlikely (!try_module_get (ca->owner))) +if (unlikely (!bpf_try_module_get (ca, ca->owner))) ca = &tcp_reno; icsk->icsk_ca_ops = ca;rcu_read_unlock ();
@@ -208,7 +208,7 @@ void tcp_cleanup_congestion_control(struct sock *sk)if (icsk->icsk_ca_ops->release) icsk->icsk_ca_ops->release (sk); -module_put (icsk->icsk_ca_ops->owner); +bpf_module_put (icsk->icsk_ca_ops, icsk->icsk_ca_ops->owner); }/* Used by sysctl to change default congestion control */
@@ -222,12 +222,12 @@ int tcp_set_default_congestion_control(struct net *net, const char *name) ca =tcp_ca_find_autoload (net, name);if (!ca) { ret = -ENOENT ; - }else if (!try_module_get (ca->owner)) { + }else if (!bpf_try_module_get (ca, ca->owner)) { ret = -EBUSY ; }else { prev =xchg (&net->ipv4.tcp_congestion_control, ca);if (prev) -module_put (prev->owner); +bpf_module_put (prev, prev->owner); ca->flags |=TCP_CONG_NON_RESTRICTED ; ret =0 ;
@@ -366,19 +366,19 @@ int tcp_set_congestion_control(struct sock *sk, const char *name, bool load, }else if (!load) {const struct tcp_congestion_ops *old_ca = icsk->icsk_ca_ops; -if (try_module_get (ca->owner)) { +if (bpf_try_module_get (ca, ca->owner)) {if (reinit) {tcp_reinit_congestion_control (sk, ca); }else { icsk->icsk_ca_ops = ca; -module_put (old_ca->owner); +bpf_module_put (old_ca, old_ca->owner); } }else { err = -EBUSY ; } }else if (!((ca->flags &TCP_CONG_NON_RESTRICTED ) || cap_net_admin)) { err = -EPERM ; - }else if (!try_module_get (ca->owner)) { + }else if (!bpf_try_module_get (ca, ca->owner)) { err = -EBUSY ; }else {tcp_reinit_congestion_control (sk, ca);
net/ipv4/tcp_ipv4.c +4 -2
@@ -2619,7 +2619,8 @@ static void __net_exit tcp_sk_exit(struct net *net)int cpu;if (net->ipv4.tcp_congestion_control) -module_put (net->ipv4.tcp_congestion_control->owner); +bpf_module_put (net->ipv4.tcp_congestion_control, + net->ipv4.tcp_congestion_control->owner);for_each_possible_cpu (cpu)inet_ctl_sock_destroy (*per_cpu_ptr (net->ipv4.tcp_sk, cpu));
@@ -2726,7 +2727,8 @@ static int __net_init tcp_sk_init(struct net *net)/* Reno is always built in */ if (!net_eq (net, &init_net) && -try_module_get (init_net.ipv4.tcp_congestion_control->owner)) +bpf_try_module_get (init_net.ipv4.tcp_congestion_control, + init_net.ipv4.tcp_congestion_control->owner)) net->ipv4.tcp_congestion_control = init_net.ipv4.tcp_congestion_control;else net->ipv4.tcp_congestion_control = &tcp_reno;
net/ipv4/tcp_minisocks.c +2 -2
@@ -414,7 +414,7 @@ void tcp_ca_openreq_child(struct sock *sk, const struct dst_entry *dst)rcu_read_lock (); ca =tcp_ca_find_key (ca_key); -if (likely (ca &&try_module_get (ca->owner))) { +if (likely (ca &&bpf_try_module_get (ca, ca->owner))) { icsk->icsk_ca_dst_locked =tcp_ca_dst_locked (dst); icsk->icsk_ca_ops = ca; ca_got_dst = true;
@@ -425,7 +425,7 @@ void tcp_ca_openreq_child(struct sock *sk, const struct dst_entry *dst)/* If no valid choice made yet, assign current system default ca. */ if (!ca_got_dst && (!icsk->icsk_ca_setsockopt || - !try_module_get (icsk->icsk_ca_ops->owner))) + !bpf_try_module_get (icsk->icsk_ca_ops, icsk->icsk_ca_ops->owner)))tcp_assign_congestion_control (sk);tcp_set_ca_state (sk, TCP_CA_Open);
net/ipv4/tcp_output.c +2 -2
@@ -3356,8 +3356,8 @@ static void tcp_ca_dst_init(struct sock *sk, const struct dst_entry *dst)rcu_read_lock (); ca =tcp_ca_find_key (ca_key); -if (likely (ca &&try_module_get (ca->owner))) { -module_put (icsk->icsk_ca_ops->owner); +if (likely (ca &&bpf_try_module_get (ca, ca->owner))) { +bpf_module_put (icsk->icsk_ca_ops, icsk->icsk_ca_ops->owner); icsk->icsk_ca_dst_locked =tcp_ca_dst_locked (dst); icsk->icsk_ca_ops = ca; }
Signature · 1 line
2.17.1
Re: [PATCH bpf-next v2 07/11] bpf: tcp: Support tcp_congestion_ops in bpf
Quoted 26 lines · Martin KaFai Lau
This patch makes "struct
tcp_congestion_ops" to be the first user of BPFSTRUCT_OPS. It allows implementing atcp_congestion_opsin bpf.The BPF implemented
tcp_congestion_opscan be used like regular kernel tcp-cc through sysctl and setsockopt. e.g. [root@arch-fb-vm1 bpf]# sysctl -a | egrep congestion net.ipv4.tcp_allowed_congestion_control = reno cubicbpf_cubicnet.ipv4.tcp_available_congestion_control = reno bic cubicbpf_cubicnet.ipv4.tcp_congestion_control =bpf_cubicThere has been attempt to move the TCP CC to the user space
(e.g. CCP in TCP). The common arguments are faster turn around,
get away from long-tail kernel versions in production...etc, which are legit points.BPF has been the continuous effort to join both kernel and userspace upsides together (e.g. XDP to gain the performance advantage without bypassing the kernel). The recent BPF advancements (in particular BTF-aware verifier, BPF trampoline, BPF CO-RE...) made implementing kernel struct ops (e.g. tcp cc) possible in BPF. It allows a faster turnaround for testing algorithm in the production while leveraging the existing (and continue growing) BPF feature/framework instead of building one specifically for userspace TCP CC.
Quoted 8 lines
This patch allows write access to a few fields in tcp-sock (in
bpf_tcp_ca_btf_struct_access()).The optional "
get_info" is unsupported now. It can be added later. One possible way is to output the info with a btf-id to describe the content.Signed-off-by Martin KaFai Lau <[email protected]>
Ack from bpf/btf perspective.
Re: [PATCH bpf-next v2 07/11] bpf: tcp: Support tcp_congestion_ops in bpf
In reply to Martin KaFai LauQuoted 35 lines · Martin KaFai Lau
This patch makes "struct
tcp_congestion_ops" to be the first user of BPFSTRUCT_OPS. It allows implementing atcp_congestion_opsin bpf.The BPF implemented
tcp_congestion_opscan be used like regular kernel tcp-cc through sysctl and setsockopt. e.g. [root@arch-fb-vm1 bpf]# sysctl -a | egrep congestion net.ipv4.tcp_allowed_congestion_control = reno cubicbpf_cubicnet.ipv4.tcp_available_congestion_control = reno bic cubicbpf_cubicnet.ipv4.tcp_congestion_control =bpf_cubicThere has been attempt to move the TCP CC to the user space
(e.g. CCP in TCP). The common arguments are faster turn around,
get away from long-tail kernel versions in production...etc, which are legit points.BPF has been the continuous effort to join both kernel and userspace upsides together (e.g. XDP to gain the performance advantage without bypassing the kernel). The recent BPF advancements (in particular BTF-aware verifier, BPF trampoline, BPF CO-RE...) made implementing kernel struct ops (e.g. tcp cc) possible in BPF. It allows a faster turnaround for testing algorithm in the production while leveraging the existing (and continue growing) BPF feature/framework instead of building one specifically for userspace TCP CC.
This patch allows write access to a few fields in tcp-sock (in
bpf_tcp_ca_btf_struct_access()).The optional "
get_info" is unsupported now. It can be added later. One possible way is to output the info with a btf-id to describe the content.Signed-off-by Martin KaFai Lau <[email protected]>
Quoted 14 lines
--- include/linux/filter.h | 2 + include/net/tcp.h | 1 + kernel/bpf/bpf_struct_ops_types.h | 7 +- net/core/filter.c | 2 +- net/ipv4/Makefile | 4 + net/ipv4/bpf_tcp_ca.c | 226 ++++++++++++++++++++++++++++++ net/ipv4/tcp_cong.c | 14 +- net/ipv4/tcp_ipv4.c | 6 +- net/ipv4/tcp_minisocks.c | 4 +- net/ipv4/tcp_output.c | 4 +- 10 files changed, 255 insertions(+), 15 deletions(-) create mode 100644 net/ipv4/bpf_tcp_ca.c
Naming nits below. Other than that:
[...]
net/ipv4/bpf_tcp_ca.c line 31
Quoted 5 lines
net/ipv4/bpf_tcp_ca.c new file +5
-0+static const struct btf_type *tcp_sock_type; +static u32 tcp_sock_id, sock_id; + +static int bpf_tcp_ca_init (struct btf *_btf_vmlinux) +{
there is no reason to pass anything but vmlinux's BTF to this function, so I think just having "btf" as a name is OK.
Quoted 8 lines
net/ipv4/bpf_tcp_ca.c new file +8
-0+s32 type_id; + + type_id =btf_find_by_name_kind (_btf_vmlinux,"sock" ,BTF_KIND_STRUCT ); +if (type_id <0 ) +return -EINVAL ; + sock_id = type_id; + + type_id = btf_find_by_name_kind(_btf_vmlinux, "tcp_sock",
net/ipv4/bpf_tcp_ca.c line 49
Quoted 10 lines
net/ipv4/bpf_tcp_ca.c new file +10
-0+ BTF_KIND_STRUCT); +if (type_id <0 ) +return -EINVAL ; + tcp_sock_id = type_id; + tcp_sock_type =btf_type_by_id (_btf_vmlinux, tcp_sock_id); + +return 0 ; +} + +static bool check_optional (u32 member_offset)
check_xxx is quite ambiguous, in general: is it a check that it is optional or that it's not optional? How about using is_optional/is_unsupported to make this clear?
Quoted 11 lines
net/ipv4/bpf_tcp_ca.c new file +11
-0+{ +unsigned int i; + +for (i =0 ; i <ARRAY_SIZE (optional_ops); i++) { +if (member_offset == optional_ops[i]) +return true; + } + +return false; +} +
[...]
kbuild test robot
build botRe: [PATCH bpf-next v2 07/11] bpf: tcp: Support tcp_congestion_ops in bpf
In reply to Martin KaFai LauI love your patch! Perhaps something to improve:
Hi Martin,
I love your patch! Perhaps something to improve:
[auto build test WARNING on bpf-next/master]
[cannot apply to bpf/master net/master v5.5-rc3 next-20191220]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system. BTW, we also suggest to use '--base' option to specify the base tree in git format-patch, please see https://stackoverflow.com/a/37406982]
url: https://github.com/0day-ci/linux/commits/Martin-KaFai-Lau/Introduce-BPF-STRUCT_OPS/20191224-085617 base: https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master config: s390-debug_defconfig (attached as .config) compiler: s390-linux-gcc (GCC) 7.5.0
reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
GCC_VERSION=7.5.0 make.cross ARCH=s390
If you fix the issue, kindly add following tag
All warnings (new ones prefixed by >>):
kernel/bpf/bpf_struct_ops.c: In function 'bpf_struct_ops_init':
>> kernel/bpf/bpf_struct_ops.c:198:1: warning: the frame size of 1208 bytes is larger than 1024 bytes [-Wframe-larger-than=]
}
^vim +198 kernel/bpf/bpf_struct_ops.c
d69ac27055a81d Martin KaFai Lau 2019-12-20 113
d69ac27055a81d Martin KaFai Lau 2019-12-20 114 module_id = btf_find_by_name_kind(_btf_vmlinux, "module",
d69ac27055a81d Martin KaFai Lau 2019-12-20 115 BTF_KIND_STRUCT);
d69ac27055a81d Martin KaFai Lau 2019-12-20 116 if (module_id < 0) {
d69ac27055a81d Martin KaFai Lau 2019-12-20 117 pr_warn("Cannot find struct module in btf_vmlinux\n");
d69ac27055a81d Martin KaFai Lau 2019-12-20 118 return;
d69ac27055a81d Martin KaFai Lau 2019-12-20 119 }
d69ac27055a81d Martin KaFai Lau 2019-12-20 120 module_type = btf_type_by_id(_btf_vmlinux, module_id);
d69ac27055a81d Martin KaFai Lau 2019-12-20 121
b14e6918483a61 Martin KaFai Lau 2019-12-20 122 for (i = 0; i < ARRAY_SIZE(bpf_struct_ops); i++) {
b14e6918483a61 Martin KaFai Lau 2019-12-20 123 st_ops = bpf_struct_ops[i];
b14e6918483a61 Martin KaFai Lau 2019-12-20 124
d69ac27055a81d Martin KaFai Lau 2019-12-20 125 if (strlen(st_ops->name) + VALUE_PREFIX_LEN >=
d69ac27055a81d Martin KaFai Lau 2019-12-20 126 sizeof(value_name)) {
d69ac27055a81d Martin KaFai Lau 2019-12-20 127 pr_warn("struct_ops name %s is too long\n",
d69ac27055a81d Martin KaFai Lau 2019-12-20 128 st_ops->name);
d69ac27055a81d Martin KaFai Lau 2019-12-20 129 continue;
d69ac27055a81d Martin KaFai Lau 2019-12-20 130 }
d69ac27055a81d Martin KaFai Lau 2019-12-20 131 sprintf(value_name, "%s%s", VALUE_PREFIX, st_ops->name);
d69ac27055a81d Martin KaFai Lau 2019-12-20 132
d69ac27055a81d Martin KaFai Lau 2019-12-20 133 value_id = btf_find_by_name_kind(_btf_vmlinux, value_name,
d69ac27055a81d Martin KaFai Lau 2019-12-20 134 BTF_KIND_STRUCT);
d69ac27055a81d Martin KaFai Lau 2019-12-20 135 if (value_id < 0) {
d69ac27055a81d Martin KaFai Lau 2019-12-20 136 pr_warn("Cannot find struct %s in btf_vmlinux\n",
d69ac27055a81d Martin KaFai Lau 2019-12-20 137 value_name);
d69ac27055a81d Martin KaFai Lau 2019-12-20 138 continue;
d69ac27055a81d Martin KaFai Lau 2019-12-20 139 }
d69ac27055a81d Martin KaFai Lau 2019-12-20 140
b14e6918483a61 Martin KaFai Lau 2019-12-20 141 type_id = btf_find_by_name_kind(_btf_vmlinux, st_ops->name,
b14e6918483a61 Martin KaFai Lau 2019-12-20 142 BTF_KIND_STRUCT);
b14e6918483a61 Martin KaFai Lau 2019-12-20 143 if (type_id < 0) {
b14e6918483a61 Martin KaFai Lau 2019-12-20 144 pr_warn("Cannot find struct %s in btf_vmlinux\n",
b14e6918483a61 Martin KaFai Lau 2019-12-20 145 st_ops->name);
b14e6918483a61 Martin KaFai Lau 2019-12-20 146 continue;
b14e6918483a61 Martin KaFai Lau 2019-12-20 147 }
b14e6918483a61 Martin KaFai Lau 2019-12-20 148 t = btf_type_by_id(_btf_vmlinux, type_id);
b14e6918483a61 Martin KaFai Lau 2019-12-20 149 if (btf_type_vlen(t) > BPF_STRUCT_OPS_MAX_NR_MEMBERS) {
b14e6918483a61 Martin KaFai Lau 2019-12-20 150 pr_warn("Cannot support #%u members in struct %s\n",
b14e6918483a61 Martin KaFai Lau 2019-12-20 151 btf_type_vlen(t), st_ops->name);
b14e6918483a61 Martin KaFai Lau 2019-12-20 152 continue;
b14e6918483a61 Martin KaFai Lau 2019-12-20 153 }
b14e6918483a61 Martin KaFai Lau 2019-12-20 154
b14e6918483a61 Martin KaFai Lau 2019-12-20 155 for_each_member(j, t, member) {
b14e6918483a61 Martin KaFai Lau 2019-12-20 156 const struct btf_type *func_proto;
b14e6918483a61 Martin KaFai Lau 2019-12-20 157
b14e6918483a61 Martin KaFai Lau 2019-12-20 158 mname = btf_name_by_offset(_btf_vmlinux,
b14e6918483a61 Martin KaFai Lau 2019-12-20 159 member->name_off);
b14e6918483a61 Martin KaFai Lau 2019-12-20 160 if (!*mname) {
b14e6918483a61 Martin KaFai Lau 2019-12-20 161 pr_warn("anon member in struct %s is not supported\n",
b14e6918483a61 Martin KaFai Lau 2019-12-20 162 st_ops->name);
b14e6918483a61 Martin KaFai Lau 2019-12-20 163 break;
b14e6918483a61 Martin KaFai Lau 2019-12-20 164 }
b14e6918483a61 Martin KaFai Lau 2019-12-20 165
b14e6918483a61 Martin KaFai Lau 2019-12-20 166 if (btf_member_bitfield_size(t, member)) {
b14e6918483a61 Martin KaFai Lau 2019-12-20 167 pr_warn("bit field member %s in struct %s is not supported\n",
b14e6918483a61 Martin KaFai Lau 2019-12-20 168 mname, st_ops->name);
b14e6918483a61 Martin KaFai Lau 2019-12-20 169 break;
b14e6918483a61 Martin KaFai Lau 2019-12-20 170 }
b14e6918483a61 Martin KaFai Lau 2019-12-20 171
b14e6918483a61 Martin KaFai Lau 2019-12-20 172 func_proto = btf_type_resolve_func_ptr(_btf_vmlinux,
b14e6918483a61 Martin KaFai Lau 2019-12-20 173 member->type,
b14e6918483a61 Martin KaFai Lau 2019-12-20 174 NULL);
b14e6918483a61 Martin KaFai Lau 2019-12-20 175 if (func_proto &&
b14e6918483a61 Martin KaFai Lau 2019-12-20 176 btf_distill_func_proto(&log, _btf_vmlinux,
b14e6918483a61 Martin KaFai Lau 2019-12-20 177 func_proto, mname,
b14e6918483a61 Martin KaFai Lau 2019-12-20 178 &st_ops->func_models[j])) {
b14e6918483a61 Martin KaFai Lau 2019-12-20 179 pr_warn("Error in parsing func ptr %s in struct %s\n",
b14e6918483a61 Martin KaFai Lau 2019-12-20 180 mname, st_ops->name);
b14e6918483a61 Martin KaFai Lau 2019-12-20 181 break;
b14e6918483a61 Martin KaFai Lau 2019-12-20 182 }
b14e6918483a61 Martin KaFai Lau 2019-12-20 183 }
b14e6918483a61 Martin KaFai Lau 2019-12-20 184
b14e6918483a61 Martin KaFai Lau 2019-12-20 185 if (j == btf_type_vlen(t)) {
b14e6918483a61 Martin KaFai Lau 2019-12-20 186 if (st_ops->init(_btf_vmlinux)) {
b14e6918483a61 Martin KaFai Lau 2019-12-20 187 pr_warn("Error in init bpf_struct_ops %s\n",
b14e6918483a61 Martin KaFai Lau 2019-12-20 188 st_ops->name);
b14e6918483a61 Martin KaFai Lau 2019-12-20 189 } else {
b14e6918483a61 Martin KaFai Lau 2019-12-20 190 st_ops->type_id = type_id;
b14e6918483a61 Martin KaFai Lau 2019-12-20 191 st_ops->type = t;
d69ac27055a81d Martin KaFai Lau 2019-12-20 192 st_ops->value_id = value_id;
d69ac27055a81d Martin KaFai Lau 2019-12-20 193 st_ops->value_type =
d69ac27055a81d Martin KaFai Lau 2019-12-20 194 btf_type_by_id(_btf_vmlinux, value_id);
b14e6918483a61 Martin KaFai Lau 2019-12-20 195 }
b14e6918483a61 Martin KaFai Lau 2019-12-20 196 }
b14e6918483a61 Martin KaFai Lau 2019-12-20 197 }b14e6918483a61 Martin KaFai Lau 2019-12-20 @198 } b14e6918483a61 Martin KaFai Lau 2019-12-20 199
:::::: The code at line 198 was first introduced by commit
:::::: b14e6918483a61bb02672580bde0aa60f4cce17d bpf: Introduce BPF_PROG_TYPE_STRUCT_OPS
:::::: TO: Martin KaFai Lau <[email protected]>
:::::: CC: 0day robot <[email protected]>
---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org Intel Corporation
kbuild test robot
build botRe: [PATCH bpf-next v2 07/11] bpf: tcp: Support tcp_congestion_ops in bpf
In reply to Martin KaFai LauI love your patch! Yet something to improve:
Hi Martin,
I love your patch! Yet something to improve:
[auto build test ERROR on bpf-next/master]
[cannot apply to bpf/master net/master v5.5-rc3 next-20191219]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system. BTW, we also suggest to use '--base' option to specify the base tree in git format-patch, please see https://stackoverflow.com/a/37406982]
url: https://github.com/0day-ci/linux/commits/Martin-KaFai-Lau/Introduce-BPF-STRUCT_OPS/20191224-085617 base: https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master config: arm64-defconfig (attached as .config) compiler: aarch64-linux-gcc (GCC) 7.5.0
reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
GCC_VERSION=7.5.0 make.cross ARCH=arm64
If you fix the issue, kindly add following tag
All errors (new ones prefixed by >>):
kernel/bpf/bpf_struct_ops.c: In function 'bpf_struct_ops_init':
kernel/bpf/bpf_struct_ops.c:176:8: error: implicit declaration of function 'btf_distill_func_proto'; did you mean 'btf_type_is_func_proto'? [-Werror=implicit-function-declaration]
btf_distill_func_proto(&log, _btf_vmlinux,
^~~~~~~~~~~~~~~~~~~~~~
btf_type_is_func_proto
kernel/bpf/bpf_struct_ops.c: In function 'bpf_struct_ops_map_update_elem':
kernel/bpf/bpf_struct_ops.c:408:2: error: implicit declaration of function 'bpf_map_inc'; did you mean 'bpf_map_put'? [-Werror=implicit-function-declaration]
bpf_map_inc(map);
^~~~~~~~~~~
bpf_map_put
kernel/bpf/bpf_struct_ops.c: In function 'bpf_struct_ops_map_free':
kernel/bpf/bpf_struct_ops.c:468:2: error: implicit declaration of function 'bpf_map_area_free'; did you mean 'bpf_prog_free'? [-Werror=implicit-function-declaration]
bpf_map_area_free(st_map->progs);
^~~~~~~~~~~~~~~~~
bpf_prog_free
kernel/bpf/bpf_struct_ops.c: In function 'bpf_struct_ops_map_alloc':
>> kernel/bpf/bpf_struct_ops.c:515:8: error: implicit declaration of function 'bpf_map_charge_init'; did you mean 'ip_misc_proc_init'? [-Werror=implicit-function-declaration]
err = bpf_map_charge_init(&mem, map_total_size);
^~~~~~~~~~~~~~~~~~~
ip_misc_proc_init
kernel/bpf/bpf_struct_ops.c:519:11: error: implicit declaration of function 'bpf_map_area_alloc'; did you mean 'bpf_prog_alloc'? [-Werror=implicit-function-declaration]
st_map = bpf_map_area_alloc(st_map_size, NUMA_NO_NODE);
^~~~~~~~~~~~~~~~~~
bpf_prog_alloc
kernel/bpf/bpf_struct_ops.c:519:9: warning: assignment makes pointer from integer without a cast [-Wint-conversion]
st_map = bpf_map_area_alloc(st_map_size, NUMA_NO_NODE);
^
kernel/bpf/bpf_struct_ops.c:521:3: error: implicit declaration of function 'bpf_map_charge_finish'; did you mean 'bpf_map_flags_to_cap'? [-Werror=implicit-function-declaration]
bpf_map_charge_finish(&mem);
^~~~~~~~~~~~~~~~~~~~~
bpf_map_flags_to_cap
kernel/bpf/bpf_struct_ops.c:527:17: warning: assignment makes pointer from integer without a cast [-Wint-conversion]
st_map->uvalue = bpf_map_area_alloc(vt->size, NUMA_NO_NODE);
^
kernel/bpf/bpf_struct_ops.c:528:16: warning: assignment makes pointer from integer without a cast [-Wint-conversion]
st_map->progs =
^
>> kernel/bpf/bpf_struct_ops.c:545:2: error: implicit declaration of function 'bpf_map_init_from_attr'; did you mean 'bioset_init_from_src'? [-Werror=implicit-function-declaration]
bpf_map_init_from_attr(map, attr);
^~~~~~~~~~~~~~~~~~~~~~
bioset_init_from_src
>> kernel/bpf/bpf_struct_ops.c:546:2: error: implicit declaration of function 'bpf_map_charge_move'; did you mean 'bio_map_user_iov'? [-Werror=implicit-function-declaration]
bpf_map_charge_move(&map->memory, &mem);
^~~~~~~~~~~~~~~~~~~
bio_map_user_iovcc1: some warnings being treated as errors
vim +515 kernel/bpf/bpf_struct_ops.c
d69ac27055a81d Martin KaFai Lau 2019-12-20 461
d69ac27055a81d Martin KaFai Lau 2019-12-20 462 static void bpf_struct_ops_map_free(struct bpf_map *map)
d69ac27055a81d Martin KaFai Lau 2019-12-20 463 {
d69ac27055a81d Martin KaFai Lau 2019-12-20 464 struct bpf_struct_ops_map *st_map = (struct bpf_struct_ops_map *)map;
d69ac27055a81d Martin KaFai Lau 2019-12-20 465
d69ac27055a81d Martin KaFai Lau 2019-12-20 466 if (st_map->progs)
d69ac27055a81d Martin KaFai Lau 2019-12-20 467 bpf_struct_ops_map_put_progs(st_map);
d69ac27055a81d Martin KaFai Lau 2019-12-20 @468 bpf_map_area_free(st_map->progs);
d69ac27055a81d Martin KaFai Lau 2019-12-20 469 bpf_jit_free_exec(st_map->image);
d69ac27055a81d Martin KaFai Lau 2019-12-20 470 bpf_map_area_free(st_map->uvalue);
d69ac27055a81d Martin KaFai Lau 2019-12-20 471 bpf_map_area_free(st_map);
d69ac27055a81d Martin KaFai Lau 2019-12-20 472 }
d69ac27055a81d Martin KaFai Lau 2019-12-20 473
d69ac27055a81d Martin KaFai Lau 2019-12-20 474 static int bpf_struct_ops_map_alloc_check(union bpf_attr *attr)
d69ac27055a81d Martin KaFai Lau 2019-12-20 475 {
d69ac27055a81d Martin KaFai Lau 2019-12-20 476 if (attr->key_size != sizeof(unsigned int) || attr->max_entries != 1 ||
d69ac27055a81d Martin KaFai Lau 2019-12-20 477 attr->map_flags || !attr->btf_vmlinux_value_type_id)
d69ac27055a81d Martin KaFai Lau 2019-12-20 478 return -EINVAL;
d69ac27055a81d Martin KaFai Lau 2019-12-20 479 return 0;
d69ac27055a81d Martin KaFai Lau 2019-12-20 480 }
d69ac27055a81d Martin KaFai Lau 2019-12-20 481
d69ac27055a81d Martin KaFai Lau 2019-12-20 482 static struct bpf_map *bpf_struct_ops_map_alloc(union bpf_attr *attr)
d69ac27055a81d Martin KaFai Lau 2019-12-20 483 {
d69ac27055a81d Martin KaFai Lau 2019-12-20 484 const struct bpf_struct_ops *st_ops;
d69ac27055a81d Martin KaFai Lau 2019-12-20 485 size_t map_total_size, st_map_size;
d69ac27055a81d Martin KaFai Lau 2019-12-20 486 struct bpf_struct_ops_map *st_map;
d69ac27055a81d Martin KaFai Lau 2019-12-20 487 const struct btf_type *t, *vt;
d69ac27055a81d Martin KaFai Lau 2019-12-20 488 struct bpf_map_memory mem;
d69ac27055a81d Martin KaFai Lau 2019-12-20 489 struct bpf_map *map;
d69ac27055a81d Martin KaFai Lau 2019-12-20 490 int err;
d69ac27055a81d Martin KaFai Lau 2019-12-20 491
d69ac27055a81d Martin KaFai Lau 2019-12-20 492 if (!capable(CAP_SYS_ADMIN))
d69ac27055a81d Martin KaFai Lau 2019-12-20 493 return ERR_PTR(-EPERM);
d69ac27055a81d Martin KaFai Lau 2019-12-20 494
d69ac27055a81d Martin KaFai Lau 2019-12-20 495 st_ops = bpf_struct_ops_find_value(attr->btf_vmlinux_value_type_id);
d69ac27055a81d Martin KaFai Lau 2019-12-20 496 if (!st_ops)
d69ac27055a81d Martin KaFai Lau 2019-12-20 497 return ERR_PTR(-ENOTSUPP);
d69ac27055a81d Martin KaFai Lau 2019-12-20 498
d69ac27055a81d Martin KaFai Lau 2019-12-20 499 vt = st_ops->value_type;
d69ac27055a81d Martin KaFai Lau 2019-12-20 500 if (attr->value_size != vt->size)
d69ac27055a81d Martin KaFai Lau 2019-12-20 501 return ERR_PTR(-EINVAL);
d69ac27055a81d Martin KaFai Lau 2019-12-20 502
d69ac27055a81d Martin KaFai Lau 2019-12-20 503 t = st_ops->type;
d69ac27055a81d Martin KaFai Lau 2019-12-20 504
d69ac27055a81d Martin KaFai Lau 2019-12-20 505 st_map_size = sizeof(*st_map) +
d69ac27055a81d Martin KaFai Lau 2019-12-20 506 /* kvalue stores the
d69ac27055a81d Martin KaFai Lau 2019-12-20 507 * struct bpf_struct_ops_tcp_congestions_ops
d69ac27055a81d Martin KaFai Lau 2019-12-20 508 */
d69ac27055a81d Martin KaFai Lau 2019-12-20 509 (vt->size - sizeof(struct bpf_struct_ops_value));
d69ac27055a81d Martin KaFai Lau 2019-12-20 510 map_total_size = st_map_size +
d69ac27055a81d Martin KaFai Lau 2019-12-20 511 /* uvalue */
d69ac27055a81d Martin KaFai Lau 2019-12-20 512 sizeof(vt->size) +
d69ac27055a81d Martin KaFai Lau 2019-12-20 513 /* struct bpf_progs **progs */
d69ac27055a81d Martin KaFai Lau 2019-12-20 514 btf_type_vlen(t) * sizeof(struct bpf_prog *);
d69ac27055a81d Martin KaFai Lau 2019-12-20 @515 err = bpf_map_charge_init(&mem, map_total_size);
d69ac27055a81d Martin KaFai Lau 2019-12-20 516 if (err < 0)
d69ac27055a81d Martin KaFai Lau 2019-12-20 517 return ERR_PTR(err);
d69ac27055a81d Martin KaFai Lau 2019-12-20 518
d69ac27055a81d Martin KaFai Lau 2019-12-20 519 st_map = bpf_map_area_alloc(st_map_size, NUMA_NO_NODE);
d69ac27055a81d Martin KaFai Lau 2019-12-20 520 if (!st_map) {
d69ac27055a81d Martin KaFai Lau 2019-12-20 521 bpf_map_charge_finish(&mem);
d69ac27055a81d Martin KaFai Lau 2019-12-20 522 return ERR_PTR(-ENOMEM);
d69ac27055a81d Martin KaFai Lau 2019-12-20 523 }
d69ac27055a81d Martin KaFai Lau 2019-12-20 524 st_map->st_ops = st_ops;
d69ac27055a81d Martin KaFai Lau 2019-12-20 525 map = &st_map->map;
d69ac27055a81d Martin KaFai Lau 2019-12-20 526
d69ac27055a81d Martin KaFai Lau 2019-12-20 @527 st_map->uvalue = bpf_map_area_alloc(vt->size, NUMA_NO_NODE);
d69ac27055a81d Martin KaFai Lau 2019-12-20 @528 st_map->progs =
d69ac27055a81d Martin KaFai Lau 2019-12-20 529 bpf_map_area_alloc(btf_type_vlen(t) * sizeof(struct bpf_prog *),
d69ac27055a81d Martin KaFai Lau 2019-12-20 530 NUMA_NO_NODE);
d69ac27055a81d Martin KaFai Lau 2019-12-20 531 /* Each trampoline costs < 64 bytes. Ensure one page
d69ac27055a81d Martin KaFai Lau 2019-12-20 532 * is enough for max number of func ptrs.
d69ac27055a81d Martin KaFai Lau 2019-12-20 533 */
d69ac27055a81d Martin KaFai Lau 2019-12-20 534 BUILD_BUG_ON(PAGE_SIZE / 64 < BPF_STRUCT_OPS_MAX_NR_MEMBERS);
d69ac27055a81d Martin KaFai Lau 2019-12-20 535 st_map->image = bpf_jit_alloc_exec(PAGE_SIZE);
d69ac27055a81d Martin KaFai Lau 2019-12-20 536 if (!st_map->uvalue || !st_map->progs || !st_map->image) {
d69ac27055a81d Martin KaFai Lau 2019-12-20 537 bpf_struct_ops_map_free(map);
d69ac27055a81d Martin KaFai Lau 2019-12-20 538 bpf_map_charge_finish(&mem);
d69ac27055a81d Martin KaFai Lau 2019-12-20 539 return ERR_PTR(-ENOMEM);
d69ac27055a81d Martin KaFai Lau 2019-12-20 540 }
d69ac27055a81d Martin KaFai Lau 2019-12-20 541
d69ac27055a81d Martin KaFai Lau 2019-12-20 542 spin_lock_init(&st_map->lock);
d69ac27055a81d Martin KaFai Lau 2019-12-20 543 set_vm_flush_reset_perms(st_map->image);
d69ac27055a81d Martin KaFai Lau 2019-12-20 544 set_memory_x((long)st_map->image, 1);
d69ac27055a81d Martin KaFai Lau 2019-12-20 @545 bpf_map_init_from_attr(map, attr);
d69ac27055a81d Martin KaFai Lau 2019-12-20 @546 bpf_map_charge_move(&map->memory, &mem);
d69ac27055a81d Martin KaFai Lau 2019-12-20 547
d69ac27055a81d Martin KaFai Lau 2019-12-20 548 return map;
d69ac27055a81d Martin KaFai Lau 2019-12-20 549 }
d69ac27055a81d Martin KaFai Lau 2019-12-20 550 :::::: The code at line 515 was first introduced by commit
:::::: d69ac27055a81d26ee1bfe54b9655cf81ebd5ac9 bpf: Introduce BPF_MAP_TYPE_STRUCT_OPS
:::::: TO: Martin KaFai Lau <[email protected]>
:::::: CC: 0day robot <[email protected]>
---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org Intel Corporation
PATCH v2 8/11 bpf: Add BPF_FUNC_tcp_send_ack helper
In reply to Martin KaFai LauAdd a helper to send out a tcp-ack. It will be used in the later bpf_dctcp implementation that requires to send out an ack when the CE state changed.
--- include/uapi/linux/bpf.h | 11 ++++++++++- net/ipv4/bpf_tcp_ca.c | 24 +++++++++++++++++++++++- 2 files changed, 33 insertions(+), 2 deletions(-)
include/uapi/linux/bpf.h +10 -1
@@ -2837,6 +2837,14 @@ union bpf_attr {* Return * On success, the strictly positive length of the string, including * the trailing NUL character. On error, a negative value. +* +* int bpf_tcp_send_ack(void *tp, u32 rcv_nxt) +* Description +* Send out a tcp-ack. *tp* is the in-kernel struct tcp_sock. +* *rcv_nxt* is the ack_seq to be sent out. +* Return +* 0 on success, or a negative error in case of failure. +* */ #define __BPF_FUNC_MAPPER (FN ) \ FN(unspec), \
@@ -2954,7 +2962,8 @@ union bpf_attr {FN (probe_read_user), \FN (probe_read_kernel), \FN (probe_read_user_str), \ -FN (probe_read_kernel_str), +FN (probe_read_kernel_str), \ +FN (tcp_send_ack),/* integer value in 'imm' field of BPF_CALL instruction selects which helper * function eBPF program intends to call
net/ipv4/bpf_tcp_ca.c +23 -1
@@ -144,11 +144,33 @@ static int bpf_tcp_ca_btf_struct_access(struct bpf_verifier_log *log,return NOT_INIT ; } +BPF_CALL_2(bpf_tcp_send_ack, struct tcp_sock *, tp, u32, rcv_nxt) +{ +/* bpf_tcp_ca prog cannot have NULL tp */ +__tcp_send_ack ((struct sock *)tp, rcv_nxt); +return 0 ; +} + +static const struct bpf_func_proto bpf_tcp_send_ack_proto = { + .func = bpf_tcp_send_ack, + .gpl_only = false, +/* In case we want to report error later */ + .ret_type =RET_INTEGER , + .arg1_type =ARG_PTR_TO_BTF_ID , + .arg2_type =ARG_ANYTHING , + .btf_id = &tcp_sock_id, +}; +static const struct bpf_func_proto *bpf_tcp_ca_get_func_proto (enum bpf_func_id func_id,const struct bpf_prog *prog) { -return bpf_base_func_proto (func_id); +switch (func_id) { +case BPF_FUNC_tcp_send_ack: +return &bpf_tcp_send_ack_proto; +default : +return bpf_base_func_proto (func_id); + } }static const struct bpf_verifier_ops bpf_tcp_ca_verifier_ops = {
Signature · 1 line
2.17.1
PATCH v2 9/11 bpf: Synch uapi bpf.h to tools/
In reply to Martin KaFai LauThis patch sync uapi bpf.h to tools/
--- tools/include/uapi/linux/bpf.h | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-)
tools/include/uapi/linux/bpf.h +17 -2
@@ -136,6 +136,7 @@ enum bpf_map_type {BPF_MAP_TYPE_STACK ,BPF_MAP_TYPE_SK_STORAGE ,BPF_MAP_TYPE_DEVMAP_HASH , + BPF_MAP_TYPE_STRUCT_OPS, };/* Note that tracing related programs such as
@@ -174,6 +175,7 @@ enum bpf_prog_type {BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE ,BPF_PROG_TYPE_CGROUP_SOCKOPT ,BPF_PROG_TYPE_TRACING , + BPF_PROG_TYPE_STRUCT_OPS, }; enum bpf_attach_type {
@@ -397,6 +399,10 @@ union bpf_attr {__u32 btf_fd;/* fd pointing to a BTF type data */ __u32 btf_key_type_id;/* BTF type_id of the key */ __u32 btf_value_type_id;/* BTF type_id of the value */ +__u32 btf_vmlinux_value_type_id;/* BTF type_id of a kernel- +* struct stored as the +* map value +*/ };struct {/* anonymous struct used by BPF_MAP_*_ELEM commands */
@@ -2831,6 +2837,14 @@ union bpf_attr {* Return * On success, the strictly positive length of the string, including * the trailing NUL character. On error, a negative value. +* +* int bpf_tcp_send_ack(void *tp, u32 rcv_nxt) +* Description +* Send out a tcp-ack. *tp* is the in-kernel struct tcp_sock. +* *rcv_nxt* is the ack_seq to be sent out. +* Return +* 0 on success, or a negative error in case of failure. +* */ #define __BPF_FUNC_MAPPER (FN ) \ FN(unspec), \
@@ -2948,7 +2962,8 @@ union bpf_attr {FN (probe_read_user), \FN (probe_read_kernel), \FN (probe_read_user_str), \ -FN (probe_read_kernel_str), +FN (probe_read_kernel_str), \ +FN (tcp_send_ack),/* integer value in 'imm' field of BPF_CALL instruction selects which helper * function eBPF program intends to call
@@ -3349,7 +3364,7 @@ struct bpf_map_info {__u32 map_flags;char name[BPF_OBJ_NAME_LEN ];__u32 ifindex; - __u32 :32 ; +__u32 btf_vmlinux_value_type_id;__u64 netns_dev;__u64 netns_ino;__u32 btf_id;
Signature · 1 line
2.17.1
PATCH v2 10/11 bpf: libbpf: Add STRUCT_OPS support
In reply to Martin KaFai LauThis patch adds BPF STRUCT_OPS support to libbpf.
The only sec_name convention is SEC(".struct_ops") to identify the struct_ops implemented in BPF,
e.g. To implement a tcp_congestion_ops:
SEC(".struct_ops")
struct tcp_congestion_ops dctcp = {
.init = (void *)dctcp_init, /* <-- a bpf_prog */
/* ... some more func prts ... */
.name = "bpf_dctcp",
};Each struct_ops is defined as a global variable under SEC(".struct_ops") as above. libbpf creates a map for each variable and the variable name is the map's name. Multiple struct_ops is supported under SEC(".struct_ops").
In the bpf_object__open phase, libbpf will look for the SEC(".struct_ops") section and find out what is the btf-type the struct_ops is implementing. Note that the btf-type here is referring to
a type in the bpf_prog.o's btf. A "struct bpf_map" is added by bpf_object__add_map() as other maps do. It will then collect (through SHT_REL) where are the bpf progs that the func ptrs are referring to. No btf_vmlinux is needed in
the open phase.
In the bpf_object__load phase, the map-fields, which depend
on the btf_vmlinux, are initialized (in bpf_map__init_kern_struct_ops()). It will also set the prog->type, prog->attach_btf_id, and prog->expected_attach_type. Thus, the prog's properties do
not rely on its section name.
[ Currently, the bpf_prog's btf-type ==> btf_vmlinux's btf-type matching
process is as simple as: member-name match + btf-kind match + size match. If these matching conditions fail, libbpf will reject.
The current targeting support is "struct tcp_congestion_ops" which most of its members are function pointers.
The member ordering of the bpf_prog's btf-type can be different from the btf_vmlinux's btf-type. ]
Then, all obj->maps are created as usual (in bpf_object__create_maps()).
Once the maps are created and prog's properties are all set, the libbpf will proceed to load all the progs.
bpf_map__attach_struct_ops() is added to register a struct_ops map to a kernel subsystem.
--- tools/lib/bpf/bpf.c | 10 +- tools/lib/bpf/bpf.h | 5 +- tools/lib/bpf/libbpf.c | 639 +++++++++++++++++++++++++++++++++- tools/lib/bpf/libbpf.h | 1 + tools/lib/bpf/libbpf.map | 1 + tools/lib/bpf/libbpf_probes.c | 2 + 6 files changed, 646 insertions(+), 12 deletions(-)
tools/lib/bpf/bpf.c +8 -2
@@ -95,7 +95,11 @@ int bpf_create_map_xattr(const struct bpf_create_map_attr *create_attr) attr.btf_key_type_id = create_attr->btf_key_type_id; attr.btf_value_type_id = create_attr->btf_value_type_id; attr.map_ifindex = create_attr->map_ifindex; - attr.inner_map_fd = create_attr->inner_map_fd; +if (attr.map_type ==BPF_MAP_TYPE_STRUCT_OPS ) + attr.btf_vmlinux_value_type_id = + create_attr->btf_vmlinux_value_type_id; +else + attr.inner_map_fd = create_attr->inner_map_fd;return sys_bpf (BPF_MAP_CREATE , &attr,sizeof (attr)); }
@@ -228,7 +232,9 @@ int bpf_load_program_xattr(const struct bpf_load_program_attr *load_attr,memset (&attr,0 ,sizeof (attr)); attr.prog_type = load_attr->prog_type; attr.expected_attach_type = load_attr->expected_attach_type; -if (attr.prog_type ==BPF_PROG_TYPE_TRACING ) { +if (attr.prog_type ==BPF_PROG_TYPE_STRUCT_OPS ) { + attr.attach_btf_id = load_attr->attach_btf_id; + }else if (attr.prog_type ==BPF_PROG_TYPE_TRACING ) { attr.attach_btf_id = load_attr->attach_btf_id; attr.attach_prog_fd = load_attr->attach_prog_fd; }else {
tools/lib/bpf/bpf.h +4 -1
@@ -46,7 +46,10 @@ struct bpf_create_map_attr {__u32 btf_key_type_id;__u32 btf_value_type_id;__u32 map_ifindex; -__u32 inner_map_fd; +union { +__u32 inner_map_fd; +__u32 btf_vmlinux_value_type_id; + }; };LIBBPF_API int
tools/lib/bpf/libbpf.c +630 -9
798 lines not on this page · raw message
tools/lib/bpf/libbpf.h +1 -0
@@ -355,6 +355,7 @@ struct bpf_map_def {* so no need to worry about a name clash. */ struct bpf_map ; +LIBBPF_API struct bpf_link *bpf_map__attach_struct_ops(struct bpf_map *map); LIBBPF_API struct bpf_map *bpf_object__find_map_by_name (const struct bpf_object *obj,const char *name);
tools/lib/bpf/libbpf.map +1 -0
@@ -213,6 +213,7 @@ LIBBPF_0.0.7 { global: btf_dump__emit_type_decl; bpf_link__disconnect; + bpf_map__attach_struct_ops; bpf_object__find_program_by_name; bpf_object__attach_skeleton; bpf_object__destroy_skeleton;
tools/lib/bpf/libbpf_probes.c +2 -0
@@ -103,6 +103,7 @@ probe_load(enum bpf_prog_type prog_type, const struct bpf_insn *insns,case BPF_PROG_TYPE_CGROUP_SYSCTL :case BPF_PROG_TYPE_CGROUP_SOCKOPT :case BPF_PROG_TYPE_TRACING : +case BPF_PROG_TYPE_STRUCT_OPS :default :break ; }
@@ -251,6 +252,7 @@ bool bpf_probe_map_type(enum bpf_map_type map_type, __u32 ifindex)case BPF_MAP_TYPE_XSKMAP :case BPF_MAP_TYPE_SOCKHASH :case BPF_MAP_TYPE_REUSEPORT_SOCKARRAY : +case BPF_MAP_TYPE_STRUCT_OPS :default :break ; }
Signature · 1 line
2.17.1
Re: [PATCH bpf-next v2 10/11] bpf: libbpf: Add STRUCT_OPS support
Quoted 43 lines · Martin KaFai Lau
This patch adds BPF
STRUCT_OPSsupport to libbpf.The only
sec_nameconvention isSEC(".struct_ops")to identify thestruct_opsimplemented in BPF,
e.g. To implement atcp_congestion_ops:
SEC(".struct_ops")struct tcp_congestion_ops dctcp = { .init = (void *)dctcp_init, /* <-- a bpf_prog */ /* ... some more func prts ... */ .name = "bpf_dctcp", };Each
struct_opsis defined as a global variable underSEC(".struct_ops")as above. libbpf creates a map for each variable and the variable name is the map's name. Multiplestruct_opsis supported underSEC(".struct_ops").In the
bpf_object__openphase, libbpf will look for theSEC(".struct_ops")section and find out what is the btf-type thestruct_opsis implementing. Note that the btf-type here is referring to
a type in the bpf_prog.o's btf. A "structbpf_map" is added bybpf_object__add_map()as other maps do. It will then collect (throughSHT_REL) where are the bpf progs that the func ptrs are referring to. Nobtf_vmlinuxis needed in
the open phase.In the
bpf_object__loadphase, the map-fields, which depend
on thebtf_vmlinux, are initialized (inbpf_map__init_kern_struct_ops()). It will also set theprog->type,prog->attach_btf_id, andprog->expected_attach_type. Thus, the prog's properties do
not rely on its section name.
[ Currently, the bpf_prog's btf-type ==> btf_vmlinux's btf-type matching
process is as simple as: member-name match + btf-kind match + size match. If these matching conditions fail, libbpf will reject.
The current targeting support is "structtcp_congestion_ops" which most of its members are function pointers.
The member ordering of the bpf_prog's btf-type can be different from the btf_vmlinux's btf-type. ]Then, all
obj->mapsare created as usual (inbpf_object__create_maps()).
Quoted 8 lines
Once the maps are created and prog's properties are all set, the libbpf will proceed to load all the progs.
bpf_map__attach_struct_ops()is added to register astruct_opsmap to a kernel subsystem.Signed-off-by Martin KaFai Lau <[email protected]>---
This looks great, Martin! I just have few nits/suggestions, but overall I think this approach is much better.
After this lands, please follow up with adding support for struct_ops maps into BPF skeleton, so that it's attached automatically on skeleton load.
Quoted 8 lines
tools/lib/bpf/bpf.c | 10 +- tools/lib/bpf/bpf.h | 5 +- tools/lib/bpf/libbpf.c | 639 +++++++++++++++++++++++++++++++++- tools/lib/bpf/libbpf.h | 1 + tools/lib/bpf/libbpf.map | 1 + tools/lib/bpf/libbpf_probes.c | 2 + 6 files changed, 646 insertions(+), 12 deletions(-)
[...]
Quoted 28 lines
tools/lib/bpf/libbpf.c +28
-0+ member =btf_members (type); +for (i =0 ; i <btf_vlen (type); i++, member++) { +const struct btf_type *mtype, *kern_mtype; +__u32 mtype_id, kern_mtype_id; +void *mdata, *kern_mdata; +__s64 msize, kern_msize; +__u32 moff, kern_moff; +__u32 kern_member_idx; +const char *mname; + + mname =btf__name_by_offset (btf, member->name_off); + kern_member =find_member_by_name (kern_btf, kern_type, mname); +if (!kern_member) { +pr_warn ("struct_ops map %s init_kern %s: Cannot find member %s in kernel BTF\n" , + map->name, tname, mname); +return -ENOTSUP ; + } + + kern_member_idx = kern_member -btf_members (kern_type); +if (btf_member_bitfield_size (type, i) || +btf_member_bitfield_size (kern_type, kern_member_idx)) { +pr_warn ("struct_ops map %s init_kern %s: bitfield %s is not supported\n" , + map->name, tname, mname); +return -ENOTSUP ; + } + + moff = member->offset /8 ; + kern_moff = kern_member->offset /8 ;
tools/lib/bpf/libbpf.c static __u32 get_kernel_version(void) · line 778
Quoted 10 lines
tools/lib/bpf/libbpf.c +10
-0+ + mdata = data + moff; + kern_mdata = kern_data + kern_moff; + + mtype_id = member->type; + kern_mtype_id = kern_member->type; + + mtype =resolve_ptr (btf, mtype_id,NULL ); + kern_mtype =resolve_ptr (kern_btf, kern_mtype_id,NULL ); +if (mtype && kern_mtype) {
This check seems more logical after you resolve mtype_id and kern_mtype_id below and check that they have same BTF_INFO_KIND. After that you can check for a case of pointers and handle it, if not pointers - proceed to determining size.
That way you might also get rid of resolve_ptr and resolve_func_ptr functions, because here you already resolved pointer, so just normal btf__resolve_type will give you what it's pointing to. Then the only use of resolve_func_ptr_resolve_ptr will be in bpf_object__collect_struct_ops_map_reloc, which you can just inline at that point and not really loose any readability.
Quoted 77 lines
tools/lib/bpf/libbpf.c +77
-0+struct bpf_program *prog; + +if (!btf_is_func_proto (mtype) || + !btf_is_func_proto (kern_mtype)) { +pr_warn ("struct_ops map %s init_kern %s: non func ptr %s is not supported\n" , + map->name, tname, mname); +return -ENOTSUP ; + } + + prog = st_ops->progs[i]; +if (!prog) { +pr_debug ("struct_ops map %s init_kern %s: func ptr %s is not set\n" , + map->name, tname, mname); +continue ; + } + +if (prog->type !=BPF_PROG_TYPE_UNSPEC && + (prog->type !=BPF_PROG_TYPE_STRUCT_OPS || + prog->attach_btf_id != kern_type_id || + prog->expected_attach_type != kern_member_idx)) { +pr_warn ("struct_ops map %s init_kern %s: Cannot use prog %s in type %u attach_btf_id %u expected_attach_type %u for func ptr %s\n" , + map->name, tname, prog->name, + prog->type, prog->attach_btf_id, + prog->expected_attach_type, mname); +return -ENOTSUP ; + } + + prog->type =BPF_PROG_TYPE_STRUCT_OPS ; + prog->attach_btf_id = kern_type_id; + prog->expected_attach_type = kern_member_idx; + + st_ops->kern_func_off[i] = kern_data_off + kern_moff; + +pr_debug ("struct_ops map %s init_kern %s: func ptr %s is set to prog %s from data(+%u) to kern_data(+%u)\n" , + map->name, tname, mname, prog->name, moff, + kern_moff); + +continue ; + } + + mtype_id =btf__resolve_type (btf, mtype_id); + kern_mtype_id =btf__resolve_type (kern_btf, kern_mtype_id); +if (mtype_id <0 || kern_mtype_id <0 ) { +pr_warn ("struct_ops map %s init_kern %s: Cannot resolve the type for %s\n" , + map->name, tname, mname); +return -ENOTSUP ; + } + + mtype =btf__type_by_id (btf, mtype_id); + kern_mtype =btf__type_by_id (kern_btf, kern_mtype_id); +if (BTF_INFO_KIND (mtype->info) != +BTF_INFO_KIND (kern_mtype->info)) { +pr_warn ("struct_ops map %s init_kern %s: Unmatched member type %s %u != %u(kernel)\n" , + map->name, tname, mname, +BTF_INFO_KIND (mtype->info), +BTF_INFO_KIND (kern_mtype->info)); +return -ENOTSUP ; + } + + msize =btf__resolve_size (btf, mtype_id); + kern_msize =btf__resolve_size (kern_btf, kern_mtype_id); +if (msize <0 || kern_msize <0 || msize != kern_msize) { +pr_warn ("struct_ops map %s init_kern %s: Error in size of member %s: %zd != %zd(kernel)\n" , + map->name, tname, mname, + (ssize_t )msize, (ssize_t )kern_msize); +return -ENOTSUP ; + } + +pr_debug ("struct_ops map %s init_kern %s: copy %s %u bytes from data(+%u) to kern_data(+%u)\n" , + map->name, tname, mname, (unsigned int )msize, + moff, kern_moff); +memcpy (kern_mdata, mdata, msize); + } + +return 0 ; +} +
[...]
tools/lib/bpf/libbpf.c static __u32 get_kernel_version(void) · line 932
Quoted 11 lines
tools/lib/bpf/libbpf.c +11
-0+ symbols = obj->efile.symbols; + btf = obj->btf; + nrels = shdr->sh_size / shdr->sh_entsize; +for (i =0 ; i < nrels; i++) { +if (!gelf_getrel (data, i, &rel)) { +pr_warn ("struct_ops map reloc: failed to get %d reloc\n" , i); +return -LIBBPF_ERRNO__FORMAT ; + } + +if (!gelf_getsym (symbols,GELF_R_SYM (rel.r_info), &sym)) { + pr_warn("struct_ops map reloc: symbol %" PRIx64 " not found\n",
please use %zx and explicit cast to size_t instead of PRIx64
Quoted 4 lines
tools/lib/bpf/libbpf.c +4
-0+ GELF_R_SYM(rel.r_info)); +return -LIBBPF_ERRNO__FORMAT ; + } +
tools/lib/bpf/libbpf.c static __u32 get_kernel_version(void) · line 946
Quoted 10 lines
tools/lib/bpf/libbpf.c +10
-0+ name =elf_strptr (obj->efile.elf, obj->efile.strtabidx, + sym.st_name) ? :"<?>" ; + map =find_struct_ops_map_by_offset (obj, rel.r_offset); +if (!map) { +pr_warn ("struct_ops map reloc: cannot find map at rel.r_offset %zu\n" , + (size_t )rel.r_offset); +return -EINVAL ; + } + + moff = rel.r_offset - map->sec_offset;
nit: double space
Quoted 3 lines
tools/lib/bpf/libbpf.c +3
-0+ shdr_idx = sym.st_shndx; + st_ops = map->st_ops; + tname = st_ops->tname;
[...]
Quoted 6 lines
tools/lib/bpf/libbpf.c +6
-0+ datasec =btf__type_by_id (btf, datasec_id); + vsi =btf_var_secinfos (datasec); +for (i =0 ; i <btf_vlen (datasec); i++, vsi++) { + type =btf__type_by_id (obj->btf, vsi->type); + var_name =btf__name_by_offset (obj->btf, type->name_off); +
tools/lib/bpf/libbpf.c static __u32 get_kernel_version(void) · line 1028
Quoted 10 lines
tools/lib/bpf/libbpf.c +10
-0+ type_id =btf__resolve_type (obj->btf, vsi->type); +if (type_id <0 ) { +pr_warn ("struct_ops init: Cannot resolve var type_id %u in DATASEC %s\n" , + vsi->type,STRUCT_OPS_SEC ); +return -EINVAL ; + } + + type =btf__type_by_id (obj->btf, type_id); + tname =btf__name_by_offset (obj->btf, type->name_off); +if (!btf_is_struct (type)) {
if tname is empty, it's also not of much use, so might be a good idea to error out here with some context?
Quoted 23 lines
tools/lib/bpf/libbpf.c +23
-0+pr_warn ("struct_ops init: %s is not a struct\n" , tname); +return -EINVAL ; + } + + map =bpf_object__add_map (obj); +if (IS_ERR (map)) +return PTR_ERR (map); + + map->sec_idx = obj->efile.st_ops_shndx; + map->sec_offset = vsi->offset; + map->name =strdup (var_name); +if (!map->name) +return -ENOMEM ; + + map->def.type =BPF_MAP_TYPE_STRUCT_OPS ; + map->def.key_size =sizeof (int ); + map->def.value_size = type->size; + map->def.max_entries =1 ; + + map->st_ops =calloc (1 ,sizeof (*map->st_ops)); +if (!map->st_ops) +return -ENOMEM ; + st_ops = map->st_ops;
tools/lib/bpf/libbpf.c static __u32 get_kernel_version(void) · line 1061
Quoted 10 lines
tools/lib/bpf/libbpf.c +10
-0+ st_ops->data =malloc (type->size); + st_ops->progs =calloc (btf_vlen (type),sizeof (*st_ops->progs)); + st_ops->kern_func_off =malloc (btf_vlen (type) * +sizeof (*st_ops->kern_func_off)); +if (!st_ops->data || !st_ops->progs || !st_ops->kern_func_off) +return -ENOMEM ; + +memcpy (st_ops->data, + obj->efile.st_ops_data->d_buf + vsi->offset, + type->size);
maybe also check that d_size is big enough to read data from?
Quoted 11 lines
tools/lib/bpf/libbpf.c +11
-0+ st_ops->tname = tname; + st_ops->type = type; + st_ops->type_id = type_id; + +pr_debug ("struct_ops init: %s found. type_id:%u var_name:%s offset:%u\n" , + tname, type_id, var_name, vsi->offset); + } + +return 0 ; +} +
[...]
tools/lib/bpf/libbpf.c int bpf_object__unload(struct bpf_object *obj) · line 5313
Quoted 6 lines
tools/lib/bpf/libbpf.c +3
-1-for (i =0 ; i < obj->nr_maps; i++) +for (i =0 ; i < obj->nr_maps; i++) {zclose (obj->maps[i].fd); +if (obj->maps[i].st_ops) +zfree (&obj->maps[i].st_ops->kern_vdata);
any specific reason to deallocate only kern_vdata? maybe just consolidate all the clean up in bpf_object__close instead?
Quoted 42 lines
tools/lib/bpf/libbpf.c +13
-1+ }for (i =0 ; i < obj->nr_programs; i++)bpf_program__unload (&obj->programs[i]);@@ -4866,6 +5427,7 @@ int bpf_object__load_xattr(struct bpf_object_load_attr *attr) err = err ? :bpf_object__resolve_externs (obj, obj->kconfig); err = err ? :bpf_object__sanitize_and_load_btf (obj); err = err ? :bpf_object__sanitize_maps (obj); + err = err ? :bpf_object__init_kern_struct_ops_maps (obj); err = err ? :bpf_object__create_maps (obj); err = err ? :bpf_object__relocate (obj, attr->target_btf_path); err = err ? :bpf_object__load_progs (obj, attr->log_level);@@ -5453,6 +6015,13 @@ void bpf_object__close(struct bpf_object *obj) map->mmaped =NULL ; } +if (map->st_ops) { +zfree (&map->st_ops->data); +zfree (&map->st_ops->progs); +zfree (&map->st_ops->kern_func_off); +zfree (&map->st_ops); + } +zfree (&map->name);zfree (&map->pin_path); }@@ -5954,7 +6523,7 @@ int libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type,int libbpf_find_vmlinux_btf_id (const char *name,enum bpf_attach_type attach_type) { -struct btf *btf =bpf_core_find_kernel_btf (); +struct btf *btf =bpf_find_kernel_btf ();char raw_tp_btf[128 ] =BTF_PREFIX ;char *dst = raw_tp_btf +sizeof (BTF_PREFIX ) -1 ;const char *btf_name;@@ -6780,6 +7349,58 @@ struct bpf_link *bpf_program__attach(struct bpf_program *prog)return sec_def->attach_fn (sec_def, prog); } +static int bpf_link__detach_struct_ops (struct bpf_link *link) +{ +struct bpf_link_fd *l = (void *)link;
tools/lib/bpf/libbpf.c struct bpf_link *bpf_program__attach(struct bpf_program *prog) · line 7364
Quoted 10 lines
tools/lib/bpf/libbpf.c +10
-0+__u32 zero =0 ; + +if (bpf_map_delete_elem (l->fd, &zero)) +return -errno; + +return 0 ; +} + +struct bpf_link *bpf_map__attach_struct_ops (struct bpf_map *map) +{
This looks great!
[...]
tools/lib/bpf/libbpf.h struct bpf_map_def { · line 358
Quoted 12 lines
tools/lib/bpf/libbpf.c +0
-0enum bpf_perf_event_ret bpf_perf_event_read_simple(void *mmap_mem, size_t mmap_size, size_t page_size, void **copy_mem, size_t *copy_size,tools/lib/bpf/libbpf.h +1
-0@@ -355,6 +355,7 @@ struct bpf_map_def {* so no need to worry about a name clash. */ struct bpf_map ; +LIBBPF_API struct bpf_link *bpf_map__attach_struct_ops(struct bpf_map *map);
nit: can you please move it into the section with all the bpf_link and attach APIs?
Quoted 3 lines
tools/lib/bpf/libbpf.h +0
-0LIBBPF_API struct bpf_map *bpf_object__find_map_by_name (const struct bpf_object *obj,const char *name);
[...]
Re: [PATCH bpf-next v2 10/11] bpf: libbpf: Add STRUCT_OPS support
Quoted 8 lines · Andrii Nakryiko
tools/lib/bpf/libbpf.c int bpf_object__unload(struct bpf_object *obj) · line 5313
Quoted 5 lines
tools/lib/bpf/libbpf.c +3
-1-for (i =0 ; i < obj->nr_maps; i++) +for (i =0 ; i < obj->nr_maps; i++) {zclose (obj->maps[i].fd); +if (obj->maps[i].st_ops) +zfree (&obj->maps[i].st_ops->kern_vdata);any specific reason to deallocate only
kern_vdata? maybe just consolidate all the clean up inbpf_object__closeinstead?
I think it is the same as why the map fd is closed here. kern_vdata is allocated at load time, so it seems more logical to me to deallocate asap at unload time. For example, if user calls bpf_object__load_xattr() and one of the load steps fails, bpf_object__unload (but not bpf_object__close) is called before returning.
PATCH v2 11/11 bpf: Add bpf_dctcp example
In reply to Martin KaFai LauThis patch adds a bpf_dctcp example. It currently does not do no-ECN fallback but the same could be done through the cgrp2-bpf.
--- tools/testing/selftests/bpf/bpf_tcp_helpers.h | 228 ++++++++++++++++++ .../selftests/bpf/prog_tests/bpf_tcp_ca.c | 218 +++++++++++++++++ tools/testing/selftests/bpf/progs/bpf_dctcp.c | 210 ++++++++++++++++ 3 files changed, 656 insertions(+) create mode 100644 tools/testing/selftests/bpf/bpf_tcp_helpers.h create mode 100644 tools/testing/selftests/bpf/prog_tests/bpf_tcp_ca.c create mode 100644 tools/testing/selftests/bpf/progs/bpf_dctcp.c
tools/testing/selftests/bpf/bpf_tcp_helpers.h new file +228 -0
@@ -0,0 +1,228 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __BPF_TCP_HELPERS_H +#define __BPF_TCP_HELPERS_H + +#include <stdbool.h> +#include <linux/types.h> +#include <bpf_helpers.h> +#include <bpf_core_read.h> +#include "bpf_trace_helpers.h" + +#define BPF_TCP_OPS_0 (fname, ret_type, ...) BPF_TRACE_x(0, #fname"_sec", fname, ret_type, __VA_ARGS__) +#define BPF_TCP_OPS_1 (fname, ret_type, ...) BPF_TRACE_x(1, #fname"_sec", fname, ret_type, __VA_ARGS__) +#define BPF_TCP_OPS_2 (fname, ret_type, ...) BPF_TRACE_x(2, #fname"_sec", fname, ret_type, __VA_ARGS__) +#define BPF_TCP_OPS_3 (fname, ret_type, ...) BPF_TRACE_x(3, #fname"_sec", fname, ret_type, __VA_ARGS__) +#define BPF_TCP_OPS_4 (fname, ret_type, ...) BPF_TRACE_x(4, #fname"_sec", fname, ret_type, __VA_ARGS__) +#define BPF_TCP_OPS_5 (fname, ret_type, ...) BPF_TRACE_x(5, #fname"_sec", fname, ret_type, __VA_ARGS__) + +struct sock_common { +unsigned char skc_state; +} __attribute__((preserve_access_index)); + +struct sock { +struct sock_common __sk_common; +} __attribute__((preserve_access_index)); + +struct inet_sock { +struct sock sk; +} __attribute__((preserve_access_index)); + +struct inet_connection_sock { +struct inet_sock icsk_inet; +__u8 icsk_ca_state:6 , + icsk_ca_setsockopt:1 , + icsk_ca_dst_locked:1 ; +struct { +__u8 pending; + } icsk_ack; +__u64 icsk_ca_priv[104 /sizeof (__u64)]; +} __attribute__((preserve_access_index)); + +struct tcp_sock { +struct inet_connection_sock inet_conn; + +__u32 rcv_nxt; +__u32 snd_nxt; +__u32 snd_una; +__u8 ecn_flags; +__u32 delivered; +__u32 delivered_ce; +__u32 snd_cwnd; +__u32 snd_cwnd_cnt; +__u32 snd_cwnd_clamp; +__u32 snd_ssthresh; +__u8 syn_data:1 ,/* SYN includes data */ + syn_fastopen:1 ,/* SYN includes Fast Open option */ + syn_fastopen_exp:1 ,/* SYN includes Fast Open exp. option */ + syn_fastopen_ch:1 ,/* Active TFO re-enabling probe */ + syn_data_acked:1 ,/* data in SYN is acked by SYN-ACK */ + save_syn:1 ,/* Save headers of SYN packet */ + is_cwnd_limited:1 ,/* forward progress limited by snd_cwnd? */ + syn_smc:1 ;/* SYN includes SMC */ +__u32 max_packets_out; +__u32 lsndtime; +__u32 prior_cwnd; +} __attribute__((preserve_access_index)); + +static __always_inline struct inet_connection_sock *inet_csk(const struct sock *sk) +{ +return (struct inet_connection_sock *)sk; +} + +static __always_inline void *inet_csk_ca(const struct sock *sk) +{ +return (void *)inet_csk (sk)->icsk_ca_priv; +} + +static __always_inline struct tcp_sock *tcp_sk(const struct sock *sk) +{ +return (struct tcp_sock *)sk; +} + +static __always_inline bool before(__u32 seq1, __u32 seq2) +{ +return (__s32)(seq1-seq2) <0 ; +} +#define after (seq2, seq1) before(seq1, seq2) + +#define TCP_ECN_OK 1 +#define TCP_ECN_QUEUE_CWR 2 +#define TCP_ECN_DEMAND_CWR 4 +#define TCP_ECN_SEEN 8 + +enum inet_csk_ack_state_t { +ICSK_ACK_SCHED =1 , +ICSK_ACK_TIMER =2 , +ICSK_ACK_PUSHED =4 , +ICSK_ACK_PUSHED2 =8 , +ICSK_ACK_NOW =16 /* Send the next ACK immediately (once) */ +}; + +enum tcp_ca_event { +CA_EVENT_TX_START =0 , +CA_EVENT_CWND_RESTART =1 , +CA_EVENT_COMPLETE_CWR =2 , +CA_EVENT_LOSS =3 , +CA_EVENT_ECN_NO_CE =4 , +CA_EVENT_ECN_IS_CE =5 , +}; + +enum tcp_ca_state { + TCP_CA_Open =0 , + TCP_CA_Disorder =1 , +TCP_CA_CWR =2 , + TCP_CA_Recovery =3 , + TCP_CA_Loss =4 +}; + +struct ack_sample { +__u32 pkts_acked; +__s32 rtt_us; +__u32 in_flight; +} __attribute__((preserve_access_index)); + +struct rate_sample { +__u64 prior_mstamp;/* starting timestamp for interval */ +__u32 prior_delivered;/* tp->delivered at "prior_mstamp" */ +__s32 delivered;/* number of packets delivered over interval */ +long interval_us;/* time for tp->delivered to incr "delivered" */ +__u32 snd_interval_us;/* snd interval for delivered packets */ +__u32 rcv_interval_us;/* rcv interval for delivered packets */ +long rtt_us;/* RTT of last (S)ACKed packet (or -1) */ +int losses;/* number of packets marked lost upon ACK */ +__u32 acked_sacked;/* number of packets newly (S)ACKed upon ACK */ +__u32 prior_in_flight;/* in flight before this ACK */ +bool is_app_limited;/* is sample from packet with bubble in pipe? */ +bool is_retrans;/* is sample from retransmission? */ +bool is_ack_delayed;/* is this (likely) a delayed ACK? */ +} __attribute__((preserve_access_index)); + +#define TCP_CA_NAME_MAX 16 +#define TCP_CONG_NEEDS_ECN 0x2 + +struct tcp_congestion_ops { +__u32 flags; + +/* initialize private data (optional) */ +void (*init)(struct sock *sk); +/* cleanup private data (optional) */ +void (*release)(struct sock *sk); + +/* return slow start threshold (required) */ +__u32 (*ssthresh)(struct sock *sk); +/* do new cwnd calculation (required) */ +void (*cong_avoid)(struct sock *sk,__u32 ack,__u32 acked); +/* call before changing ca_state (optional) */ +void (*set_state)(struct sock *sk,__u8 new_state); +/* call when cwnd event occurs (optional) */ +void (*cwnd_event)(struct sock *sk,enum tcp_ca_event ev); +/* call when ack arrives (optional) */ +void (*in_ack_event)(struct sock *sk,__u32 flags); +/* new value of cwnd after loss (required) */ +__u32 (*undo_cwnd)(struct sock *sk); +/* hook for packet ack accounting (optional) */ +void (*pkts_acked)(struct sock *sk,const struct ack_sample *sample); +/* override sysctl_tcp_min_tso_segs */ +__u32 (*min_tso_segs)(struct sock *sk); +/* returns the multiplier used in tcp_sndbuf_expand (optional) */ +__u32 (*sndbuf_expand)(struct sock *sk); +/* call when packets are delivered to update cwnd and pacing rate, +* after all the ca_state processing. (optional) +*/ +void (*cong_control)(struct sock *sk,const struct rate_sample *rs); + +char name[TCP_CA_NAME_MAX ]; +}; + +#define min (a, b) ((a) < (b) ? (a) : (b)) +#define max (a, b) ((a) > (b) ? (a) : (b)) +#define min_not_zero (x, y) ({ \ + typeof(x) __x = (x); \ + typeof(y) __y = (y); \ + __x == 0 ? __y : ((__y == 0) ? __x : min(__x, __y)); }) + +static __always_inline __u32 tcp_slow_start(struct tcp_sock *tp, __u32 acked) +{ +__u32 cwnd =min (tp->snd_cwnd + acked, tp->snd_ssthresh); + + acked -= cwnd - tp->snd_cwnd; + tp->snd_cwnd =min (cwnd, tp->snd_cwnd_clamp); + +return acked; +} + +static __always_inline bool tcp_in_slow_start(const struct tcp_sock *tp) +{ +return tp->snd_cwnd < tp->snd_ssthresh; +} + +static __always_inline bool tcp_is_cwnd_limited(const struct sock *sk) +{ +const struct tcp_sock *tp =tcp_sk (sk); + +/* If in slow start, ensure cwnd grows to twice what was ACKed. */ +if (tcp_in_slow_start (tp)) +return tp->snd_cwnd <2 * tp->max_packets_out; + +return !!BPF_CORE_READ_BITFIELD (tp, is_cwnd_limited); +} + +static __always_inline void tcp_cong_avoid_ai(struct tcp_sock *tp, __u32 w, __u32 acked) +{ +/* If credits accumulated at a higher w, apply them gently now. */ +if (tp->snd_cwnd_cnt >= w) { + tp->snd_cwnd_cnt =0 ; + tp->snd_cwnd++; + } + + tp->snd_cwnd_cnt += acked; +if (tp->snd_cwnd_cnt >= w) { +__u32 delta = tp->snd_cwnd_cnt / w; + + tp->snd_cwnd_cnt -= delta * w; + tp->snd_cwnd += delta; + } + tp->snd_cwnd =min (tp->snd_cwnd, tp->snd_cwnd_clamp); +} + +#endif
tools/testing/selftests/bpf/prog_tests/bpf_tcp_ca.c new file +218 -0
218 lines not on this page · raw message
tools/testing/selftests/bpf/progs/bpf_dctcp.c new file +210 -0
210 lines not on this page · raw message
Signature · 1 line
2.17.1
Re: [PATCH bpf-next v2 11/11] bpf: Add bpf_dctcp example
Quoted 26 lines · Martin KaFai Lau
This patch adds a
bpf_dctcpexample. It currently does not do no-ECN fallback but the same could be done through the cgrp2-bpf.Signed-off-by Martin KaFai Lau <[email protected]>--- tools/testing/selftests/bpf/bpf_tcp_helpers.h | 228 ++++++++++++++++++ .../selftests/bpf/prog_tests/bpf_tcp_ca.c | 218 +++++++++++++++++ tools/testing/selftests/bpf/progs/bpf_dctcp.c | 210 ++++++++++++++++ 3 files changed, 656 insertions(+) create mode 100644 tools/testing/selftests/bpf/bpf_tcp_helpers.h create mode 100644 tools/testing/selftests/bpf/prog_tests/bpf_tcp_ca.c create mode 100644 tools/testing/selftests/bpf/progs/bpf_dctcp.ctools/testing/selftests/bpf/bpf_tcp_helpers.h new file +6
-0@@ -0,0 +1,228 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __BPF_TCP_HELPERS_H +#define __BPF_TCP_HELPERS_H + +#include <stdbool.h> +#include <linux/types.h>
tools/testing/selftests/bpf/bpf_tcp_helpers.h line 16
Quoted 10 lines
tools/testing/selftests/bpf/bpf_tcp_helpers.h new file +10
-0+#include <bpf_helpers.h> +#include <bpf_core_read.h> +#include "bpf_trace_helpers.h" + +#define BPF_TCP_OPS_0 (fname, ret_type, ...) BPF_TRACE_x(0, #fname"_sec", fname, ret_type, __VA_ARGS__) +#define BPF_TCP_OPS_1 (fname, ret_type, ...) BPF_TRACE_x(1, #fname"_sec", fname, ret_type, __VA_ARGS__) +#define BPF_TCP_OPS_2 (fname, ret_type, ...) BPF_TRACE_x(2, #fname"_sec", fname, ret_type, __VA_ARGS__) +#define BPF_TCP_OPS_3 (fname, ret_type, ...) BPF_TRACE_x(3, #fname"_sec", fname, ret_type, __VA_ARGS__) +#define BPF_TCP_OPS_4 (fname, ret_type, ...) BPF_TRACE_x(4, #fname"_sec", fname, ret_type, __VA_ARGS__) +#define BPF_TCP_OPS_5 (fname, ret_type, ...) BPF_TRACE_x(5, #fname"_sec", fname, ret_type, __VA_ARGS__)
Should we try to put those BPF programs into some section that would indicate they are used with struct opts? libbpf doesn't use or enforce that (even though it could to derive and enforce that they are STRUCT_OPS programs). So something like SEC("struct_ops/<ideally-operation-name-here>"). I think having this convention is very useful for consistency and to do a quick ELF dump and see what is where. WDYT?
Quoted 149 lines
tools/testing/selftests/bpf/bpf_tcp_helpers.h new file +149
-0+ +struct sock_common { +unsigned char skc_state; +} __attribute__((preserve_access_index)); + +struct sock { +struct sock_common __sk_common; +} __attribute__((preserve_access_index)); + +struct inet_sock { +struct sock sk; +} __attribute__((preserve_access_index)); + +struct inet_connection_sock { +struct inet_sock icsk_inet; +__u8 icsk_ca_state:6 , + icsk_ca_setsockopt:1 , + icsk_ca_dst_locked:1 ; +struct { +__u8 pending; + } icsk_ack; +__u64 icsk_ca_priv[104 /sizeof (__u64)]; +} __attribute__((preserve_access_index)); + +struct tcp_sock { +struct inet_connection_sock inet_conn; + +__u32 rcv_nxt; +__u32 snd_nxt; +__u32 snd_una; +__u8 ecn_flags; +__u32 delivered; +__u32 delivered_ce; +__u32 snd_cwnd; +__u32 snd_cwnd_cnt; +__u32 snd_cwnd_clamp; +__u32 snd_ssthresh; +__u8 syn_data:1 ,/* SYN includes data */ + syn_fastopen:1 ,/* SYN includes Fast Open option */ + syn_fastopen_exp:1 ,/* SYN includes Fast Open exp. option */ + syn_fastopen_ch:1 ,/* Active TFO re-enabling probe */ + syn_data_acked:1 ,/* data in SYN is acked by SYN-ACK */ + save_syn:1 ,/* Save headers of SYN packet */ + is_cwnd_limited:1 ,/* forward progress limited by snd_cwnd? */ + syn_smc:1 ;/* SYN includes SMC */ +__u32 max_packets_out; +__u32 lsndtime; +__u32 prior_cwnd; +} __attribute__((preserve_access_index)); + +static __always_inline struct inet_connection_sock *inet_csk(const struct sock *sk) +{ +return (struct inet_connection_sock *)sk; +} + +static __always_inline void *inet_csk_ca(const struct sock *sk) +{ +return (void *)inet_csk (sk)->icsk_ca_priv; +} + +static __always_inline struct tcp_sock *tcp_sk(const struct sock *sk) +{ +return (struct tcp_sock *)sk; +} + +static __always_inline bool before(__u32 seq1, __u32 seq2) +{ +return (__s32)(seq1-seq2) <0 ; +} +#define after (seq2, seq1) before(seq1, seq2) + +#define TCP_ECN_OK 1 +#define TCP_ECN_QUEUE_CWR 2 +#define TCP_ECN_DEMAND_CWR 4 +#define TCP_ECN_SEEN 8 + +enum inet_csk_ack_state_t { +ICSK_ACK_SCHED =1 , +ICSK_ACK_TIMER =2 , +ICSK_ACK_PUSHED =4 , +ICSK_ACK_PUSHED2 =8 , +ICSK_ACK_NOW =16 /* Send the next ACK immediately (once) */ +}; + +enum tcp_ca_event { +CA_EVENT_TX_START =0 , +CA_EVENT_CWND_RESTART =1 , +CA_EVENT_COMPLETE_CWR =2 , +CA_EVENT_LOSS =3 , +CA_EVENT_ECN_NO_CE =4 , +CA_EVENT_ECN_IS_CE =5 , +}; + +enum tcp_ca_state { + TCP_CA_Open =0 , + TCP_CA_Disorder =1 , +TCP_CA_CWR =2 , + TCP_CA_Recovery =3 , + TCP_CA_Loss =4 +}; + +struct ack_sample { +__u32 pkts_acked; +__s32 rtt_us; +__u32 in_flight; +} __attribute__((preserve_access_index)); + +struct rate_sample { +__u64 prior_mstamp;/* starting timestamp for interval */ +__u32 prior_delivered;/* tp->delivered at "prior_mstamp" */ +__s32 delivered;/* number of packets delivered over interval */ +long interval_us;/* time for tp->delivered to incr "delivered" */ +__u32 snd_interval_us;/* snd interval for delivered packets */ +__u32 rcv_interval_us;/* rcv interval for delivered packets */ +long rtt_us;/* RTT of last (S)ACKed packet (or -1) */ +int losses;/* number of packets marked lost upon ACK */ +__u32 acked_sacked;/* number of packets newly (S)ACKed upon ACK */ +__u32 prior_in_flight;/* in flight before this ACK */ +bool is_app_limited;/* is sample from packet with bubble in pipe? */ +bool is_retrans;/* is sample from retransmission? */ +bool is_ack_delayed;/* is this (likely) a delayed ACK? */ +} __attribute__((preserve_access_index)); + +#define TCP_CA_NAME_MAX 16 +#define TCP_CONG_NEEDS_ECN 0x2 + +struct tcp_congestion_ops { +__u32 flags; + +/* initialize private data (optional) */ +void (*init)(struct sock *sk); +/* cleanup private data (optional) */ +void (*release)(struct sock *sk); + +/* return slow start threshold (required) */ +__u32 (*ssthresh)(struct sock *sk); +/* do new cwnd calculation (required) */ +void (*cong_avoid)(struct sock *sk,__u32 ack,__u32 acked); +/* call before changing ca_state (optional) */ +void (*set_state)(struct sock *sk,__u8 new_state); +/* call when cwnd event occurs (optional) */ +void (*cwnd_event)(struct sock *sk,enum tcp_ca_event ev); +/* call when ack arrives (optional) */ +void (*in_ack_event)(struct sock *sk,__u32 flags); +/* new value of cwnd after loss (required) */ +__u32 (*undo_cwnd)(struct sock *sk); +/* hook for packet ack accounting (optional) */ +void (*pkts_acked)(struct sock *sk,const struct ack_sample *sample); +/* override sysctl_tcp_min_tso_segs */
tools/testing/selftests/bpf/bpf_tcp_helpers.h line 175
Quoted 10 lines
tools/testing/selftests/bpf/bpf_tcp_helpers.h new file +10
-0+__u32 (*min_tso_segs)(struct sock *sk); +/* returns the multiplier used in tcp_sndbuf_expand (optional) */ +__u32 (*sndbuf_expand)(struct sock *sk); +/* call when packets are delivered to update cwnd and pacing rate, +* after all the ca_state processing. (optional) +*/ +void (*cong_control)(struct sock *sk,const struct rate_sample *rs); + +char name[TCP_CA_NAME_MAX ]; +};
Can all of these types come from vmlinux.h instead of being duplicated here?
Quoted 8 lines
tools/testing/selftests/bpf/bpf_tcp_helpers.h new file +8
-0+ +#define min (a, b) ((a) < (b) ? (a) : (b)) +#define max (a, b) ((a) > (b) ? (a) : (b)) +#define min_not_zero (x, y) ({ \ + typeof(x) __x = (x); \ + typeof(y) __y = (y); \ + __x == 0 ? __y : ((__y == 0) ? __x : min(__x, __y)); }) +
[...]
Quoted 16 lines
tools/testing/selftests/bpf/bpf_tcp_helpers.h new file +16
-0+static struct bpf_object *load (const char *filename,const char *map_name, +struct bpf_link **link) +{ +struct bpf_object *obj; +struct bpf_map *map; +struct bpf_link *l; +int err; + + obj =bpf_object__open (filename); +if (CHECK (IS_ERR (obj),"bpf_obj__open_file" ,"obj:%ld\n" , +PTR_ERR (obj))) +return obj; + + err =bpf_object__load (obj); +if (CHECK (err,"bpf_object__load" ,"err:%d\n" , err)) { +bpf_object__close (obj);
tools/testing/selftests/bpf/bpf_tcp_helpers.h
Quoted 10 lines
tools/testing/selftests/bpf/bpf_tcp_helpers.h new file +10
-0+return ERR_PTR (err); + } + + map =bpf_object__find_map_by_name (obj, map_name); +if (CHECK (!map,"bpf_object__find_map_by_name" ,"%s not found\n" , + map_name)) { +bpf_object__close (obj); +return ERR_PTR (-ENOENT ); + } +
use skeleton instead?
Quoted 33 lines
tools/testing/selftests/bpf/bpf_tcp_helpers.h new file +33
-0+ l =bpf_map__attach_struct_ops (map); +if (CHECK (IS_ERR (l),"bpf_struct_ops_map__attach" ,"err:%ld\n" , +PTR_ERR (l))) { +bpf_object__close (obj); +return (void *)l; + } + + *link = l; + +return obj; +} + +static void test_dctcp (void ) +{ +struct bpf_object *obj; +/* compiler warning... */ +struct bpf_link *link =NULL ; + + obj =load ("bpf_dctcp.o" ,"dctcp" , &link); +if (IS_ERR (obj)) +return ; + +do_test ("bpf_dctcp" ); + +bpf_link__destroy (link); +bpf_object__close (obj); +} + +void test_bpf_tcp_ca (void ) +{ +if (test__start_subtest ("dctcp" )) +test_dctcp (); +}
[...]
Re: [PATCH bpf-next v2 11/11] bpf: Add bpf_dctcp example
Quoted 38 lines · Andrii Nakryiko
Quoted 26 lines · Martin KaFai Lau
This patch adds a
bpf_dctcpexample. It currently does not do no-ECN fallback but the same could be done through the cgrp2-bpf.Signed-off-by Martin KaFai Lau <[email protected]>--- tools/testing/selftests/bpf/bpf_tcp_helpers.h | 228 ++++++++++++++++++ .../selftests/bpf/prog_tests/bpf_tcp_ca.c | 218 +++++++++++++++++ tools/testing/selftests/bpf/progs/bpf_dctcp.c | 210 ++++++++++++++++ 3 files changed, 656 insertions(+) create mode 100644 tools/testing/selftests/bpf/bpf_tcp_helpers.h create mode 100644 tools/testing/selftests/bpf/prog_tests/bpf_tcp_ca.c create mode 100644 tools/testing/selftests/bpf/progs/bpf_dctcp.ctools/testing/selftests/bpf/bpf_tcp_helpers.h new file +6
-0@@ -0,0 +1,228 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __BPF_TCP_HELPERS_H +#define __BPF_TCP_HELPERS_H + +#include <stdbool.h> +#include <linux/types.h> tools/testing/selftests/bpf/bpf_tcp_helpers.h line 16
Quoted 10 lines
tools/testing/selftests/bpf/bpf_tcp_helpers.h new file +10
-0+#include <bpf_helpers.h> +#include <bpf_core_read.h> +#include "bpf_trace_helpers.h" + +#define BPF_TCP_OPS_0 (fname, ret_type, ...) BPF_TRACE_x(0, #fname"_sec", fname, ret_type, __VA_ARGS__) +#define BPF_TCP_OPS_1 (fname, ret_type, ...) BPF_TRACE_x(1, #fname"_sec", fname, ret_type, __VA_ARGS__) +#define BPF_TCP_OPS_2 (fname, ret_type, ...) BPF_TRACE_x(2, #fname"_sec", fname, ret_type, __VA_ARGS__) +#define BPF_TCP_OPS_3 (fname, ret_type, ...) BPF_TRACE_x(3, #fname"_sec", fname, ret_type, __VA_ARGS__) +#define BPF_TCP_OPS_4 (fname, ret_type, ...) BPF_TRACE_x(4, #fname"_sec", fname, ret_type, __VA_ARGS__) +#define BPF_TCP_OPS_5 (fname, ret_type, ...) BPF_TRACE_x(5, #fname"_sec", fname, ret_type, __VA_ARGS__)
Quoted 7 lines
Should we try to put those BPF programs into some section that would indicate they are used with struct opts? libbpf doesn't use or enforce that (even though it could to derive and enforce that they are
STRUCT_OPSprograms). So something likeSEC("struct_ops/<ideally-operation-name-here>"). I think having this convention is very useful for consistency and to do a quick ELF dump and see what is where. WDYT?
I did not use it here because I don't want any misperception that it is a required convention by libbpf.
Sure, I can prefix it here and comment that it is just a convention but not a libbpf's requirement.
Quoted 154 lines
Quoted 154 lines
tools/testing/selftests/bpf/bpf_tcp_helpers.h new file +153
-0+ +struct sock_common { +unsigned char skc_state; +} __attribute__((preserve_access_index)); + +struct sock { +struct sock_common __sk_common; +} __attribute__((preserve_access_index)); + +struct inet_sock { +struct sock sk; +} __attribute__((preserve_access_index)); + +struct inet_connection_sock { +struct inet_sock icsk_inet; +__u8 icsk_ca_state:6 , + icsk_ca_setsockopt:1 , + icsk_ca_dst_locked:1 ; +struct { +__u8 pending; + } icsk_ack; +__u64 icsk_ca_priv[104 /sizeof (__u64)]; +} __attribute__((preserve_access_index)); + +struct tcp_sock { +struct inet_connection_sock inet_conn; + +__u32 rcv_nxt; +__u32 snd_nxt; +__u32 snd_una; +__u8 ecn_flags; +__u32 delivered; +__u32 delivered_ce; +__u32 snd_cwnd; +__u32 snd_cwnd_cnt; +__u32 snd_cwnd_clamp; +__u32 snd_ssthresh; +__u8 syn_data:1 ,/* SYN includes data */ + syn_fastopen:1 ,/* SYN includes Fast Open option */ + syn_fastopen_exp:1 ,/* SYN includes Fast Open exp. option */ + syn_fastopen_ch:1 ,/* Active TFO re-enabling probe */ + syn_data_acked:1 ,/* data in SYN is acked by SYN-ACK */ + save_syn:1 ,/* Save headers of SYN packet */ + is_cwnd_limited:1 ,/* forward progress limited by snd_cwnd? */ + syn_smc:1 ;/* SYN includes SMC */ +__u32 max_packets_out; +__u32 lsndtime; +__u32 prior_cwnd; +} __attribute__((preserve_access_index)); + +static __always_inline struct inet_connection_sock *inet_csk(const struct sock *sk) +{ +return (struct inet_connection_sock *)sk; +} + +static __always_inline void *inet_csk_ca(const struct sock *sk) +{ +return (void *)inet_csk (sk)->icsk_ca_priv; +} + +static __always_inline struct tcp_sock *tcp_sk(const struct sock *sk) +{ +return (struct tcp_sock *)sk; +} + +static __always_inline bool before(__u32 seq1, __u32 seq2) +{ +return (__s32)(seq1-seq2) <0 ; +} +#define after (seq2, seq1) before(seq1, seq2) + +#define TCP_ECN_OK 1 +#define TCP_ECN_QUEUE_CWR 2 +#define TCP_ECN_DEMAND_CWR 4 +#define TCP_ECN_SEEN 8 + +enum inet_csk_ack_state_t { +ICSK_ACK_SCHED =1 , +ICSK_ACK_TIMER =2 , +ICSK_ACK_PUSHED =4 , +ICSK_ACK_PUSHED2 =8 , +ICSK_ACK_NOW =16 /* Send the next ACK immediately (once) */ +}; + +enum tcp_ca_event { +CA_EVENT_TX_START =0 , +CA_EVENT_CWND_RESTART =1 , +CA_EVENT_COMPLETE_CWR =2 , +CA_EVENT_LOSS =3 , +CA_EVENT_ECN_NO_CE =4 , +CA_EVENT_ECN_IS_CE =5 , +}; + +enum tcp_ca_state { + TCP_CA_Open =0 , + TCP_CA_Disorder =1 , +TCP_CA_CWR =2 , + TCP_CA_Recovery =3 , + TCP_CA_Loss =4 +}; + +struct ack_sample { +__u32 pkts_acked; +__s32 rtt_us; +__u32 in_flight; +} __attribute__((preserve_access_index)); + +struct rate_sample { +__u64 prior_mstamp;/* starting timestamp for interval */ +__u32 prior_delivered;/* tp->delivered at "prior_mstamp" */ +__s32 delivered;/* number of packets delivered over interval */ +long interval_us;/* time for tp->delivered to incr "delivered" */ +__u32 snd_interval_us;/* snd interval for delivered packets */ +__u32 rcv_interval_us;/* rcv interval for delivered packets */ +long rtt_us;/* RTT of last (S)ACKed packet (or -1) */ +int losses;/* number of packets marked lost upon ACK */ +__u32 acked_sacked;/* number of packets newly (S)ACKed upon ACK */ +__u32 prior_in_flight;/* in flight before this ACK */ +bool is_app_limited;/* is sample from packet with bubble in pipe? */ +bool is_retrans;/* is sample from retransmission? */ +bool is_ack_delayed;/* is this (likely) a delayed ACK? */ +} __attribute__((preserve_access_index)); + +#define TCP_CA_NAME_MAX 16 +#define TCP_CONG_NEEDS_ECN 0x2 + +struct tcp_congestion_ops { +__u32 flags; + +/* initialize private data (optional) */ +void (*init)(struct sock *sk); +/* cleanup private data (optional) */ +void (*release)(struct sock *sk); + +/* return slow start threshold (required) */ +__u32 (*ssthresh)(struct sock *sk); +/* do new cwnd calculation (required) */ +void (*cong_avoid)(struct sock *sk,__u32 ack,__u32 acked); +/* call before changing ca_state (optional) */ +void (*set_state)(struct sock *sk,__u8 new_state); +/* call when cwnd event occurs (optional) */ +void (*cwnd_event)(struct sock *sk,enum tcp_ca_event ev); +/* call when ack arrives (optional) */ +void (*in_ack_event)(struct sock *sk,__u32 flags); +/* new value of cwnd after loss (required) */ +__u32 (*undo_cwnd)(struct sock *sk); +/* hook for packet ack accounting (optional) */ +void (*pkts_acked)(struct sock *sk,const struct ack_sample *sample); +/* override sysctl_tcp_min_tso_segs */ +__u32 (*min_tso_segs)(struct sock *sk); +/* returns the multiplier used in tcp_sndbuf_expand (optional) */ +__u32 (*sndbuf_expand)(struct sock *sk); +/* call when packets are delivered to update cwnd and pacing rate,
Quoted 8 lines
tools/testing/selftests/bpf/bpf_tcp_helpers.h line 175
Quoted 6 lines
tools/testing/selftests/bpf/bpf_tcp_helpers.h new file +6
-0+* after all the ca_state processing. (optional) +*/ +void (*cong_control)(struct sock *sk,const struct rate_sample *rs); + +char name[TCP_CA_NAME_MAX ]; +};Can all of these types come from
vmlinux.hinstead of being duplicated here?
It can but I prefer leaving it as is in bpf_tcp_helpers.h like another existing test in kfree_skb.c. Without directly using the same struct in vmlinux.h, I think it is a good test for libbpf.
That remind me to shuffle the member ordering a little in tcp_congestion_ops here.
Quoted 32 lines
Quoted 9 lines
tools/testing/selftests/bpf/bpf_tcp_helpers.h new file +8
-0+ +#define min (a, b) ((a) < (b) ? (a) : (b)) +#define max (a, b) ((a) > (b) ? (a) : (b)) +#define min_not_zero (x, y) ({ \ + typeof(x) __x = (x); \ + typeof(y) __y = (y); \ + __x == 0 ? __y : ((__y == 0) ? __x : min(__x, __y)); }) +[...]
Quoted 20 lines
tools/testing/selftests/bpf/bpf_tcp_helpers.h new file +20
-0+static struct bpf_object *load (const char *filename,const char *map_name, +struct bpf_link **link) +{ +struct bpf_object *obj; +struct bpf_map *map; +struct bpf_link *l; +int err; + + obj =bpf_object__open (filename); +if (CHECK (IS_ERR (obj),"bpf_obj__open_file" ,"obj:%ld\n" , +PTR_ERR (obj))) +return obj; + + err =bpf_object__load (obj); +if (CHECK (err,"bpf_object__load" ,"err:%d\n" , err)) { +bpf_object__close (obj); +return ERR_PTR (err); + } + + map =bpf_object__find_map_by_name (obj, map_name);
Quoted 8 lines
tools/testing/selftests/bpf/bpf_tcp_helpers.h
Quoted 6 lines
tools/testing/selftests/bpf/bpf_tcp_helpers.h new file +6
-0+if (CHECK (!map,"bpf_object__find_map_by_name" ,"%s not found\n" , + map_name)) { +bpf_object__close (obj); +return ERR_PTR (-ENOENT ); + } +use skeleton instead?
Will give it a spin.
Quoted 13 lines
Quoted 13 lines
tools/testing/selftests/bpf/bpf_tcp_helpers.h new file +12
-0+ l =bpf_map__attach_struct_ops (map); +if (CHECK (IS_ERR (l),"bpf_struct_ops_map__attach" ,"err:%ld\n" , +PTR_ERR (l))) { +bpf_object__close (obj); +return (void *)l; + } + + *link = l; + +return obj; +} +
Re: [PATCH bpf-next v2 11/11] bpf: Add bpf_dctcp example
Quoted 47 lines · Martin Lau
Quoted 38 lines · Andrii Nakryiko
Quoted 26 lines · Martin KaFai Lau
This patch adds a
bpf_dctcpexample. It currently does not do no-ECN fallback but the same could be done through the cgrp2-bpf.Signed-off-by Martin KaFai Lau <[email protected]>--- tools/testing/selftests/bpf/bpf_tcp_helpers.h | 228 ++++++++++++++++++ .../selftests/bpf/prog_tests/bpf_tcp_ca.c | 218 +++++++++++++++++ tools/testing/selftests/bpf/progs/bpf_dctcp.c | 210 ++++++++++++++++ 3 files changed, 656 insertions(+) create mode 100644 tools/testing/selftests/bpf/bpf_tcp_helpers.h create mode 100644 tools/testing/selftests/bpf/prog_tests/bpf_tcp_ca.c create mode 100644 tools/testing/selftests/bpf/progs/bpf_dctcp.ctools/testing/selftests/bpf/bpf_tcp_helpers.h new file +6
-0@@ -0,0 +1,228 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __BPF_TCP_HELPERS_H +#define __BPF_TCP_HELPERS_H + +#include <stdbool.h> +#include <linux/types.h> tools/testing/selftests/bpf/bpf_tcp_helpers.h line 16
Quoted 10 lines
tools/testing/selftests/bpf/bpf_tcp_helpers.h new file +10
-0+#include <bpf_helpers.h> +#include <bpf_core_read.h> +#include "bpf_trace_helpers.h" + +#define BPF_TCP_OPS_0 (fname, ret_type, ...) BPF_TRACE_x(0, #fname"_sec", fname, ret_type, __VA_ARGS__) +#define BPF_TCP_OPS_1 (fname, ret_type, ...) BPF_TRACE_x(1, #fname"_sec", fname, ret_type, __VA_ARGS__) +#define BPF_TCP_OPS_2 (fname, ret_type, ...) BPF_TRACE_x(2, #fname"_sec", fname, ret_type, __VA_ARGS__) +#define BPF_TCP_OPS_3 (fname, ret_type, ...) BPF_TRACE_x(3, #fname"_sec", fname, ret_type, __VA_ARGS__) +#define BPF_TCP_OPS_4 (fname, ret_type, ...) BPF_TRACE_x(4, #fname"_sec", fname, ret_type, __VA_ARGS__) +#define BPF_TCP_OPS_5 (fname, ret_type, ...) BPF_TRACE_x(5, #fname"_sec", fname, ret_type, __VA_ARGS__)Quoted 7 lines
Should we try to put those BPF programs into some section that would indicate they are used with struct opts? libbpf doesn't use or enforce that (even though it could to derive and enforce that they are
STRUCT_OPSprograms). So something likeSEC("struct_ops/<ideally-operation-name-here>"). I think having this convention is very useful for consistency and to do a quick ELF dump and see what is where. WDYT?
Quoted 5 lines
I did not use it here because I don't want any misperception that it is a required convention by libbpf.
Sure, I can prefix it here and comment that it is just a convention but not a libbpf's requirement.
Well, we can actually make it a requirement of sorts. Currently your code expects that BPF program's type is UNSPEC and then it sets it to STRUCT_OPS. Alternatively we can say that any BPF program in SEC("struct_ops/<whatever>") will be automatically assigned STRUCT_OPTS BPF program type (which is done generically in bpf_object__open()), and then as .struct_ops section is parsed, all those programs will be "assembled" by the code you added into a struct_ops map.
It's a requirement "of sorts", because even if user doesn't do that, stuff will still work, if user manually will call bpf_program__set_struct_ops(prog). Which actually reminds me that it would be good to add bpf_program__set_struct_ops() and bpf_program__is_struct_ops() APIs for completeness, similarly to how KP's LSM patch set does.
BTW, libbpf will emit debug message for every single BPF program it doesn't recognize section for, so it is still nice to have it be something more or less standardized and recognizable by libbpf.
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
tools/testing/selftests/bpf/bpf_tcp_helpers.h new file +1
-0+
[...]
Quoted 7 lines
Quoted 2 lines
Can all of these types come from
vmlinux.hinstead of being duplicated here?It can but I prefer leaving it as is in
bpf_tcp_helpers.hlike another existing test inkfree_skb.c. Without directly using the same struct invmlinux.h, I think it is a good test for libbpf.
That remind me to shuffle the member ordering a little intcp_congestion_opshere.
Sure no problem. When I looked at this it was a bit discouraging on how much types I'd need to duplicate, but surely we don't want to make an impression that vmlinux.h is the only way to achieve this.
Quoted 56 lines
Quoted 33 lines
Quoted 10 lines
tools/testing/selftests/bpf/bpf_tcp_helpers.h new file +8
-0+ +#define min (a, b) ((a) < (b) ? (a) : (b)) +#define max (a, b) ((a) > (b) ? (a) : (b)) +#define min_not_zero (x, y) ({ \ + typeof(x) __x = (x); \ + typeof(y) __y = (y); \ + __x == 0 ? __y : ((__y == 0) ? __x : min(__x, __y)); }) +[...]
Quoted 20 lines
tools/testing/selftests/bpf/bpf_tcp_helpers.h new file +20
-0+static struct bpf_object *load (const char *filename,const char *map_name, +struct bpf_link **link) +{ +struct bpf_object *obj; +struct bpf_map *map; +struct bpf_link *l; +int err; + + obj =bpf_object__open (filename); +if (CHECK (IS_ERR (obj),"bpf_obj__open_file" ,"obj:%ld\n" , +PTR_ERR (obj))) +return obj; + + err =bpf_object__load (obj); +if (CHECK (err,"bpf_object__load" ,"err:%d\n" , err)) { +bpf_object__close (obj); +return ERR_PTR (err); + } + + map =bpf_object__find_map_by_name (obj, map_name);Quoted 8 lines
tools/testing/selftests/bpf/bpf_tcp_helpers.h
Quoted 6 lines
tools/testing/selftests/bpf/bpf_tcp_helpers.h new file +6
-0+if (CHECK (!map,"bpf_object__find_map_by_name" ,"%s not found\n" , + map_name)) { +bpf_object__close (obj); +return ERR_PTR (-ENOENT ); + } +use skeleton instead?
Will give it a spin.
Quoted 13 lines
Quoted 13 lines
tools/testing/selftests/bpf/bpf_tcp_helpers.h new file +12
-0+ l =bpf_map__attach_struct_ops (map); +if (CHECK (IS_ERR (l),"bpf_struct_ops_map__attach" ,"err:%ld\n" , +PTR_ERR (l))) { +bpf_object__close (obj); +return (void *)l; + } + + *link = l; + +return obj; +} +
Re: [PATCH bpf-next v2 11/11] bpf: Add bpf_dctcp example
Quoted 10 lines · Andrii Nakryiko
Quoted 6 lines
Quoted 1 line
Can all of these types come from
vmlinux.hinstead of being duplicated here?It can but I prefer leaving it as is in
bpf_tcp_helpers.hlike another existing test inkfree_skb.c. Without directly using the same struct invmlinux.h, I think it is a good test for libbpf.
That remind me to shuffle the member ordering a little intcp_congestion_opshere.Sure no problem. When I looked at this it was a bit discouraging on how much types I'd need to duplicate, but surely we don't want to make an impression that
vmlinux.his the only way to achieve this.
IMO, it is a very compact set of fields that work for both dctcp and cubic. Also, duplication is not a concern here. The deviation between the kernel and bpf_tcp_helpers.h is not a problem with CO-RE.
Re: [PATCH bpf-next v2 11/11] bpf: Add bpf_dctcp example
In reply to Andrii NakryikoQuoted 54 lines · Andrii Nakryiko
Quoted 47 lines · Martin Lau
Quoted 38 lines · Andrii Nakryiko
Quoted 26 lines · Martin KaFai Lau
This patch adds a
bpf_dctcpexample. It currently does not do no-ECN fallback but the same could be done through the cgrp2-bpf.Signed-off-by Martin KaFai Lau <[email protected]>--- tools/testing/selftests/bpf/bpf_tcp_helpers.h | 228 ++++++++++++++++++ .../selftests/bpf/prog_tests/bpf_tcp_ca.c | 218 +++++++++++++++++ tools/testing/selftests/bpf/progs/bpf_dctcp.c | 210 ++++++++++++++++ 3 files changed, 656 insertions(+) create mode 100644 tools/testing/selftests/bpf/bpf_tcp_helpers.h create mode 100644 tools/testing/selftests/bpf/prog_tests/bpf_tcp_ca.c create mode 100644 tools/testing/selftests/bpf/progs/bpf_dctcp.ctools/testing/selftests/bpf/bpf_tcp_helpers.h new file +6
-0@@ -0,0 +1,228 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __BPF_TCP_HELPERS_H +#define __BPF_TCP_HELPERS_H + +#include <stdbool.h> +#include <linux/types.h> tools/testing/selftests/bpf/bpf_tcp_helpers.h line 16
Quoted 10 lines
tools/testing/selftests/bpf/bpf_tcp_helpers.h new file +10
-0+#include <bpf_helpers.h> +#include <bpf_core_read.h> +#include "bpf_trace_helpers.h" + +#define BPF_TCP_OPS_0 (fname, ret_type, ...) BPF_TRACE_x(0, #fname"_sec", fname, ret_type, __VA_ARGS__) +#define BPF_TCP_OPS_1 (fname, ret_type, ...) BPF_TRACE_x(1, #fname"_sec", fname, ret_type, __VA_ARGS__) +#define BPF_TCP_OPS_2 (fname, ret_type, ...) BPF_TRACE_x(2, #fname"_sec", fname, ret_type, __VA_ARGS__) +#define BPF_TCP_OPS_3 (fname, ret_type, ...) BPF_TRACE_x(3, #fname"_sec", fname, ret_type, __VA_ARGS__) +#define BPF_TCP_OPS_4 (fname, ret_type, ...) BPF_TRACE_x(4, #fname"_sec", fname, ret_type, __VA_ARGS__) +#define BPF_TCP_OPS_5 (fname, ret_type, ...) BPF_TRACE_x(5, #fname"_sec", fname, ret_type, __VA_ARGS__)Quoted 7 lines
Should we try to put those BPF programs into some section that would indicate they are used with struct opts? libbpf doesn't use or enforce that (even though it could to derive and enforce that they are
STRUCT_OPSprograms). So something likeSEC("struct_ops/<ideally-operation-name-here>"). I think having this convention is very useful for consistency and to do a quick ELF dump and see what is where. WDYT?Quoted 5 lines
I did not use it here because I don't want any misperception that it is a required convention by libbpf.
Sure, I can prefix it here and comment that it is just a convention but not a libbpf's requirement.
Quoted 8 lines
Well, we can actually make it a requirement of sorts. Currently your code expects that BPF program's type is UNSPEC and then it sets it to
STRUCT_OPS. Alternatively we can say that any BPF program inSEC("struct_ops/<whatever>")will be automatically assignedSTRUCT_OPTSBPF program type (which is done generically inbpf_object__open()), and then as .struct_ops section is parsed, all those programs will be "assembled" by the code you added into astruct_opsmap.
Setting BPF_PROG_TYPE_STRUCT_OPS can be done automatically at open phase (during collect_reloc time). I will make this change.
Quoted 5 lines
It's a requirement "of sorts", because even if user doesn't do that, stuff will still work, if user manually will call
bpf_program__set_struct_ops(prog). Which actually reminds me that it would be good to addbpf_program__set_struct_ops()and
Although there is BPF_PROG_TYPE_FNS macro,
I don't see moving bpf_prog__set_struct_ops(prog) to LIBBPF_API is useful while actually may cause confusion and error. How could __set_struct_ops() a prog to struct_ops prog_type help a program, which is not used in SEC(".struct_ops"), to be loaded successfully as a struct_ops prog?
Assigning a bpf_prog to a function ptr under the SEC(".struct_ops") is the only way for a program to be successfully loaded as struct_ops prog type. Extra way to allow a prog to be changed to struct_ops prog_type is either useless or redundant.
If it is really necessary to have __set_struct_ops() as a API for completeness, it can be added...
Quoted 1 line
bpf_program__is_struct_ops()APIs for completeness, similarly to how
is_struct_ops() makes sense.
Quoted 3 lines
BTW, libbpf will emit debug message for every single BPF program it doesn't recognize section for, so it is still nice to have it be something more or less standardized and recognizable by libbpf.
I can make this debug (not error) message go away too after setting the BPF_PROG_TYPE_STRUCT_OPS automatically at open time.
Re: [PATCH bpf-next v2 11/11] bpf: Add bpf_dctcp example
Quoted 64 lines · Martin Lau
Quoted 54 lines · Andrii Nakryiko
Quoted 47 lines · Martin Lau
Quoted 38 lines · Andrii Nakryiko
Quoted 26 lines · Martin KaFai Lau
This patch adds a
bpf_dctcpexample. It currently does not do no-ECN fallback but the same could be done through the cgrp2-bpf.Signed-off-by Martin KaFai Lau <[email protected]>--- tools/testing/selftests/bpf/bpf_tcp_helpers.h | 228 ++++++++++++++++++ .../selftests/bpf/prog_tests/bpf_tcp_ca.c | 218 +++++++++++++++++ tools/testing/selftests/bpf/progs/bpf_dctcp.c | 210 ++++++++++++++++ 3 files changed, 656 insertions(+) create mode 100644 tools/testing/selftests/bpf/bpf_tcp_helpers.h create mode 100644 tools/testing/selftests/bpf/prog_tests/bpf_tcp_ca.c create mode 100644 tools/testing/selftests/bpf/progs/bpf_dctcp.ctools/testing/selftests/bpf/bpf_tcp_helpers.h new file +6
-0@@ -0,0 +1,228 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __BPF_TCP_HELPERS_H +#define __BPF_TCP_HELPERS_H + +#include <stdbool.h> +#include <linux/types.h> tools/testing/selftests/bpf/bpf_tcp_helpers.h line 16
Quoted 10 lines
tools/testing/selftests/bpf/bpf_tcp_helpers.h new file +10
-0+#include <bpf_helpers.h> +#include <bpf_core_read.h> +#include "bpf_trace_helpers.h" + +#define BPF_TCP_OPS_0 (fname, ret_type, ...) BPF_TRACE_x(0, #fname"_sec", fname, ret_type, __VA_ARGS__) +#define BPF_TCP_OPS_1 (fname, ret_type, ...) BPF_TRACE_x(1, #fname"_sec", fname, ret_type, __VA_ARGS__) +#define BPF_TCP_OPS_2 (fname, ret_type, ...) BPF_TRACE_x(2, #fname"_sec", fname, ret_type, __VA_ARGS__) +#define BPF_TCP_OPS_3 (fname, ret_type, ...) BPF_TRACE_x(3, #fname"_sec", fname, ret_type, __VA_ARGS__) +#define BPF_TCP_OPS_4 (fname, ret_type, ...) BPF_TRACE_x(4, #fname"_sec", fname, ret_type, __VA_ARGS__) +#define BPF_TCP_OPS_5 (fname, ret_type, ...) BPF_TRACE_x(5, #fname"_sec", fname, ret_type, __VA_ARGS__)Quoted 7 lines
Should we try to put those BPF programs into some section that would indicate they are used with struct opts? libbpf doesn't use or enforce that (even though it could to derive and enforce that they are
STRUCT_OPSprograms). So something likeSEC("struct_ops/<ideally-operation-name-here>"). I think having this convention is very useful for consistency and to do a quick ELF dump and see what is where. WDYT?Quoted 5 lines
I did not use it here because I don't want any misperception that it is a required convention by libbpf.
Sure, I can prefix it here and comment that it is just a convention but not a libbpf's requirement.
Quoted 8 lines
Well, we can actually make it a requirement of sorts. Currently your code expects that BPF program's type is UNSPEC and then it sets it to
STRUCT_OPS. Alternatively we can say that any BPF program inSEC("struct_ops/<whatever>")will be automatically assignedSTRUCT_OPTSBPF program type (which is done generically inbpf_object__open()), and then as .struct_ops section is parsed, all those programs will be "assembled" by the code you added into astruct_opsmap.
Quoted 3 lines
Setting
BPF_PROG_TYPE_STRUCT_OPScan be done automatically at open phase (duringcollect_reloctime). I will make this change.
Can you please extend exiting logic in __bpf_object__open() to do this? See how libbpf_prog_type_by_name() is used for that.
Quoted 11 lines
Quoted 5 lines
It's a requirement "of sorts", because even if user doesn't do that, stuff will still work, if user manually will call
bpf_program__set_struct_ops(prog). Which actually reminds me that it would be good to addbpf_program__set_struct_ops()andAlthough there is
BPF_PROG_TYPE_FNSmacro,
I don't see movingbpf_prog__set_struct_ops(prog)toLIBBPF_APIis useful while actually may cause confusion and error. How could__set_struct_ops()a prog tostruct_opsprog_typehelp a program, which is not used inSEC(".struct_ops"), to be loaded successfully as astruct_opsprog?
Quoted 4 lines
Assigning a
bpf_progto a function ptr under theSEC(".struct_ops")is the only way for a program to be successfully loaded asstruct_opsprog type. Extra way to allow a prog to be changed tostruct_opsprog_typeis either useless or redundant.
Well, first of all, just for consistency with everything else. We have such methods for all prog_types, so I'd like to avoid a special snowflake one that doesn't.
Second, while high-level libbpf API provides all the magic to construct STRUCT_OPS map based on .struct_ops section types, technically, user might decide to do that using low-level map creation API, right? So not making unnecessary assumptions and providing complete APIs is a good thing, IMO. Especially if it costs basically nothing in terms of code and maintenance.
Quoted 4 lines
If it is really necessary to have
__set_struct_ops()as a API for completeness, it can be added...
Quoted 8 lines
Quoted 1 line
bpf_program__is_struct_ops()APIs for completeness, similarly to how
is_struct_ops()makes sense.Quoted 3 lines
BTW, libbpf will emit debug message for every single BPF program it doesn't recognize section for, so it is still nice to have it be something more or less standardized and recognizable by libbpf.
I can make this debug (not error) message go away too after setting the
BPF_PROG_TYPE_STRUCT_OPSautomatically at open time.
Yep, that would be great.
Re: [PATCH bpf-next v2 11/11] bpf: Add bpf_dctcp example
Quoted 65 lines · Andrii Nakryiko
Quoted 64 lines · Martin Lau
Quoted 62 lines · Andrii Nakryiko
Quoted 47 lines · Martin Lau
Quoted 38 lines · Andrii Nakryiko
Quoted 26 lines · Martin KaFai Lau
This patch adds a
bpf_dctcpexample. It currently does not do no-ECN fallback but the same could be done through the cgrp2-bpf.Signed-off-by Martin KaFai Lau <[email protected]>--- tools/testing/selftests/bpf/bpf_tcp_helpers.h | 228 ++++++++++++++++++ .../selftests/bpf/prog_tests/bpf_tcp_ca.c | 218 +++++++++++++++++ tools/testing/selftests/bpf/progs/bpf_dctcp.c | 210 ++++++++++++++++ 3 files changed, 656 insertions(+) create mode 100644 tools/testing/selftests/bpf/bpf_tcp_helpers.h create mode 100644 tools/testing/selftests/bpf/prog_tests/bpf_tcp_ca.c create mode 100644 tools/testing/selftests/bpf/progs/bpf_dctcp.ctools/testing/selftests/bpf/bpf_tcp_helpers.h new file +6
-0@@ -0,0 +1,228 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __BPF_TCP_HELPERS_H +#define __BPF_TCP_HELPERS_H + +#include <stdbool.h> +#include <linux/types.h> tools/testing/selftests/bpf/bpf_tcp_helpers.h line 16
Quoted 10 lines
tools/testing/selftests/bpf/bpf_tcp_helpers.h new file +10
-0+#include <bpf_helpers.h> +#include <bpf_core_read.h> +#include "bpf_trace_helpers.h" + +#define BPF_TCP_OPS_0 (fname, ret_type, ...) BPF_TRACE_x(0, #fname"_sec", fname, ret_type, __VA_ARGS__) +#define BPF_TCP_OPS_1 (fname, ret_type, ...) BPF_TRACE_x(1, #fname"_sec", fname, ret_type, __VA_ARGS__) +#define BPF_TCP_OPS_2 (fname, ret_type, ...) BPF_TRACE_x(2, #fname"_sec", fname, ret_type, __VA_ARGS__) +#define BPF_TCP_OPS_3 (fname, ret_type, ...) BPF_TRACE_x(3, #fname"_sec", fname, ret_type, __VA_ARGS__) +#define BPF_TCP_OPS_4 (fname, ret_type, ...) BPF_TRACE_x(4, #fname"_sec", fname, ret_type, __VA_ARGS__) +#define BPF_TCP_OPS_5 (fname, ret_type, ...) BPF_TRACE_x(5, #fname"_sec", fname, ret_type, __VA_ARGS__)Quoted 7 lines
Should we try to put those BPF programs into some section that would indicate they are used with struct opts? libbpf doesn't use or enforce that (even though it could to derive and enforce that they are
STRUCT_OPSprograms). So something likeSEC("struct_ops/<ideally-operation-name-here>"). I think having this convention is very useful for consistency and to do a quick ELF dump and see what is where. WDYT?Quoted 5 lines
I did not use it here because I don't want any misperception that it is a required convention by libbpf.
Sure, I can prefix it here and comment that it is just a convention but not a libbpf's requirement.
Well, we can actually make it a requirement of sorts. Currently your code expects that BPF program's type is UNSPEC and then it sets it to
STRUCT_OPS. Alternatively we can say that any BPF program inSEC("struct_ops/<whatever>")will be automatically assignedSTRUCT_OPTSBPF program type (which is done generically inbpf_object__open()), and then as .struct_ops section is parsed, all those programs will be "assembled" by the code you added into astruct_opsmap.
Quoted 6 lines
Quoted 3 lines
Setting
BPF_PROG_TYPE_STRUCT_OPScan be done automatically at open phase (duringcollect_reloctime). I will make this change.Can you please extend exiting logic in
__bpf_object__open()to do this? See howlibbpf_prog_type_by_name()is used for that.
Does it have to call libbpf_prog_type_by_name() if everything has already been decided by the earlier bpf_object__collect_struct_ops_map_reloc()?
Quoted 12 lines
Quoted 12 lines
Quoted 6 lines
It's a requirement "of sorts", because even if user doesn't do that, stuff will still work, if user manually will call
bpf_program__set_struct_ops(prog). Which actually reminds me that it would be good to addbpf_program__set_struct_ops()andAlthough there is
BPF_PROG_TYPE_FNSmacro,
I don't see movingbpf_prog__set_struct_ops(prog)toLIBBPF_APIis useful while actually may cause confusion and error. How could__set_struct_ops()a prog tostruct_opsprog_typehelp a program, which is not used inSEC(".struct_ops"), to be loaded successfully as astruct_opsprog?
Quoted 8 lines
Quoted 4 lines
Assigning a
bpf_progto a function ptr under theSEC(".struct_ops")is the only way for a program to be successfully loaded asstruct_opsprog type. Extra way to allow a prog to be changed tostruct_opsprog_typeis either useless or redundant.Well, first of all, just for consistency with everything else. We have such methods for all
prog_types, so I'd like to avoid a special snowflake one that doesn't.
Yes, for consistency is fine as I mentioned in the earlier reply, as long as it is understood the usefulness of it.
Quoted 4 lines
Second, while high-level libbpf API provides all the magic to construct
STRUCT_OPSmap based on .struct_ops section types, technically, user might decide to do that using low-level map creation API, right?
How?
Correct that the map api is reused as is in SEC(".struct_ops").
For prog, AFAICT, it is not possible to create struct_ops prog from raw and use it in struct_ops map unless more LIBBPF_API is added. Lets put aside the need to find the btf_vmlinux and its btf-types...etc. At least, there is no LIBBPF_API to set prog->attach_btf_id. Considering the amount of preparation is needed to create a struct_ops map from raw, I would like to see a real use case first before even considering what else is needed and add another LIBBPF_API that may not be used.
Re: [PATCH bpf-next v2 11/11] bpf: Add bpf_dctcp example
Quoted 71 lines · Martin Lau
Quoted 69 lines · Andrii Nakryiko
Quoted 67 lines · Martin Lau
Quoted 54 lines · Andrii Nakryiko
Quoted 47 lines · Martin Lau
Quoted 38 lines · Andrii Nakryiko
Quoted 26 lines · Martin KaFai Lau
This patch adds a
bpf_dctcpexample. It currently does not do no-ECN fallback but the same could be done through the cgrp2-bpf.Signed-off-by Martin KaFai Lau <[email protected]>--- tools/testing/selftests/bpf/bpf_tcp_helpers.h | 228 ++++++++++++++++++ .../selftests/bpf/prog_tests/bpf_tcp_ca.c | 218 +++++++++++++++++ tools/testing/selftests/bpf/progs/bpf_dctcp.c | 210 ++++++++++++++++ 3 files changed, 656 insertions(+) create mode 100644 tools/testing/selftests/bpf/bpf_tcp_helpers.h create mode 100644 tools/testing/selftests/bpf/prog_tests/bpf_tcp_ca.c create mode 100644 tools/testing/selftests/bpf/progs/bpf_dctcp.ctools/testing/selftests/bpf/bpf_tcp_helpers.h new file +6
-0@@ -0,0 +1,228 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __BPF_TCP_HELPERS_H +#define __BPF_TCP_HELPERS_H + +#include <stdbool.h> +#include <linux/types.h> tools/testing/selftests/bpf/bpf_tcp_helpers.h line 16
Quoted 10 lines
tools/testing/selftests/bpf/bpf_tcp_helpers.h new file +10
-0+#include <bpf_helpers.h> +#include <bpf_core_read.h> +#include "bpf_trace_helpers.h" + +#define BPF_TCP_OPS_0 (fname, ret_type, ...) BPF_TRACE_x(0, #fname"_sec", fname, ret_type, __VA_ARGS__) +#define BPF_TCP_OPS_1 (fname, ret_type, ...) BPF_TRACE_x(1, #fname"_sec", fname, ret_type, __VA_ARGS__) +#define BPF_TCP_OPS_2 (fname, ret_type, ...) BPF_TRACE_x(2, #fname"_sec", fname, ret_type, __VA_ARGS__) +#define BPF_TCP_OPS_3 (fname, ret_type, ...) BPF_TRACE_x(3, #fname"_sec", fname, ret_type, __VA_ARGS__) +#define BPF_TCP_OPS_4 (fname, ret_type, ...) BPF_TRACE_x(4, #fname"_sec", fname, ret_type, __VA_ARGS__) +#define BPF_TCP_OPS_5 (fname, ret_type, ...) BPF_TRACE_x(5, #fname"_sec", fname, ret_type, __VA_ARGS__)Quoted 7 lines
Should we try to put those BPF programs into some section that would indicate they are used with struct opts? libbpf doesn't use or enforce that (even though it could to derive and enforce that they are
STRUCT_OPSprograms). So something likeSEC("struct_ops/<ideally-operation-name-here>"). I think having this convention is very useful for consistency and to do a quick ELF dump and see what is where. WDYT?Quoted 5 lines
I did not use it here because I don't want any misperception that it is a required convention by libbpf.
Sure, I can prefix it here and comment that it is just a convention but not a libbpf's requirement.
Quoted 8 lines
Well, we can actually make it a requirement of sorts. Currently your code expects that BPF program's type is UNSPEC and then it sets it to
STRUCT_OPS. Alternatively we can say that any BPF program inSEC("struct_ops/<whatever>")will be automatically assignedSTRUCT_OPTSBPF program type (which is done generically inbpf_object__open()), and then as .struct_ops section is parsed, all those programs will be "assembled" by the code you added into astruct_opsmap.Setting
BPF_PROG_TYPE_STRUCT_OPScan be done automatically at open phase (duringcollect_reloctime). I will make this change.
Quoted 5 lines
Quoted 2 lines
Can you please extend exiting logic in
__bpf_object__open()to do this? See howlibbpf_prog_type_by_name()is used for that.Does it have to call
libbpf_prog_type_by_name()if everything has already been decided by the earlierbpf_object__collect_struct_ops_map_reloc()?
We can certainly change the logic to omit guessing program type if it's already set to something else than UNSPEC.
But all I'm asking is that instead of using #fname"_sec" section name, is to use "struct_ops/"#fname, because it's consistent with all other program types. If you do that, then you don't have to do anything extra (well, add single entry to section_defs, of course), it will just work as is.
Quoted 32 lines
Quoted 13 lines
Quoted 13 lines
Quoted 7 lines
It's a requirement "of sorts", because even if user doesn't do that, stuff will still work, if user manually will call
bpf_program__set_struct_ops(prog). Which actually reminds me that it would be good to addbpf_program__set_struct_ops()andAlthough there is
BPF_PROG_TYPE_FNSmacro,
I don't see movingbpf_prog__set_struct_ops(prog)toLIBBPF_APIis useful while actually may cause confusion and error. How could__set_struct_ops()a prog tostruct_opsprog_typehelp a program, which is not used inSEC(".struct_ops"), to be loaded successfully as astruct_opsprog?Quoted 8 lines
Quoted 4 lines
Assigning a
bpf_progto a function ptr under theSEC(".struct_ops")is the only way for a program to be successfully loaded asstruct_opsprog type. Extra way to allow a prog to be changed tostruct_opsprog_typeis either useless or redundant.Well, first of all, just for consistency with everything else. We have such methods for all
prog_types, so I'd like to avoid a special snowflake one that doesn't.Yes, for consistency is fine as I mentioned in the earlier reply, as long as it is understood the usefulness of it.
Quoted 4 lines
Second, while high-level libbpf API provides all the magic to construct
STRUCT_OPSmap based on .struct_ops section types, technically, user might decide to do that using low-level map creation API, right?How?
Correct that the map api is reused as is in
SEC(".struct_ops").
Quoted 8 lines
For prog, AFAICT, it is not possible to create
struct_opsprog from raw and use it instruct_opsmap unless moreLIBBPF_APIis added. Lets put aside the need to find thebtf_vmlinuxand its btf-types...etc. At least, there is noLIBBPF_APIto setprog->attach_btf_id. Considering the amount of preparation is needed to create astruct_opsmap from raw, I would like to see a real use case first before even considering what else is needed and add anotherLIBBPF_APIthat may not be used.
To be clear, I don't think anyone in their right mind should do this by hand. I'm just saying that in the end it's not magic, just calls to low-level map APIs. See above, though, all I care is consistent pattern for sections names: "program_type/<whatever-makes-sense>".
Re: [PATCH bpf-next v2 11/11] bpf: Add bpf_dctcp example
Quoted 78 lines · Andrii Nakryiko
Quoted 71 lines · Martin Lau
Quoted 69 lines · Andrii Nakryiko
Quoted 67 lines · Martin Lau
Quoted 54 lines · Andrii Nakryiko
Quoted 47 lines · Martin Lau
Quoted 38 lines · Andrii Nakryiko
Quoted 26 lines · Martin KaFai Lau
This patch adds a
bpf_dctcpexample. It currently does not do no-ECN fallback but the same could be done through the cgrp2-bpf.Signed-off-by Martin KaFai Lau <[email protected]>--- tools/testing/selftests/bpf/bpf_tcp_helpers.h | 228 ++++++++++++++++++ .../selftests/bpf/prog_tests/bpf_tcp_ca.c | 218 +++++++++++++++++ tools/testing/selftests/bpf/progs/bpf_dctcp.c | 210 ++++++++++++++++ 3 files changed, 656 insertions(+) create mode 100644 tools/testing/selftests/bpf/bpf_tcp_helpers.h create mode 100644 tools/testing/selftests/bpf/prog_tests/bpf_tcp_ca.c create mode 100644 tools/testing/selftests/bpf/progs/bpf_dctcp.ctools/testing/selftests/bpf/bpf_tcp_helpers.h new file +6
-0@@ -0,0 +1,228 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __BPF_TCP_HELPERS_H +#define __BPF_TCP_HELPERS_H + +#include <stdbool.h> +#include <linux/types.h> tools/testing/selftests/bpf/bpf_tcp_helpers.h line 16
Quoted 10 lines
tools/testing/selftests/bpf/bpf_tcp_helpers.h new file +10
-0+#include <bpf_helpers.h> +#include <bpf_core_read.h> +#include "bpf_trace_helpers.h" + +#define BPF_TCP_OPS_0 (fname, ret_type, ...) BPF_TRACE_x(0, #fname"_sec", fname, ret_type, __VA_ARGS__) +#define BPF_TCP_OPS_1 (fname, ret_type, ...) BPF_TRACE_x(1, #fname"_sec", fname, ret_type, __VA_ARGS__) +#define BPF_TCP_OPS_2 (fname, ret_type, ...) BPF_TRACE_x(2, #fname"_sec", fname, ret_type, __VA_ARGS__) +#define BPF_TCP_OPS_3 (fname, ret_type, ...) BPF_TRACE_x(3, #fname"_sec", fname, ret_type, __VA_ARGS__) +#define BPF_TCP_OPS_4 (fname, ret_type, ...) BPF_TRACE_x(4, #fname"_sec", fname, ret_type, __VA_ARGS__) +#define BPF_TCP_OPS_5 (fname, ret_type, ...) BPF_TRACE_x(5, #fname"_sec", fname, ret_type, __VA_ARGS__)Quoted 7 lines
Should we try to put those BPF programs into some section that would indicate they are used with struct opts? libbpf doesn't use or enforce that (even though it could to derive and enforce that they are
STRUCT_OPSprograms). So something likeSEC("struct_ops/<ideally-operation-name-here>"). I think having this convention is very useful for consistency and to do a quick ELF dump and see what is where. WDYT?Quoted 5 lines
I did not use it here because I don't want any misperception that it is a required convention by libbpf.
Sure, I can prefix it here and comment that it is just a convention but not a libbpf's requirement.
Quoted 8 lines
Well, we can actually make it a requirement of sorts. Currently your code expects that BPF program's type is UNSPEC and then it sets it to
STRUCT_OPS. Alternatively we can say that any BPF program inSEC("struct_ops/<whatever>")will be automatically assignedSTRUCT_OPTSBPF program type (which is done generically inbpf_object__open()), and then as .struct_ops section is parsed, all those programs will be "assembled" by the code you added into astruct_opsmap.Setting
BPF_PROG_TYPE_STRUCT_OPScan be done automatically at open phase (duringcollect_reloctime). I will make this change.Quoted 5 lines
Quoted 2 lines
Can you please extend exiting logic in
__bpf_object__open()to do this? See howlibbpf_prog_type_by_name()is used for that.Does it have to call
libbpf_prog_type_by_name()if everything has already been decided by the earlierbpf_object__collect_struct_ops_map_reloc()?
Quoted 8 lines
We can certainly change the logic to omit guessing program type if it's already set to something else than UNSPEC.
But all I'm asking is that instead of using #fname"_sec" section name, is to use "struct_ops/"#fname, because it's consistent with all other program types. If you do that, then you don't have to do anything extra (well, add single entry to
section_defs, of course), it will just work as is.
Re: adding "struct_ops/" to section_defs,
Sure. as long as SEC(".struct_ops") can use prog that libbpf_prog_type_by_name() concluded it is either -ESRCH or STRUCT_OPS.
It is not the only change though. Other changes are still needed in collect_reloc (e.g. check prog type mismatch). They won't be much though.
Re: [PATCH bpf-next v2 11/11] bpf: Add bpf_dctcp example
Quoted 88 lines · Martin Lau
Quoted 78 lines · Andrii Nakryiko
Quoted 71 lines · Martin Lau
Quoted 69 lines · Andrii Nakryiko
Quoted 67 lines · Martin Lau
Quoted 54 lines · Andrii Nakryiko
Quoted 47 lines · Martin Lau
Quoted 38 lines · Andrii Nakryiko
Quoted 26 lines · Martin KaFai Lau
This patch adds a
bpf_dctcpexample. It currently does not do no-ECN fallback but the same could be done through the cgrp2-bpf.Signed-off-by Martin KaFai Lau <[email protected]>--- tools/testing/selftests/bpf/bpf_tcp_helpers.h | 228 ++++++++++++++++++ .../selftests/bpf/prog_tests/bpf_tcp_ca.c | 218 +++++++++++++++++ tools/testing/selftests/bpf/progs/bpf_dctcp.c | 210 ++++++++++++++++ 3 files changed, 656 insertions(+) create mode 100644 tools/testing/selftests/bpf/bpf_tcp_helpers.h create mode 100644 tools/testing/selftests/bpf/prog_tests/bpf_tcp_ca.c create mode 100644 tools/testing/selftests/bpf/progs/bpf_dctcp.ctools/testing/selftests/bpf/bpf_tcp_helpers.h new file +6
-0@@ -0,0 +1,228 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __BPF_TCP_HELPERS_H +#define __BPF_TCP_HELPERS_H + +#include <stdbool.h> +#include <linux/types.h> tools/testing/selftests/bpf/bpf_tcp_helpers.h line 16
Quoted 10 lines
tools/testing/selftests/bpf/bpf_tcp_helpers.h new file +10
-0+#include <bpf_helpers.h> +#include <bpf_core_read.h> +#include "bpf_trace_helpers.h" + +#define BPF_TCP_OPS_0 (fname, ret_type, ...) BPF_TRACE_x(0, #fname"_sec", fname, ret_type, __VA_ARGS__) +#define BPF_TCP_OPS_1 (fname, ret_type, ...) BPF_TRACE_x(1, #fname"_sec", fname, ret_type, __VA_ARGS__) +#define BPF_TCP_OPS_2 (fname, ret_type, ...) BPF_TRACE_x(2, #fname"_sec", fname, ret_type, __VA_ARGS__) +#define BPF_TCP_OPS_3 (fname, ret_type, ...) BPF_TRACE_x(3, #fname"_sec", fname, ret_type, __VA_ARGS__) +#define BPF_TCP_OPS_4 (fname, ret_type, ...) BPF_TRACE_x(4, #fname"_sec", fname, ret_type, __VA_ARGS__) +#define BPF_TCP_OPS_5 (fname, ret_type, ...) BPF_TRACE_x(5, #fname"_sec", fname, ret_type, __VA_ARGS__)Quoted 7 lines
Should we try to put those BPF programs into some section that would indicate they are used with struct opts? libbpf doesn't use or enforce that (even though it could to derive and enforce that they are
STRUCT_OPSprograms). So something likeSEC("struct_ops/<ideally-operation-name-here>"). I think having this convention is very useful for consistency and to do a quick ELF dump and see what is where. WDYT?Quoted 5 lines
I did not use it here because I don't want any misperception that it is a required convention by libbpf.
Sure, I can prefix it here and comment that it is just a convention but not a libbpf's requirement.
Quoted 8 lines
Well, we can actually make it a requirement of sorts. Currently your code expects that BPF program's type is UNSPEC and then it sets it to
STRUCT_OPS. Alternatively we can say that any BPF program inSEC("struct_ops/<whatever>")will be automatically assignedSTRUCT_OPTSBPF program type (which is done generically inbpf_object__open()), and then as .struct_ops section is parsed, all those programs will be "assembled" by the code you added into astruct_opsmap.Setting
BPF_PROG_TYPE_STRUCT_OPScan be done automatically at open phase (duringcollect_reloctime). I will make this change.Quoted 5 lines
Quoted 2 lines
Can you please extend exiting logic in
__bpf_object__open()to do this? See howlibbpf_prog_type_by_name()is used for that.Does it have to call
libbpf_prog_type_by_name()if everything has already been decided by the earlierbpf_object__collect_struct_ops_map_reloc()?Quoted 8 lines
We can certainly change the logic to omit guessing program type if it's already set to something else than UNSPEC.
But all I'm asking is that instead of using #fname"_sec" section name, is to use "struct_ops/"#fname, because it's consistent with all other program types. If you do that, then you don't have to do anything extra (well, add single entry to
section_defs, of course), it will just work as is.
Quoted 8 lines
Re: adding "struct_ops/" to
section_defs,
Sure. as long asSEC(".struct_ops")can use prog thatlibbpf_prog_type_by_name()concluded it is either-ESRCHorSTRUCT_OPS.It is not the only change though. Other changes are still needed in
collect_reloc(e.g. check prog type mismatch). They won't be much though.
sounds good, thanks!