bpf: getsockopt and setsockopt hooks
Subsystems (11)
v8 cover bpf: getsockopt and setsockopt hooks
This series implements two new per-cgroup hooks: getsockopt and setsockopt along with a new sockopt program type. The idea is pretty similar to recently introduced cgroup sysctl hooks, but implementation is simpler (no need to convert to/from strings).
What this can be applied to:
* move business logic of what tos/priority/etc can be set by containers (either pass or reject)
* handle existing options (or introduce new ones) differently by propagating some information in cgroup/socket local storage
Compared to a simple syscall/{g,s}etsockopt tracepoint, those hooks are context aware. Meaning, they can access underlying socket and use cgroup and socket local storage.
Stanislav Fomichev (9):
bpf: implement getsockopt and setsockopt hooks
bpf: sync bpf.h to tools/
libbpf: support sockopt hooks
selftests/bpf: test sockopt section name selftests/bpf: add sockopt test
selftests/bpf: add sockopt test that exercises sk helpers selftests/bpf: add sockopt test that exercises BPF_F_ALLOW_MULTI
bpf: add sockopt documentation
bpftool: support cgroup sockopt
Documentation/bpf/index.rst | 1 + Documentation/bpf/prog_cgroup_sockopt.rst | 82 ++ include/linux/bpf-cgroup.h | 43 + include/linux/bpf.h | 2 + include/linux/bpf_types.h | 1 + include/linux/filter.h | 14 + include/uapi/linux/bpf.h | 14 + kernel/bpf/cgroup.c | 317 +++++++ kernel/bpf/core.c | 9 + kernel/bpf/syscall.c | 19 + kernel/bpf/verifier.c | 13 + net/core/filter.c | 2 +- net/socket.c | 16 + .../bpftool/Documentation/bpftool-cgroup.rst | 7 +- .../bpftool/Documentation/bpftool-prog.rst | 3 +- tools/bpf/bpftool/bash-completion/bpftool | 9 +- tools/bpf/bpftool/cgroup.c | 5 +- tools/bpf/bpftool/main.h | 1 + tools/bpf/bpftool/prog.c | 3 +- tools/include/uapi/linux/bpf.h | 14 + tools/lib/bpf/libbpf.c | 5 + tools/lib/bpf/libbpf_probes.c | 1 + tools/testing/selftests/bpf/.gitignore | 3 + tools/testing/selftests/bpf/Makefile | 6 +- .../selftests/bpf/progs/sockopt_multi.c | 53 ++ .../testing/selftests/bpf/progs/sockopt_sk.c | 91 ++ .../selftests/bpf/test_section_names.c | 10 + tools/testing/selftests/bpf/test_sockopt.c | 892 ++++++++++++++++++ .../selftests/bpf/test_sockopt_multi.c | 276 ++++++ tools/testing/selftests/bpf/test_sockopt_sk.c | 185 ++++ 30 files changed, 2087 insertions(+), 10 deletions(-) create mode 100644 Documentation/bpf/prog_cgroup_sockopt.rst create mode 100644 tools/testing/selftests/bpf/progs/sockopt_multi.c create mode 100644 tools/testing/selftests/bpf/progs/sockopt_sk.c create mode 100644 tools/testing/selftests/bpf/test_sockopt.c create mode 100644 tools/testing/selftests/bpf/test_sockopt_multi.c create mode 100644 tools/testing/selftests/bpf/test_sockopt_sk.c
Signature · 1 line
2.22.0.410.gd8fdbe21b5-goog
PATCH v8 1/9 bpf: implement getsockopt and setsockopt hooks
Implement new BPF_PROG_TYPE_CGROUP_SOCKOPT program type and BPF_CGROUP_{G,S}ETSOCKOPT cgroup hooks.
BPF_CGROUP_SETSOCKOPT get a read-only view of the setsockopt arguments. BPF_CGROUP_GETSOCKOPT can modify the supplied buffer.
Both of them reuse existing PTR_TO_PACKET{,_END} infrastructure.
The buffer memory is pre-allocated (because I don't think there is a precedent for working with __user memory from bpf). This might be slow to do for each {s,g}etsockopt call, that's why I've added __cgroup_bpf_prog_array_is_empty that exits early if there is nothing attached to a cgroup. Note, however, that there is a race between __cgroup_bpf_prog_array_is_empty and BPF_PROG_RUN_ARRAY where cgroup program layout might have changed; this should not be a problem because in general there is a race between multiple calls to {s,g}etsocktop and user adding/removing bpf progs from a cgroup.
The return code of the BPF program is handled as follows:
* 0: EPERM
* 1: success, continue with next BPF program in the cgroup chain
v8:
* use s32 for optlen (Andrii Nakryiko)
v7:
* return only 0 or 1 (Alexei Starovoitov)
* always run all progs (Alexei Starovoitov)
* use optval=0 as kernel bypass in setsockopt (Alexei Starovoitov) (decided to use optval=-1 instead, optval=0 might be a valid input)
* call getsockopt hook after kernel handlers (Alexei Starovoitov)
v6:
* rework cgroup chaining; stop as soon as bpf program returns 0 or 2; see patch with the documentation for the details
* drop Andrii's and Martin's Acked-by (not sure they are comfortable with the new state of things)
v5:
* skip copy_to_user() and put_user() when ret == 0 (Martin Lau)
v4:
* don't export bpf_sk_fullsock helper (Martin Lau)
* size != sizeof(__u64) for uapi pointers (Martin Lau)
* offsetof instead of bpf_ctx_range when checking ctx access (Martin Lau)
v3:
* typos in BPF_PROG_CGROUP_SOCKOPT_RUN_ARRAY comments (Andrii Nakryiko)
* reverse christmas tree in BPF_PROG_CGROUP_SOCKOPT_RUN_ARRAY (Andrii Nakryiko)
* use __bpf_md_ptr instead of __u32 for optval{,_end} (Martin Lau)
* use BPF_FIELD_SIZEOF() for consistency (Martin Lau)
* new CG_SOCKOPT_ACCESS macro to wrap repeated parts
v2:
* moved bpf_sockopt_kern fields around to remove a hole (Martin Lau)
* aligned bpf_sockopt_kern->buf to 8 bytes (Martin Lau)
* bpf_prog_array_is_empty instead of bpf_prog_array_length (Martin Lau)
* added [0,2] return code check to verifier (Martin Lau)
* dropped unused buf[64] from the stack (Martin Lau)
* use PTR_TO_SOCKET for bpf_sockopt->sk (Martin Lau)
* dropped bpf_target_off from ctx rewrites (Martin Lau)
* use return code for kernel bypass (Martin Lau & Andrii Nakryiko)
--- include/linux/bpf-cgroup.h | 43 +++++ include/linux/bpf.h | 2 + include/linux/bpf_types.h | 1 + include/linux/filter.h | 14 ++ include/uapi/linux/bpf.h | 14 ++ kernel/bpf/cgroup.c | 317 +++++++++++++++++++++++++++++++++++++ kernel/bpf/core.c | 9 ++ kernel/bpf/syscall.c | 19 +++ kernel/bpf/verifier.c | 13 ++ net/core/filter.c | 2 +- net/socket.c | 16 ++ 11 files changed, 449 insertions(+), 1 deletion(-)
include/linux/bpf-cgroup.h +43 -0
@@ -124,6 +124,14 @@ int __cgroup_bpf_run_filter_sysctl(struct ctl_table_header *head, loff_t *ppos, void **new_buf, enum bpf_attach_type type); +int __cgroup_bpf_run_filter_setsockopt (struct sock *sock,int level, +int optname,char __user *optval, +int optlen); +int __cgroup_bpf_run_filter_getsockopt (struct sock *sk,int level, +int optname,char __user *optval, +int __user *optlen,int max_optlen, +int retval); +static inline enum bpf_cgroup_storage_type cgroup_storage_type (struct bpf_map *map) {
@@ -286,6 +294,36 @@ int bpf_percpu_cgroup_storage_update(struct bpf_map *map, void *key, __ret; \ }) +#define BPF_CGROUP_RUN_PROG_SETSOCKOPT (sock, level, optname, optval, optlen) \ +({ \ + int __ret = 0; \ + if (cgroup_bpf_enabled) \ + __ret = __cgroup_bpf_run_filter_setsockopt(sock, level, \ + optname, optval, \ + optlen); \ + __ret; \ +}) + +#define BPF_CGROUP_GETSOCKOPT_MAX_OPTLEN (optlen) \ +({ \ + int __ret = 0; \ + if (cgroup_bpf_enabled) \ + get_user(__ret, optlen); \ + __ret; \ +}) + +#define BPF_CGROUP_RUN_PROG_GETSOCKOPT (sock, level, optname, optval, optlen, \ + max_optlen, retval) \ +({ \ + int __ret = retval; \ + if (cgroup_bpf_enabled) \ + __ret = __cgroup_bpf_run_filter_getsockopt(sock, level, \ + optname, optval, \ + optlen, max_optlen, \ + retval); \ + __ret; \ +}) +int cgroup_bpf_prog_attach (const union bpf_attr *attr,enum bpf_prog_type ptype,struct bpf_prog *prog); int cgroup_bpf_prog_detach(const union bpf_attr *attr,
@@ -357,6 +395,11 @@ static inline int bpf_percpu_cgroup_storage_update(struct bpf_map *map,#define BPF_CGROUP_RUN_PROG_SOCK_OPS (sock_ops) ({ 0; })#define BPF_CGROUP_RUN_PROG_DEVICE_CGROUP (type,major,minor,access) ({ 0; })#define BPF_CGROUP_RUN_PROG_SYSCTL (head,table,write,buf,count,pos,nbuf) ({ 0; }) +#define BPF_CGROUP_GETSOCKOPT_MAX_OPTLEN (optlen) ({ 0; }) +#define BPF_CGROUP_RUN_PROG_GETSOCKOPT (sock, level, optname, optval, \ + optlen, max_optlen, retval) ({ retval; }) +#define BPF_CGROUP_RUN_PROG_SETSOCKOPT (sock, level, optname, optval, \ + optlen) ({ 0; })#define for_each_cgroup_storage_type (stype) for (; false; )
include/linux/bpf.h +2 -0
@@ -518,6 +518,7 @@ struct bpf_prog_array {struct bpf_prog_array *bpf_prog_array_alloc (u32 prog_cnt,gfp_t flags);void bpf_prog_array_free (struct bpf_prog_array *progs);int bpf_prog_array_length (struct bpf_prog_array *progs); +bool bpf_prog_array_is_empty (struct bpf_prog_array *array);int bpf_prog_array_copy_to_user (struct bpf_prog_array *progs,__u32 __user *prog_ids,u32 cnt);
@@ -1051,6 +1052,7 @@ extern const struct bpf_func_proto bpf_spin_unlock_proto;extern const struct bpf_func_proto bpf_get_local_storage_proto;extern const struct bpf_func_proto bpf_strtol_proto;extern const struct bpf_func_proto bpf_strtoul_proto; +extern const struct bpf_func_proto bpf_tcp_sock_proto;/* Shared helpers among cBPF and eBPF. */ void bpf_user_rnd_init_once (void );
include/linux/bpf_types.h +1 -0
@@ -30,6 +30,7 @@ BPF_PROG_TYPE(BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE, raw_tracepoint_writable)#ifdef CONFIG_CGROUP_BPF BPF_PROG_TYPE(BPF_PROG_TYPE_CGROUP_DEVICE, cg_dev) BPF_PROG_TYPE(BPF_PROG_TYPE_CGROUP_SYSCTL, cg_sysctl) +BPF_PROG_TYPE(BPF_PROG_TYPE_CGROUP_SOCKOPT, cg_sockopt)#endif #ifdef CONFIG_BPF_LIRC_MODE2 BPF_PROG_TYPE (BPF_PROG_TYPE_LIRC_MODE2 , lirc_mode2)
include/linux/filter.h +14 -0
@@ -1199,4 +1199,18 @@ struct bpf_sysctl_kern { u64 tmp_reg; }; +struct bpf_sockopt_kern { +struct sock *sk; +u8 *optval; +u8 *optval_end; +s32 level; +s32 optname; +s32 optlen; +s32 retval; + +/* Small on-stack optval buffer to avoid small allocations. +*/ +u8 buf[64 ]__aligned(8) ; +}; +#endif /* __LINUX_FILTER_H__ */
include/uapi/linux/bpf.h +14 -0
@@ -170,6 +170,7 @@ enum bpf_prog_type {BPF_PROG_TYPE_FLOW_DISSECTOR ,BPF_PROG_TYPE_CGROUP_SYSCTL ,BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE , + BPF_PROG_TYPE_CGROUP_SOCKOPT, }; enum bpf_attach_type {
@@ -194,6 +195,8 @@ enum bpf_attach_type {BPF_CGROUP_SYSCTL ,BPF_CGROUP_UDP4_RECVMSG ,BPF_CGROUP_UDP6_RECVMSG , +BPF_CGROUP_GETSOCKOPT , +BPF_CGROUP_SETSOCKOPT , __MAX_BPF_ATTACH_TYPE };
@@ -3541,4 +3544,15 @@ struct bpf_sysctl {*/ }; +struct bpf_sockopt { + __bpf_md_ptr(struct bpf_sock *, sk); + __bpf_md_ptr(void *, optval); + __bpf_md_ptr(void *, optval_end); + +__s32 level; +__s32 optname; +__s32 optlen; +__s32 retval; +}; +#endif /* _UAPI__LINUX_BPF_H__ */
kernel/bpf/cgroup.c +317 -0
332 lines not on this page · raw message
kernel/bpf/core.c +9 -0
@@ -1809,6 +1809,15 @@ int bpf_prog_array_length(struct bpf_prog_array *array)return cnt; } +bool bpf_prog_array_is_empty (struct bpf_prog_array *array) +{ +struct bpf_prog_array_item *item; + +for (item = array->items; item->prog; item++) +if (item->prog != &dummy_bpf_prog.prog) +return false; +return true; +} static bool bpf_prog_array_copy_core(struct bpf_prog_array *array, u32 *prog_ids,
kernel/bpf/syscall.c +19 -0
@@ -1590,6 +1590,14 @@ bpf_prog_load_check_attach_type(enum bpf_prog_type prog_type,default :return -EINVAL ; } +case BPF_PROG_TYPE_CGROUP_SOCKOPT : +switch (expected_attach_type) { +case BPF_CGROUP_SETSOCKOPT : +case BPF_CGROUP_GETSOCKOPT : +return 0 ; +default : +return -EINVAL ; + }default :return 0 ; }
@@ -1840,6 +1848,7 @@ static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog,switch (prog->type) {case BPF_PROG_TYPE_CGROUP_SOCK :case BPF_PROG_TYPE_CGROUP_SOCK_ADDR : +case BPF_PROG_TYPE_CGROUP_SOCKOPT :return attach_type == prog->expected_attach_type ?0 : -EINVAL ;case BPF_PROG_TYPE_CGROUP_SKB : return prog->enforce_expected_attach_type &&
@@ -1912,6 +1921,10 @@ static int bpf_prog_attach(const union bpf_attr *attr)case BPF_CGROUP_SYSCTL : ptype =BPF_PROG_TYPE_CGROUP_SYSCTL ;break ; +case BPF_CGROUP_GETSOCKOPT : +case BPF_CGROUP_SETSOCKOPT : + ptype =BPF_PROG_TYPE_CGROUP_SOCKOPT ; +break ;default :return -EINVAL ; }
@@ -1995,6 +2008,10 @@ static int bpf_prog_detach(const union bpf_attr *attr)case BPF_CGROUP_SYSCTL : ptype =BPF_PROG_TYPE_CGROUP_SYSCTL ;break ; +case BPF_CGROUP_GETSOCKOPT : +case BPF_CGROUP_SETSOCKOPT : + ptype =BPF_PROG_TYPE_CGROUP_SOCKOPT ; +break ;default :return -EINVAL ; }
@@ -2031,6 +2048,8 @@ static int bpf_prog_query(const union bpf_attr *attr,case BPF_CGROUP_SOCK_OPS :case BPF_CGROUP_DEVICE :case BPF_CGROUP_SYSCTL : +case BPF_CGROUP_GETSOCKOPT : +case BPF_CGROUP_SETSOCKOPT :break ;case BPF_LIRC_MODE2 :return lirc_prog_query (attr, uattr);
kernel/bpf/verifier.c +13 -0
@@ -2215,6 +2215,18 @@ static bool may_access_direct_pkt_data(struct bpf_verifier_env *env, env->seen_direct_write = true;return true; + +case BPF_PROG_TYPE_CGROUP_SOCKOPT : +if (t ==BPF_WRITE ) { +if (env->prog->expected_attach_type == +BPF_CGROUP_GETSOCKOPT ) { + env->seen_direct_write = true; +return true; + } +return false; + } +return true; +default :return false; }
@@ -6066,6 +6078,7 @@ static int check_return_code(struct bpf_verifier_env *env)case BPF_PROG_TYPE_SOCK_OPS :case BPF_PROG_TYPE_CGROUP_DEVICE :case BPF_PROG_TYPE_CGROUP_SYSCTL : +case BPF_PROG_TYPE_CGROUP_SOCKOPT :break ;default :return 0 ;
net/core/filter.c +1 -1
@@ -5651,7 +5651,7 @@ BPF_CALL_1(bpf_tcp_sock, struct sock *, sk)return (unsigned long )NULL ; } -static const struct bpf_func_proto bpf_tcp_sock_proto = { +const struct bpf_func_proto bpf_tcp_sock_proto = { .func = bpf_tcp_sock, .gpl_only = false, .ret_type =RET_PTR_TO_TCP_SOCK_OR_NULL ,
net/socket.c +16 -0
@@ -2063,6 +2063,15 @@ static int __sys_setsockopt(int fd, int level, int optname,if (err) goto out_put; + err =BPF_CGROUP_RUN_PROG_SETSOCKOPT (sock->sk, level, optname, + optval, optlen); +if (err <0 ) { + goto out_put; + }else if (err >0 ) { + err =0 ; + goto out_put; + } +if (level ==SOL_SOCKET ) err = sock_setsockopt(sock, level, optname, optval,
@@ -2093,6 +2102,7 @@ static int __sys_getsockopt(int fd, int level, int optname, {int err, fput_needed;struct socket *sock; +int max_optlen; sock =sockfd_lookup_light (fd, &err, &fput_needed);if (sock !=NULL ) {
@@ -2100,6 +2110,8 @@ static int __sys_getsockopt(int fd, int level, int optname,if (err) goto out_put; + max_optlen =BPF_CGROUP_GETSOCKOPT_MAX_OPTLEN (optlen); +if (level ==SOL_SOCKET ) err = sock_getsockopt(sock, level, optname, optval,
@@ -2108,6 +2120,10 @@ static int __sys_getsockopt(int fd, int level, int optname, err = sock->ops->getsockopt (sock, level, optname, optval, optlen); + + err =BPF_CGROUP_RUN_PROG_GETSOCKOPT (sock->sk, level, optname, + optval, optlen, + max_optlen, err); out_put:fput_light (sock->file, fput_needed); }
Signature · 1 line
2.22.0.410.gd8fdbe21b5-goog
PATCH v8 2/9 bpf: sync bpf.h to tools/
In reply to Stanislav FomichevExport new prog type and hook points to the libbpf.
--- tools/include/uapi/linux/bpf.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
tools/include/uapi/linux/bpf.h +14 -0
@@ -170,6 +170,7 @@ enum bpf_prog_type {BPF_PROG_TYPE_FLOW_DISSECTOR ,BPF_PROG_TYPE_CGROUP_SYSCTL ,BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE , + BPF_PROG_TYPE_CGROUP_SOCKOPT, }; enum bpf_attach_type {
@@ -194,6 +195,8 @@ enum bpf_attach_type {BPF_CGROUP_SYSCTL ,BPF_CGROUP_UDP4_RECVMSG ,BPF_CGROUP_UDP6_RECVMSG , +BPF_CGROUP_GETSOCKOPT , +BPF_CGROUP_SETSOCKOPT , __MAX_BPF_ATTACH_TYPE };
@@ -3541,4 +3544,15 @@ struct bpf_sysctl {*/ }; +struct bpf_sockopt { + __bpf_md_ptr(struct bpf_sock *, sk); + __bpf_md_ptr(void *, optval); + __bpf_md_ptr(void *, optval_end); + +__s32 level; +__s32 optname; +__s32 optlen; +__s32 retval; +}; +#endif /* _UAPI__LINUX_BPF_H__ */
Signature · 1 line
2.22.0.410.gd8fdbe21b5-goog
PATCH v8 4/9 selftests/bpf: test sockopt section name
In reply to Stanislav FomichevAdd tests that make sure libbpf section detection works.
--- tools/testing/selftests/bpf/test_section_names.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
tools/testing/selftests/bpf/test_section_names.c +10 -0
@@ -134,6 +134,16 @@ static struct sec_name_test tests[] = { {0, BPF_PROG_TYPE_CGROUP_SYSCTL, BPF_CGROUP_SYSCTL}, {0, BPF_CGROUP_SYSCTL}, }, + { +"cgroup/getsockopt" , + {0, BPF_PROG_TYPE_CGROUP_SOCKOPT, BPF_CGROUP_GETSOCKOPT}, + {0, BPF_CGROUP_GETSOCKOPT}, + }, + { +"cgroup/setsockopt" , + {0, BPF_PROG_TYPE_CGROUP_SOCKOPT, BPF_CGROUP_SETSOCKOPT}, + {0, BPF_CGROUP_SETSOCKOPT}, + }, };static int test_prog_type_by_name (const struct sec_name_test *test)
Signature · 1 line
2.22.0.410.gd8fdbe21b5-goog
PATCH v8 5/9 selftests/bpf: add sockopt test
In reply to Stanislav FomichevAdd sockopt selftests:
* require proper expected_attach_type
* enforce context field read/write access
* test bpf_sockopt_handled handler
* test EPERM
* test limiting optlen from getsockopt
* test out-of-bounds access
v7:
* remove return 2; test retval=0 and optlen=-1
v3:
* use DW for optval{,_end} loads
v2:
* use return code 2 for kernel bypass
--- tools/testing/selftests/bpf/.gitignore | 1 + tools/testing/selftests/bpf/Makefile | 3 +- tools/testing/selftests/bpf/test_sockopt.c | 892 +++++++++++++++++++++ 3 files changed, 895 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/bpf/test_sockopt.c
tools/testing/selftests/bpf/.gitignore +1 -0
@@ -39,3 +39,4 @@ libbpf.so.* test_hashmap test_btf_dump xdping +test_sockopt
tools/testing/selftests/bpf/Makefile +2 -1
@@ -26,7 +26,7 @@ TEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_lpm_map test test_sock test_btf test_sockmap get_cgroup_id_user test_socket_cookie \ test_cgroup_storage test_select_reuseport test_section_names \ test_netcnt test_tcpnotify_user test_sock_fields test_sysctl test_hashmap \ - test_btf_dump test_cgroup_attach xdping + test_btf_dump test_cgroup_attach xdping test_sockopt BPF_OBJ_FILES = $(patsubst %.c,%.o, $(notdir $(wildcard progs/*.c))) TEST_GEN_FILES = $(BPF_OBJ_FILES)
@@ -103,6 +103,7 @@ $(OUTPUT)/test_netcnt: cgroup_helpers.c $(OUTPUT)/test_sock_fields: cgroup_helpers.c $(OUTPUT)/test_sysctl: cgroup_helpers.c $(OUTPUT)/test_cgroup_attach: cgroup_helpers.c +$(OUTPUT)/test_sockopt: cgroup_helpers.c .PHONY: force
tools/testing/selftests/bpf/test_sockopt.c new file +892 -0
892 lines not on this page · raw message
Signature · 1 line
2.22.0.410.gd8fdbe21b5-goog
PATCH v8 6/9 selftests/bpf: add sockopt test that exercises sk helpers
In reply to Stanislav Fomichevsocktop test that introduces new SOL_CUSTOM sockopt level and stores whatever users sets in sk storage. Whenever getsockopt is called, the original value is retrieved.
v7:
* use retval=0 and optlen-1
v6:
* test 'ret=1' use-case as well (Alexei Starovoitov)
v4:
* don't call bpf_sk_fullsock helper
v3:
* drop (__u8 *)(long) casts for optval{,_end}
v2:
* new test
--- tools/testing/selftests/bpf/.gitignore | 1 + tools/testing/selftests/bpf/Makefile | 3 +- .../testing/selftests/bpf/progs/sockopt_sk.c | 91 +++++++++ tools/testing/selftests/bpf/test_sockopt_sk.c | 185 ++++++++++++++++++ 4 files changed, 279 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/bpf/progs/sockopt_sk.c create mode 100644 tools/testing/selftests/bpf/test_sockopt_sk.c
tools/testing/selftests/bpf/.gitignore +1 -0
@@ -40,3 +40,4 @@ test_hashmap test_btf_dump xdping test_sockopt +test_sockopt_sk
tools/testing/selftests/bpf/Makefile +2 -1
@@ -26,7 +26,7 @@ TEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_lpm_map test test_sock test_btf test_sockmap get_cgroup_id_user test_socket_cookie \ test_cgroup_storage test_select_reuseport test_section_names \ test_netcnt test_tcpnotify_user test_sock_fields test_sysctl test_hashmap \ - test_btf_dump test_cgroup_attach xdping test_sockopt + test_btf_dump test_cgroup_attach xdping test_sockopt test_sockopt_sk BPF_OBJ_FILES = $(patsubst %.c,%.o, $(notdir $(wildcard progs/*.c))) TEST_GEN_FILES = $(BPF_OBJ_FILES)
@@ -104,6 +104,7 @@ $(OUTPUT)/test_sock_fields: cgroup_helpers.c $(OUTPUT)/test_sysctl: cgroup_helpers.c $(OUTPUT)/test_cgroup_attach: cgroup_helpers.c $(OUTPUT)/test_sockopt: cgroup_helpers.c +$(OUTPUT)/test_sockopt_sk: cgroup_helpers.c .PHONY: force
tools/testing/selftests/bpf/progs/sockopt_sk.c new file +91 -0
91 lines not on this page · raw message
tools/testing/selftests/bpf/test_sockopt_sk.c new file +185 -0
185 lines not on this page · raw message
Signature · 1 line
2.22.0.410.gd8fdbe21b5-goog
PATCH v8 8/9 bpf: add sockopt documentation
In reply to Stanislav FomichevProvide user documentation about sockopt prog type and cgroup hooks.
v7:
* add description for retval=0 and optlen=-1
v6:
* describe cgroup chaining, add example
v2:
* use return code 2 for kernel bypass
--- Documentation/bpf/index.rst | 1 + Documentation/bpf/prog_cgroup_sockopt.rst | 82 +++++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 Documentation/bpf/prog_cgroup_sockopt.rst
Documentation/bpf/index.rst +1 -0
@@ -42,6 +42,7 @@ Program types .. toctree:: :maxdepth: 1 + prog_cgroup_sockopt prog_cgroup_sysctl prog_flow_dissector
Documentation/bpf/prog_cgroup_sockopt.rst new file +82 -0
82 lines not on this page · raw message
Signature · 1 line
2.22.0.410.gd8fdbe21b5-goog
PATCH v8 7/9 selftests/bpf: add sockopt test that exercises BPF_F_ALLOW_MULTI
In reply to Stanislav Fomichevsockopt test that verifies chaining behavior.
v7:
* rework the test to verify cgroup getsockopt chaining
--- tools/testing/selftests/bpf/.gitignore | 1 + tools/testing/selftests/bpf/Makefile | 4 +- .../selftests/bpf/progs/sockopt_multi.c | 53 ++++ .../selftests/bpf/test_sockopt_multi.c | 276 ++++++++++++++++++ 4 files changed, 333 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/bpf/progs/sockopt_multi.c create mode 100644 tools/testing/selftests/bpf/test_sockopt_multi.c
tools/testing/selftests/bpf/.gitignore +1 -0
@@ -41,3 +41,4 @@ test_btf_dump xdping test_sockopt test_sockopt_sk +test_sockopt_multi
tools/testing/selftests/bpf/Makefile +3 -1
@@ -26,7 +26,8 @@ TEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_lpm_map test test_sock test_btf test_sockmap get_cgroup_id_user test_socket_cookie \ test_cgroup_storage test_select_reuseport test_section_names \ test_netcnt test_tcpnotify_user test_sock_fields test_sysctl test_hashmap \ - test_btf_dump test_cgroup_attach xdping test_sockopt test_sockopt_sk + test_btf_dump test_cgroup_attach xdping test_sockopt test_sockopt_sk \ + test_sockopt_multi BPF_OBJ_FILES = $(patsubst %.c,%.o, $(notdir $(wildcard progs/*.c))) TEST_GEN_FILES = $(BPF_OBJ_FILES)
@@ -105,6 +106,7 @@ $(OUTPUT)/test_sysctl: cgroup_helpers.c $(OUTPUT)/test_cgroup_attach: cgroup_helpers.c $(OUTPUT)/test_sockopt: cgroup_helpers.c $(OUTPUT)/test_sockopt_sk: cgroup_helpers.c +$(OUTPUT)/test_sockopt_multi: cgroup_helpers.c .PHONY: force
tools/testing/selftests/bpf/progs/sockopt_multi.c new file +53 -0
53 lines not on this page · raw message
tools/testing/selftests/bpf/test_sockopt_multi.c new file +276 -0
276 lines not on this page · raw message
Signature · 1 line
2.22.0.410.gd8fdbe21b5-goog
PATCH v8 9/9 bpftool: support cgroup sockopt
In reply to Stanislav FomichevSupport sockopt prog type and cgroup hooks in the bpftool.
--- tools/bpf/bpftool/Documentation/bpftool-cgroup.rst | 7 +++++-- tools/bpf/bpftool/Documentation/bpftool-prog.rst | 3 ++- tools/bpf/bpftool/bash-completion/bpftool | 9 ++++++--- tools/bpf/bpftool/cgroup.c | 5 ++++- tools/bpf/bpftool/main.h | 1 + tools/bpf/bpftool/prog.c | 3 ++- 6 files changed, 20 insertions(+), 8 deletions(-)
tools/bpf/bpftool/Documentation/bpftool-cgroup.rst +5 -2
@@ -30,7 +30,8 @@ CGROUP COMMANDS | *PROG* := { **id** *PROG_ID* | **pinned** *FILE* | **tag** *PROG_TAG* } | *ATTACH_TYPE* := { **ingress** | **egress** | **sock_create** | **sock_ops** | **device** | | **bind4** | **bind6** | **post_bind4** | **post_bind6** | **connect4** | **connect6** | -| **sendmsg4** | **sendmsg6** | **recvmsg4** | **recvmsg6** | **sysctl** } +| **sendmsg4** | **sendmsg6** | **recvmsg4** | **recvmsg6** | **sysctl** | +| **getsockopt** | **setsockopt** } | *ATTACH_FLAGS* := { **multi** | **override** } DESCRIPTION
@@ -91,7 +92,9 @@ DESCRIPTION an unconnected udp4 socket (since 5.2); **recvmsg6** call to recvfrom(2), recvmsg(2), recvmmsg(2) for an unconnected udp6 socket (since 5.2); - **sysctl** sysctl access (since 5.2). + **sysctl** sysctl access (since 5.2); + **getsockopt** call to getsockopt (since 5.3); + **setsockopt** call to setsockopt (since 5.3). **bpftool cgroup detach** *CGROUP* *ATTACH_TYPE* *PROG* Detach *PROG* from the cgroup *CGROUP* and attach type
tools/bpf/bpftool/Documentation/bpftool-prog.rst +2 -1
@@ -40,7 +40,8 @@ PROG COMMANDS | **lwt_seg6local** | **sockops** | **sk_skb** | **sk_msg** | **lirc_mode2** | | **cgroup/bind4** | **cgroup/bind6** | **cgroup/post_bind4** | **cgroup/post_bind6** | | **cgroup/connect4** | **cgroup/connect6** | **cgroup/sendmsg4** | **cgroup/sendmsg6** | -| **cgroup/recvmsg4** | **cgroup/recvmsg6** | **cgroup/sysctl** +| **cgroup/recvmsg4** | **cgroup/recvmsg6** | **cgroup/sysctl** | +| **cgroup/getsockopt** | **cgroup/setsockopt** | } | *ATTACH_TYPE* := { | **msg_verdict** | **stream_verdict** | **stream_parser** | **flow_dissector**
tools/bpf/bpftool/bash-completion/bpftool +6 -3
@@ -379,7 +379,8 @@ _bpftool() cgroup/sendmsg4 cgroup/sendmsg6 \ cgroup/recvmsg4 cgroup/recvmsg6 \ cgroup/post_bind4 cgroup/post_bind6 \ - cgroup/sysctl" -- \ + cgroup/sysctl cgroup/getsockopt \ + cgroup/setsockopt" -- \ "$cur" ) ) return 0 ;;
@@ -689,7 +690,8 @@ _bpftool() attach|detach) local ATTACH_TYPES='ingress egress sock_create sock_ops \ device bind4 bind6 post_bind4 post_bind6 connect4 \ - connect6 sendmsg4 sendmsg6 recvmsg4 recvmsg6 sysctl' + connect6 sendmsg4 sendmsg6 recvmsg4 recvmsg6 sysctl \ + getsockopt setsockopt' local ATTACH_FLAGS='multi override' local PROG_TYPE='id pinned tag' case $prev in
@@ -699,7 +701,8 @@ _bpftool() ;; ingress|egress|sock_create|sock_ops|device|bind4|bind6|\ post_bind4|post_bind6|connect4|connect6|sendmsg4|\ - sendmsg6|recvmsg4|recvmsg6|sysctl) + sendmsg6|recvmsg4|recvmsg6|sysctl|getsockopt|\ + setsockopt) COMPREPLY=( $( compgen -W "$PROG_TYPE" -- \ "$cur" ) ) return 0
tools/bpf/bpftool/cgroup.c +4 -1
@@ -26,7 +26,8 @@" sock_ops | device | bind4 | bind6 |\n" \" post_bind4 | post_bind6 | connect4 |\n" \" connect6 | sendmsg4 | sendmsg6 |\n" \ -" recvmsg4 | recvmsg6 | sysctl }" +" recvmsg4 | recvmsg6 | sysctl |\n" \ +" getsockopt | setsockopt }" static const char * const attach_type_strings[] = { [BPF_CGROUP_INET_INGRESS] = "ingress",
@@ -45,6 +46,8 @@ static const char * const attach_type_strings[] = { [BPF_CGROUP_SYSCTL] = "sysctl", [BPF_CGROUP_UDP4_RECVMSG] = "recvmsg4", [BPF_CGROUP_UDP6_RECVMSG] = "recvmsg6", + [BPF_CGROUP_GETSOCKOPT] = "getsockopt", + [BPF_CGROUP_SETSOCKOPT] = "setsockopt", [__MAX_BPF_ATTACH_TYPE] = NULL, };
tools/bpf/bpftool/main.h +1 -0
@@ -74,6 +74,7 @@ static const char * const prog_type_name[] = { [BPF_PROG_TYPE_SK_REUSEPORT] = "sk_reuseport", [BPF_PROG_TYPE_FLOW_DISSECTOR] = "flow_dissector", [BPF_PROG_TYPE_CGROUP_SYSCTL] = "cgroup_sysctl", + [BPF_PROG_TYPE_CGROUP_SOCKOPT] = "cgroup_sockopt", };extern const char *const map_type_name[];
tools/bpf/bpftool/prog.c +2 -1
@@ -1071,7 +1071,8 @@ static int do_help(int argc, char **argv)" cgroup/bind4 | cgroup/bind6 | cgroup/post_bind4 |\n" " cgroup/post_bind6 | cgroup/connect4 | cgroup/connect6 |\n" " cgroup/sendmsg4 | cgroup/sendmsg6 | cgroup/recvmsg4 |\n" -" cgroup/recvmsg6 }\n" +" cgroup/recvmsg6 | cgroup/getsockopt |\n" +" cgroup/setsockopt }\n" " ATTACH_TYPE := { msg_verdict | stream_verdict | stream_parser |\n" " flow_dissector }\n" " " HELP_SPEC_OPTIONS "\n"
Signature · 1 line
2.22.0.410.gd8fdbe21b5-goog
PATCH v8 3/9 libbpf: support sockopt hooks
In reply to Stanislav FomichevMake libbpf aware of new sockopt hooks so it can derive prog type and hook point from the section names.
--- tools/lib/bpf/libbpf.c | 5 +++++ tools/lib/bpf/libbpf_probes.c | 1 + 2 files changed, 6 insertions(+)
tools/lib/bpf/libbpf.c +5 -0
@@ -2646,6 +2646,7 @@ static bool bpf_prog_type__needs_kver(enum bpf_prog_type type)case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE :case BPF_PROG_TYPE_PERF_EVENT :case BPF_PROG_TYPE_CGROUP_SYSCTL : +case BPF_PROG_TYPE_CGROUP_SOCKOPT :return false;case BPF_PROG_TYPE_KPROBE :default :
@@ -3604,6 +3605,10 @@ static const struct { BPF_CGROUP_UDP6_RECVMSG),BPF_EAPROG_SEC ("cgroup/sysctl" ,BPF_PROG_TYPE_CGROUP_SYSCTL ,BPF_CGROUP_SYSCTL ), +BPF_EAPROG_SEC ("cgroup/getsockopt" ,BPF_PROG_TYPE_CGROUP_SOCKOPT , +BPF_CGROUP_GETSOCKOPT ), +BPF_EAPROG_SEC ("cgroup/setsockopt" ,BPF_PROG_TYPE_CGROUP_SOCKOPT , + BPF_CGROUP_SETSOCKOPT), };#undef BPF_PROG_SEC_IMPL
tools/lib/bpf/libbpf_probes.c +1 -0
@@ -101,6 +101,7 @@ probe_load(enum bpf_prog_type prog_type, const struct bpf_insn *insns,case BPF_PROG_TYPE_SK_REUSEPORT :case BPF_PROG_TYPE_FLOW_DISSECTOR :case BPF_PROG_TYPE_CGROUP_SYSCTL : +case BPF_PROG_TYPE_CGROUP_SOCKOPT :default :break ; }
Signature · 1 line
2.22.0.410.gd8fdbe21b5-goog
Re: [PATCH bpf-next v8 1/9] bpf: implement getsockopt and setsockopt hooks
In reply to Stanislav FomichevQuoted 64 lines · Stanislav Fomichev
Implement new
BPF_PROG_TYPE_CGROUP_SOCKOPTprogram type and BPF_CGROUP_{G,S}ETSOCKOPT cgroup hooks.
BPF_CGROUP_SETSOCKOPTget a read-only view of the setsockopt arguments.BPF_CGROUP_GETSOCKOPTcan modify the supplied buffer.
Both of them reuse existing PTR_TO_PACKET{,_END} infrastructure.The buffer memory is pre-allocated (because I don't think there is a precedent for working with
__usermemory from bpf). This might be slow to do for each {s,g}etsockopt call, that's why I've added__cgroup_bpf_prog_array_is_emptythat exits early if there is nothing attached to a cgroup. Note, however, that there is a race between__cgroup_bpf_prog_array_is_emptyandBPF_PROG_RUN_ARRAYwhere cgroup program layout might have changed; this should not be a problem because in general there is a race between multiple calls to {s,g}etsocktop and user adding/removing bpf progs from a cgroup.The return code of the BPF program is handled as follows:
* 0: EPERM
* 1: success, continue with next BPF program in the cgroup chainv8:
* use s32 for optlen (Andrii Nakryiko)v7:
* return only 0 or 1 (Alexei Starovoitov)
* always run all progs (Alexei Starovoitov)
* use optval=0 as kernel bypass in setsockopt (Alexei Starovoitov) (decided to use optval=-1 instead, optval=0 might be a valid input)
* call getsockopt hook after kernel handlers (Alexei Starovoitov)v6:
* rework cgroup chaining; stop as soon as bpf program returns 0 or 2; see patch with the documentation for the details
* drop Andrii's and Martin's Acked-by (not sure they are comfortable with the new state of things)v5:
* skipcopy_to_user()andput_user()when ret == 0 (Martin Lau)v4:
* don't exportbpf_sk_fullsockhelper (Martin Lau)
* size !=sizeof(__u64)for uapi pointers (Martin Lau)
* offsetof instead ofbpf_ctx_rangewhen checking ctx access (Martin Lau)v3:
* typos inBPF_PROG_CGROUP_SOCKOPT_RUN_ARRAYcomments (Andrii Nakryiko)
* reverse christmas tree inBPF_PROG_CGROUP_SOCKOPT_RUN_ARRAY(Andrii Nakryiko)
* use__bpf_md_ptrinstead of__u32for optval{,_end} (Martin Lau)
* useBPF_FIELD_SIZEOF()for consistency (Martin Lau)
* newCG_SOCKOPT_ACCESSmacro to wrap repeated partsv2:
* movedbpf_sockopt_kernfields around to remove a hole (Martin Lau)
* alignedbpf_sockopt_kern->bufto 8 bytes (Martin Lau)
*bpf_prog_array_is_emptyinstead ofbpf_prog_array_length(Martin Lau)
* added [0,2] return code check to verifier (Martin Lau)
* dropped unused buf[64] from the stack (Martin Lau)
* usePTR_TO_SOCKETforbpf_sockopt->sk(Martin Lau)
* droppedbpf_target_offfrom ctx rewrites (Martin Lau)
* use return code for kernel bypass (Martin Lau & Andrii Nakryiko)
Quoted 3 lines
Cc Martin Lau <[email protected]>Signed-off-by Stanislav Fomichev <[email protected]>---
Re: [PATCH bpf-next v8 1/9] bpf: implement getsockopt and setsockopt hooks
In reply to Stanislav FomichevQuoted 6 lines · Stanislav Fomichev
Implement new
BPF_PROG_TYPE_CGROUP_SOCKOPTprogram type and BPF_CGROUP_{G,S}ETSOCKOPT cgroup hooks.
BPF_CGROUP_SETSOCKOPTget a read-only view of the setsockopt arguments.BPF_CGROUP_GETSOCKOPTcan modify the supplied buffer.
Both of them reuse existing PTR_TO_PACKET{,_END} infrastructure.
getsockopt side looks good to me.
I tried to convince myself that readonly setsockopt is fine for now, but it feels we need to make it writeable from the start. I agree with your reasoning that doing copy_to_user is no good, but we can do certainly do set_fs(KERNEL_DS) game.
The same way as kernel_setsockopt() is doing.
It seems quite useful to modify 'optval' before passing it to kernel. Then bpf prog would be able to specify sane values for SO_SNDBUF instead of rejecting them.
The alternative would be to allow bpf prog to call setsockopt from inside, but sock is locked when prog is running,
so unlocking within helper is not going to be clean.
wdyt?
Stanislav Fomichev
reviewerRe: [PATCH bpf-next v8 1/9] bpf: implement getsockopt and setsockopt hooks
Quoted 8 lines · Alexei Starovoitov
Quoted 6 lines · Stanislav Fomichev
Implement new
BPF_PROG_TYPE_CGROUP_SOCKOPTprogram type and BPF_CGROUP_{G,S}ETSOCKOPT cgroup hooks.
BPF_CGROUP_SETSOCKOPTget a read-only view of the setsockopt arguments.BPF_CGROUP_GETSOCKOPTcan modify the supplied buffer.
Both of them reuse existing PTR_TO_PACKET{,_END} infrastructure.
Quoted 13 lines
getsockopt side looks good to me.
I tried to convince myself that readonly setsockopt is fine for now, but it feels we need to make it writeable from the start. I agree with your reasoning that doingcopy_to_useris no good, but we can do certainly doset_fs(KERNEL_DS)game.
The same way askernel_setsockopt()is doing.
It seems quite useful to modify 'optval' before passing it to kernel. Then bpf prog would be able to specify sane values forSO_SNDBUFinstead of rejecting them.
The alternative would be to allow bpf prog to call setsockopt from inside, but sock is locked when prog is running,
so unlocking within helper is not going to be clean.
wdyt?
Sure, I can take a look if you think that it would be useful in general. Looks like set_fs should do the trick.
(I was thinking about exporting something like the existing bpf_setsockopt to a setsockopt hooks, but I agree, it comes with its own bag of problems).
Re: [PATCH bpf-next v8 1/9] bpf: implement getsockopt and setsockopt hooks
Quoted 22 lines · Stanislav Fomichev
Quoted 8 lines · Alexei Starovoitov
Quoted 6 lines · Stanislav Fomichev
Implement new
BPF_PROG_TYPE_CGROUP_SOCKOPTprogram type and BPF_CGROUP_{G,S}ETSOCKOPT cgroup hooks.
BPF_CGROUP_SETSOCKOPTget a read-only view of the setsockopt arguments.BPF_CGROUP_GETSOCKOPTcan modify the supplied buffer.
Both of them reuse existing PTR_TO_PACKET{,_END} infrastructure.Quoted 13 lines
getsockopt side looks good to me.
I tried to convince myself that readonly setsockopt is fine for now, but it feels we need to make it writeable from the start. I agree with your reasoning that doingcopy_to_useris no good, but we can do certainly doset_fs(KERNEL_DS)game.
The same way askernel_setsockopt()is doing.
It seems quite useful to modify 'optval' before passing it to kernel. Then bpf prog would be able to specify sane values forSO_SNDBUFinstead of rejecting them.
The alternative would be to allow bpf prog to call setsockopt from inside, but sock is locked when prog is running,
so unlocking within helper is not going to be clean.
wdyt?
Quoted 2 lines
Sure, I can take a look if you think that it would be useful in general. Looks like
set_fsshould do the trick.
Thanks. I think it's useful.
For example see the recent sack steam issue and Eric's workaround for older kernel to add 128k to sk_sndbuf.
If we had an ability to do adjust SO_SNDBUF from cgroup-bpf prog when user space is doing setsockopt we could have mitigated it by rolling bpf prog instead of patching and rebooting the kernels. That's a bit of a stretch use case, of course.
My feeling that if not today, but really soon people will find solid use cases for adjusting sockopt values via cgroup-bpf.