bpf: Be less specific about socket cookies guarantees
Subsystems (9)
PATCH v3 1/4 bpf: Be less specific about socket cookies guarantees
Since "92acdc58ab11 bpf, net: Rework cookie generator as per-cpu one" socket cookies are not guaranteed to be non-decreasing. The bpf_get_socket_cookie helper descriptions are currently specifying that cookies are non-decreasing but we don't want users to rely on that.
--- include/uapi/linux/bpf.h | 8 ++++---- tools/include/uapi/linux/bpf.h | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-)
include/uapi/linux/bpf.h +4 -4
@@ -1650,22 +1650,22 @@ union bpf_attr {* networking traffic statistics as it provides a global socket * identifier that can be assumed unique. * Return -* A 8-byte long non-decreasing number on success, or 0 if the -* socket field is missing inside *skb*. +* A 8-byte long unique number on success, or 0 if the socket +* field is missing inside *skb*. * * u64 bpf_get_socket_cookie(struct bpf_sock_addr *ctx) * Description * Equivalent to bpf_get_socket_cookie() helper that accepts * *skb*, but gets socket from **struct bpf_sock_addr** context. * Return -* A 8-byte long non-decreasing number. +* A 8-byte long unique number. * * u64 bpf_get_socket_cookie(struct bpf_sock_ops *ctx) * Description * Equivalent to **bpf_get_socket_cookie**\ () helper that accepts * *skb*, but gets socket from **struct bpf_sock_ops** context. * Return -* A 8-byte long non-decreasing number. +* A 8-byte long unique number. * * u32 bpf_get_socket_uid(struct sk_buff *skb) * Return
tools/include/uapi/linux/bpf.h +4 -4
@@ -1650,22 +1650,22 @@ union bpf_attr {* networking traffic statistics as it provides a global socket * identifier that can be assumed unique. * Return -* A 8-byte long non-decreasing number on success, or 0 if the -* socket field is missing inside *skb*. +* A 8-byte long unique number on success, or 0 if the socket +* field is missing inside *skb*. * * u64 bpf_get_socket_cookie(struct bpf_sock_addr *ctx) * Description * Equivalent to bpf_get_socket_cookie() helper that accepts * *skb*, but gets socket from **struct bpf_sock_addr** context. * Return -* A 8-byte long non-decreasing number. +* A 8-byte long unique number. * * u64 bpf_get_socket_cookie(struct bpf_sock_ops *ctx) * Description * Equivalent to **bpf_get_socket_cookie**\ () helper that accepts * *skb*, but gets socket from **struct bpf_sock_ops** context. * Return -* A 8-byte long non-decreasing number. +* A 8-byte long unique number. * * u32 bpf_get_socket_uid(struct sk_buff *skb) * Return
Signature · 1 line
2.29.2.576.ga3fc446d84-goog
PATCH v3 2/4 bpf: Expose bpf_get_socket_cookie to tracing programs
This needs two new helpers, one that works in a sleepable context (using sock_gen_cookie which disables/enables preemption) and one that does not (for performance reasons). Both take a struct sock pointer and need to check it for NULLness.
This helper could also be useful to other BPF program types such as LSM.
--- include/linux/bpf.h | 2 ++ include/uapi/linux/bpf.h | 7 +++++++ kernel/trace/bpf_trace.c | 4 ++++ net/core/filter.c | 24 ++++++++++++++++++++++++ tools/include/uapi/linux/bpf.h | 7 +++++++ 5 files changed, 44 insertions(+)
include/linux/bpf.h +2 -0
@@ -1859,6 +1859,8 @@ extern const struct bpf_func_proto bpf_snprintf_btf_proto;extern const struct bpf_func_proto bpf_per_cpu_ptr_proto;extern const struct bpf_func_proto bpf_this_cpu_ptr_proto;extern const struct bpf_func_proto bpf_ktime_get_coarse_ns_proto; +extern const struct bpf_func_proto bpf_get_socket_ptr_cookie_sleepable_proto; +extern const struct bpf_func_proto bpf_get_socket_ptr_cookie_proto;const struct bpf_func_proto *bpf_tracing_func_proto (enum bpf_func_id func_id,const struct bpf_prog *prog);
include/uapi/linux/bpf.h +7 -0
@@ -1667,6 +1667,13 @@ union bpf_attr {* Return * A 8-byte long unique number. * +* u64 bpf_get_socket_cookie(void *sk) +* Description +* Equivalent to **bpf_get_socket_cookie**\ () helper that accepts +* *sk*, but gets socket from a BTF **struct sock**. +* Return +* A 8-byte long unique number. +* * u32 bpf_get_socket_uid(struct sk_buff *skb) * Return * The owner UID of the socket associated to *skb*. If the socket
kernel/trace/bpf_trace.c +4 -0
@@ -1778,6 +1778,10 @@ tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)return &bpf_sk_storage_get_tracing_proto;case BPF_FUNC_sk_storage_delete:return &bpf_sk_storage_delete_tracing_proto; +case BPF_FUNC_get_socket_cookie: +return prog->aux->sleepable ? + &bpf_get_socket_ptr_cookie_sleepable_proto : + &bpf_get_socket_ptr_cookie_proto;#endif case BPF_FUNC_seq_printf: return prog->expected_attach_type == BPF_TRACE_ITER ?
net/core/filter.c +24 -0
@@ -4631,6 +4631,30 @@ static const struct bpf_func_proto bpf_get_socket_cookie_sock_proto = { .arg1_type = ARG_PTR_TO_CTX, }; +BPF_CALL_1(bpf_get_socket_ptr_cookie_sleepable, struct sock *, sk) +{ +return sk ?sock_gen_cookie (sk) :0 ; +} + +const struct bpf_func_proto bpf_get_socket_ptr_cookie_sleepable_proto = { + .func = bpf_get_socket_ptr_cookie_sleepable, + .gpl_only = false, + .ret_type =RET_INTEGER , + .arg1_type =ARG_PTR_TO_BTF_ID_SOCK_COMMON , +}; + +BPF_CALL_1(bpf_get_socket_ptr_cookie, struct sock *, sk) +{ +return sk ?__sock_gen_cookie (sk) :0 ; +} + +const struct bpf_func_proto bpf_get_socket_ptr_cookie_proto = { + .func = bpf_get_socket_ptr_cookie, + .gpl_only = false, + .ret_type =RET_INTEGER , + .arg1_type =ARG_PTR_TO_BTF_ID_SOCK_COMMON , +}; + BPF_CALL_1(bpf_get_socket_cookie_sock_ops, struct bpf_sock_ops_kern *, ctx) {return __sock_gen_cookie (ctx->sk);
tools/include/uapi/linux/bpf.h +7 -0
@@ -1667,6 +1667,13 @@ union bpf_attr {* Return * A 8-byte long unique number. * +* u64 bpf_get_socket_cookie(void *sk) +* Description +* Equivalent to **bpf_get_socket_cookie**\ () helper that accepts +* *sk*, but gets socket from a BTF **struct sock**. +* Return +* A 8-byte long unique number. +* * u32 bpf_get_socket_uid(struct sk_buff *skb) * Return * The owner UID of the socket associated to *skb*. If the socket
Signature · 1 line
2.29.2.576.ga3fc446d84-goog
KP Singh
maintainerRe: [PATCH bpf-next v3 2/4] bpf: Expose bpf_get_socket_cookie to tracing programs
Quoted 68 lines · Florent Revest
This needs two new helpers, one that works in a sleepable context (using
sock_gen_cookiewhich disables/enables preemption) and one that does not (for performance reasons). Both take a struct sock pointer and need to check it for NULLness.This helper could also be useful to other BPF program types such as LSM.
Signed-off-by Florent Revest <[email protected]>--- include/linux/bpf.h | 2 ++ include/uapi/linux/bpf.h | 7 +++++++ kernel/trace/bpf_trace.c | 4 ++++ net/core/filter.c | 24 ++++++++++++++++++++++++ tools/include/uapi/linux/bpf.h | 7 +++++++ 5 files changed, 44 insertions(+)include/linux/bpf.h +2
-0@@ -1859,6 +1859,8 @@ extern const struct bpf_func_proto bpf_snprintf_btf_proto;extern const struct bpf_func_proto bpf_per_cpu_ptr_proto;extern const struct bpf_func_proto bpf_this_cpu_ptr_proto;extern const struct bpf_func_proto bpf_ktime_get_coarse_ns_proto; +extern const struct bpf_func_proto bpf_get_socket_ptr_cookie_sleepable_proto; +extern const struct bpf_func_proto bpf_get_socket_ptr_cookie_proto;const struct bpf_func_proto *bpf_tracing_func_proto (enum bpf_func_id func_id,const struct bpf_prog *prog);include/uapi/linux/bpf.h +7
-0@@ -1667,6 +1667,13 @@ union bpf_attr {* Return * A 8-byte long unique number. * +* u64 bpf_get_socket_cookie(void *sk) +* Description +* Equivalent to **bpf_get_socket_cookie**\ () helper that accepts +* *sk*, but gets socket from a BTF **struct sock**. +* Return +* A 8-byte long unique number. +* * u32 bpf_get_socket_uid(struct sk_buff *skb) * Return * The owner UID of the socket associated to *skb*. If the socket kernel/trace/bpf_trace.c +4
-0@@ -1778,6 +1778,10 @@ tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)return &bpf_sk_storage_get_tracing_proto;case BPF_FUNC_sk_storage_delete:return &bpf_sk_storage_delete_tracing_proto; +case BPF_FUNC_get_socket_cookie: +return prog->aux->sleepable ? + &bpf_get_socket_ptr_cookie_sleepable_proto : + &bpf_get_socket_ptr_cookie_proto;#endif case BPF_FUNC_seq_printf: return prog->expected_attach_type == BPF_TRACE_ITER ?net/core/filter.c +0
-0
net/core/filter.c static const struct bpf_func_proto bpf_get_socket_cookie_sock_proto = { · line 4636
Quoted 6 lines
net/core/filter.c +3
-0@@ -4631,6 +4631,30 @@ static const struct bpf_func_proto bpf_get_socket_cookie_sock_proto = { .arg1_type = ARG_PTR_TO_CTX, }; +BPF_CALL_1(bpf_get_socket_ptr_cookie_sleepable, struct sock *, sk) +{ +return sk ?sock_gen_cookie (sk) :0 ;
My understanding is you can simply always call sock_gen_cookie and not have two protos.
This will disable preemption in sleepable programs and not have any effect in non-sleepable programs since preemption will already be disabled.
Re: [PATCH bpf-next v3 2/4] bpf: Expose bpf_get_socket_cookie to tracing programs
Quoted 6 lines · KP Singh
My understanding is you can simply always call
sock_gen_cookieand not have two protos.This will disable preemption in sleepable programs and not have any effect in non-sleepable programs since preemption will already be disabled.
Sure, that works. I thought that providing two helper implems would slightly improve performances on non-sleepable programs but I can send a v4 with only one helper that calls sock_gen_cookie.
PATCH v3 3/4 selftests/bpf: Integrate the socket_cookie test to test_progs
In reply to Florent RevestCurrently, the selftest for the BPF socket_cookie helpers is built and run independently from test_progs. It's easy to forget and hard to maintain.
This patch moves the socket cookies test into prog_tests/ and vastly simplifies its logic by:
- rewriting the loading code with BPF skeletons
- rewriting the server/client code with network helpers
- rewriting the cgroup code with test__join_cgroup
- rewriting the error handling code with CHECKs
--- tools/testing/selftests/bpf/Makefile | 3 +- .../selftests/bpf/prog_tests/socket_cookie.c | 82 +++++++ .../selftests/bpf/progs/socket_cookie_prog.c | 2 - .../selftests/bpf/test_socket_cookie.c | 208 ------------------ 4 files changed, 83 insertions(+), 212 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/socket_cookie.c delete mode 100644 tools/testing/selftests/bpf/test_socket_cookie.c
tools/testing/selftests/bpf/Makefile +1 -2
@@ -33,7 +33,7 @@ LDLIBS += -lcap -lelf -lz -lrt -lpthread # Order correspond to 'make run_tests' order TEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_lpm_map test_progs \ test_verifier_log test_dev_cgroup \ - test_sock test_sockmap get_cgroup_id_user test_socket_cookie \ + test_sock test_sockmap get_cgroup_id_user \ test_cgroup_storage \ test_netcnt test_tcpnotify_user test_sysctl \ test_progs-no_alu32 \
@@ -167,7 +167,6 @@ $(OUTPUT)/test_dev_cgroup: cgroup_helpers.c $(OUTPUT)/test_skb_cgroup_id_user: cgroup_helpers.c $(OUTPUT)/test_sock: cgroup_helpers.c $(OUTPUT)/test_sock_addr: cgroup_helpers.c -$(OUTPUT)/test_socket_cookie: cgroup_helpers.c $(OUTPUT)/test_sockmap: cgroup_helpers.c $(OUTPUT)/test_tcpnotify_user: cgroup_helpers.c trace_helpers.c $(OUTPUT)/get_cgroup_id_user: cgroup_helpers.c
tools/testing/selftests/bpf/prog_tests/socket_cookie.c new file +82 -0
82 lines not on this page · raw message
tools/testing/selftests/bpf/progs/socket_cookie_prog.c +0 -2
@@ -65,6 +65,4 @@ int update_cookie(struct bpf_sock_ops *ctx)return 1 ; } -int _version SEC("version") = 1; - char _license[] SEC("license") = "GPL";
tools/testing/selftests/bpf/test_socket_cookie.c deleted +0 -208
208 lines not on this page · raw message
Signature · 1 line
2.29.2.576.ga3fc446d84-goog
PATCH v3 4/4 selftests/bpf: Add a selftest for the tracing bpf_get_socket_cookie
In reply to Florent RevestThis builds up on the existing socket cookie test which checks whether the bpf_get_socket_cookie helpers provide the same value in cgroup/connect6 and sockops programs for a socket created by the userspace part of the test.
Adding a tracing program to the existing objects requires a different attachment strategy and different headers.
--- .../selftests/bpf/prog_tests/socket_cookie.c | 24 +++++++---- .../selftests/bpf/progs/socket_cookie_prog.c | 41 ++++++++++++++++--- 2 files changed, 52 insertions(+), 13 deletions(-)
tools/testing/selftests/bpf/prog_tests/socket_cookie.c +16 -8
@@ -15,8 +15,8 @@ struct socket_cookie {void test_socket_cookie (void ) { +struct bpf_link *set_link, *update_sockops_link, *update_tracing_link;socklen_t addr_len =sizeof (struct sockaddr_in6 ); -struct bpf_link *set_link, *update_link;int server_fd, client_fd, cgroup_fd;struct socket_cookie_prog *skel;__u32 cookie_expected_value;
@@ -39,15 +39,21 @@ void test_socket_cookie(void) PTR_ERR(set_link))) goto close_cgroup_fd; - update_link =bpf_program__attach_cgroup (skel->progs.update_cookie, - cgroup_fd); -if (CHECK (IS_ERR (update_link),"update-link-cg-attach" ,"err %ld\n" , -PTR_ERR (update_link))) + update_sockops_link =bpf_program__attach_cgroup ( + skel->progs.update_cookie_sockops, cgroup_fd); +if (CHECK (IS_ERR (update_sockops_link),"update-sockops-link-cg-attach" , +"err %ld\n" ,PTR_ERR (update_sockops_link))) goto free_set_link; + update_tracing_link =bpf_program__attach ( + skel->progs.update_cookie_tracing); +if (CHECK (IS_ERR (update_tracing_link),"update-tracing-link-attach" , +"err %ld\n" ,PTR_ERR (update_tracing_link))) + goto free_update_sockops_link; + server_fd =start_server (AF_INET6 ,SOCK_STREAM ,"::1" ,0 ,0 );if (CHECK (server_fd <0 ,"start_server" ,"errno %d\n" , errno)) - goto free_update_link; + goto free_update_tracing_link; client_fd =connect_to_fd (server_fd,0 );if (CHECK (client_fd <0 ,"connect_to_fd" ,"errno %d\n" , errno))
@@ -71,8 +77,10 @@ void test_socket_cookie(void)close (client_fd); close_server_fd:close (server_fd); -free_update_link: -bpf_link__destroy (update_link); +free_update_tracing_link: +bpf_link__destroy (update_tracing_link); +free_update_sockops_link: +bpf_link__destroy (update_sockops_link); free_set_link:bpf_link__destroy (set_link); close_cgroup_fd:
tools/testing/selftests/bpf/progs/socket_cookie_prog.c +36 -5
76 lines not on this page · raw message
Signature · 1 line
2.29.2.576.ga3fc446d84-goog