bpf: Expose bpf_get_socket_cookie to tracing programs
Subsystems (8)
PATCH v2 1/3 bpf: Expose bpf_get_socket_cookie to tracing programs
This creates a new helper proto because the existing bpf_get_socket_cookie_sock_proto has a ARG_PTR_TO_CTX argument and only works for BPF programs where the context is a sock.
This helper could also be useful to other BPF program types such as LSM.
--- include/uapi/linux/bpf.h | 7 +++++++ kernel/trace/bpf_trace.c | 4 ++++ net/core/filter.c | 7 +++++++ tools/include/uapi/linux/bpf.h | 7 +++++++ 4 files changed, 25 insertions(+)
include/uapi/linux/bpf.h +7 -0
@@ -1662,6 +1662,13 @@ union bpf_attr {* Return * A 8-byte long non-decreasing 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 non-decreasing 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
@@ -1725,6 +1725,8 @@ raw_tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) } } +extern const struct bpf_func_proto bpf_get_socket_cookie_sock_tracing_proto; +const struct bpf_func_proto *tracing_prog_func_proto (enum bpf_func_id func_id,const struct bpf_prog *prog) {
@@ -1748,6 +1750,8 @@ 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 &bpf_get_socket_cookie_sock_tracing_proto;#endif case BPF_FUNC_seq_printf: return prog->expected_attach_type == BPF_TRACE_ITER ?
net/core/filter.c +7 -0
@@ -4631,6 +4631,13 @@ static const struct bpf_func_proto bpf_get_socket_cookie_sock_proto = { .arg1_type = ARG_PTR_TO_CTX, }; +const struct bpf_func_proto bpf_get_socket_cookie_sock_tracing_proto = { + .func = bpf_get_socket_cookie_sock, + .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
@@ -1662,6 +1662,13 @@ union bpf_attr {* Return * A 8-byte long non-decreasing 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 non-decreasing 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
PATCH v2 2/3 selftests/bpf: Integrate the socket_cookie test to test_progs
Currently, 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 \
@@ -161,7 +161,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 v2 3/3 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
Daniel Borkmann
maintainerQuoted 19 lines · Florent Revest
This creates a new helper proto because the existing
bpf_get_socket_cookie_sock_protohas aARG_PTR_TO_CTXargument and only works for BPF programs where the context is a sock.This helper could also be useful to other BPF program types such as LSM.
Signed-off-by Florent Revest <[email protected]>--- include/uapi/linux/bpf.h | 7 +++++++ kernel/trace/bpf_trace.c | 4 ++++ net/core/filter.c | 7 +++++++ tools/include/uapi/linux/bpf.h | 7 +++++++ 4 files changed, 25 insertions(+)include/uapi/linux/bpf.h +0
-0
include/uapi/linux/bpf.h union bpf_attr { · line 1670
Quoted 9 lines
include/uapi/linux/bpf.h +6
-0@@ -1662,6 +1662,13 @@ union bpf_attr {* Return * A 8-byte long non-decreasing 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 non-decreasing number.
I would not mention this here since it's not fully correct and we should avoid users taking non-decreasing granted in their progs. The only assumption you can make is that it can be considered a unique number. See also [0] with reverse counter..
Quoted 41 lines
include/uapi/linux/bpf.h +1
-0+* * 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@@ -1725,6 +1725,8 @@ raw_tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) } } +extern const struct bpf_func_proto bpf_get_socket_cookie_sock_tracing_proto; +const struct bpf_func_proto *tracing_prog_func_proto (enum bpf_func_id func_id,const struct bpf_prog *prog) {@@ -1748,6 +1750,8 @@ 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 &bpf_get_socket_cookie_sock_tracing_proto;#endif case BPF_FUNC_seq_printf: return prog->expected_attach_type == BPF_TRACE_ITER ?net/core/filter.c +7
-0@@ -4631,6 +4631,13 @@ static const struct bpf_func_proto bpf_get_socket_cookie_sock_proto = { .arg1_type = ARG_PTR_TO_CTX, }; +const struct bpf_func_proto bpf_get_socket_cookie_sock_tracing_proto = { + .func = bpf_get_socket_cookie_sock, + .gpl_only = false, + .ret_type =RET_INTEGER , + .arg1_type =ARG_PTR_TO_BTF_ID_SOCK_COMMON , +}; +
Daniel Borkmann
maintainerQuoted 34 lines · Daniel Borkmann
Quoted 19 lines · Florent Revest
This creates a new helper proto because the existing
bpf_get_socket_cookie_sock_protohas aARG_PTR_TO_CTXargument and only works for BPF programs where the context is a sock.This helper could also be useful to other BPF program types such as LSM.
Signed-off-by Florent Revest <[email protected]>---
include/uapi/linux/bpf.h | 7 +++++++
kernel/trace/bpf_trace.c | 4 ++++
net/core/filter.c | 7 +++++++
tools/include/uapi/linux/bpf.h| 7 +++++++4 files changed, 25 insertions(+)include/uapi/linux/bpf.h +0
-0include/uapi/linux/bpf.h union bpf_attr { · line 1670
Quoted 9 lines
include/uapi/linux/bpf.h +6
-0@@ -1662,6 +1662,13 @@ union bpf_attr { * Return * A 8-byte long non-decreasing 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 non-decreasing number.I would not mention this here since it's not fully correct and we should avoid users taking non-decreasing granted in their progs. The only assumption you can make is that it can be considered a unique number. See also [0] with reverse counter..
Quoted 1 line
One more thought, in case you plan to use this from sleepable context, you would need to use sock_gen_cookie() variant in the BPF helper instead.
Quoted 39 lines · Daniel Borkmann
Quoted 38 lines · Daniel Borkmann
Quoted 27 lines · Florent Revest
This creates a new helper proto because the existing
bpf_get_socket_cookie_sock_protohas aARG_PTR_TO_CTXargument and only
works for BPF programs where the context is a sock.This helper could also be useful to other BPF program types such as LSM.
Signed-off-by Florent Revest <[email protected]>--- include/uapi/linux/bpf.h | 7 +++++++ kernel/trace/bpf_trace.c | 4 ++++ net/core/filter.c | 7 +++++++ tools/include/uapi/linux/bpf.h | 7 +++++++ 4 files changed, 25 insertions(+)include/uapi/linux/bpf.h +3
-0@@ -1662,6 +1662,13 @@ union bpf_attr {* Return * A 8-byte long non-decreasing number. * +* u64 bpf_get_socket_cookie(void *sk) +* Description +* Equivalent to **bpf_get_socket_cookie**\ () helper Quoted 4 lines
that accepts
+ * *sk*, but gets socket from a BTF **struct sock**.
+ * Return
+ * A 8-byte long non-decreasing number.I would not mention this here since it's not fully correct and we should avoid users taking non-decreasing granted in their progs. The only assumption you can make is that it can be considered a unique number. See also [0] with reverse counter..
Quoted 2 lines
Ah this is a good point, thank you! I will send a v3 with an extra patch that s/non-decreasing/unique/ in the other descriptions. I had not given it any extra thought, I just stupidly copied/pasted existing descriptions. :)
Quoted 3 lines
One more thought, in case you plan to use this from sleepable context, you would need to use
sock_gen_cookie()variant in the BPF helper instead.
Out of curiosity, why don't we just always call sock_gen_cookie? Is it to avoid the performance impact of increasing the preempt counter and introducing a memory barriers ?
Daniel Borkmann
maintainerQuoted 48 lines · Florent Revest
Quoted 39 lines · Daniel Borkmann
Quoted 38 lines · Daniel Borkmann
Quoted 27 lines · Florent Revest
This creates a new helper proto because the existing
bpf_get_socket_cookie_sock_protohas aARG_PTR_TO_CTXargument and only
works for BPF programs where the context is a sock.This helper could also be useful to other BPF program types such as LSM.
Signed-off-by Florent Revest <[email protected]>--- include/uapi/linux/bpf.h | 7 +++++++ kernel/trace/bpf_trace.c | 4 ++++ net/core/filter.c | 7 +++++++ tools/include/uapi/linux/bpf.h | 7 +++++++ 4 files changed, 25 insertions(+)include/uapi/linux/bpf.h +3
-0@@ -1662,6 +1662,13 @@ union bpf_attr {* Return * A 8-byte long non-decreasing number. * +* u64 bpf_get_socket_cookie(void *sk) +* Description +* Equivalent to **bpf_get_socket_cookie**\ () helper Quoted 4 lines
that accepts
+ * *sk*, but gets socket from a BTF **struct sock**.
+ * Return
+ * A 8-byte long non-decreasing number.I would not mention this here since it's not fully correct and we should avoid users taking non-decreasing granted in their progs. The only assumption you can make is that it can be considered a unique number. See also [0] with reverse counter..
Quoted 2 lines
Ah this is a good point, thank you! I will send a v3 with an extra patch that s/non-decreasing/unique/ in the other descriptions. I had not given it any extra thought, I just stupidly copied/pasted existing descriptions. :)
Quoted 7 lines
Quoted 3 lines
One more thought, in case you plan to use this from sleepable context, you would need to use
sock_gen_cookie()variant in the BPF helper instead.Out of curiosity, why don't we just always call
sock_gen_cookie? Is it to avoid the performance impact of increasing the preempt counter and introducing a memory barriers ?
Yes, all the other contexts where the existing helpers are used already have preemption disabled, so the extra preempt_{disable,enable}() is unnecessary overhead given we want the cookie generation be efficient.
Thanks,
Daniel
Quoted 59 lines · Florent Revest
This creates a new helper proto because the existing
bpf_get_socket_cookie_sock_protohas aARG_PTR_TO_CTXargument and only works for BPF programs where the context is a sock.This helper could also be useful to other BPF program types such as LSM.
Signed-off-by Florent Revest <[email protected]>--- include/uapi/linux/bpf.h | 7 +++++++ kernel/trace/bpf_trace.c | 4 ++++ net/core/filter.c | 7 +++++++ tools/include/uapi/linux/bpf.h | 7 +++++++ 4 files changed, 25 insertions(+)include/uapi/linux/bpf.h +7
-0@@ -1662,6 +1662,13 @@ union bpf_attr {* Return * A 8-byte long non-decreasing 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 non-decreasing 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@@ -1725,6 +1725,8 @@ raw_tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) } } +extern const struct bpf_func_proto bpf_get_socket_cookie_sock_tracing_proto; +const struct bpf_func_proto *tracing_prog_func_proto (enum bpf_func_id func_id,const struct bpf_prog *prog) {@@ -1748,6 +1750,8 @@ 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 &bpf_get_socket_cookie_sock_tracing_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 4638
Quoted 8 lines
net/core/filter.c +5
-0@@ -4631,6 +4631,13 @@ static const struct bpf_func_proto bpf_get_socket_cookie_sock_proto = { .arg1_type = ARG_PTR_TO_CTX, }; +const struct bpf_func_proto bpf_get_socket_cookie_sock_tracing_proto = { + .func = bpf_get_socket_cookie_sock, + .gpl_only = false, + .ret_type =RET_INTEGER , + .arg1_type =ARG_PTR_TO_BTF_ID_SOCK_COMMON ,
In tracing where it gets a sk pointer, the sk could be NULL. A NULL check is required in the helper. Please refer to bpf_skc_to_tcp_sock[_proto] as an example.
This proto is in general also useful for non tracing context where it can get a hold of a sk pointer. (e.g. another similar usage that will have a hold on a sk pointer for BPF_PROG_TYPE_SK_REUSEPORT [0]). In case if you don't need sleepable at this point as Daniel mentioned in another thread. Does it make sense to rename this proto to something like bpf_get_socket_pointer_cookie_proto?
Quoted 11 lines · Martin KaFai Lau
net/core/filter.c static const struct bpf_func_proto bpf_get_socket_cookie_sock_proto = { · line 4638
Quoted 6 lines · Florent Revest
+const struct
bpf_func_protobpf_get_socket_cookie_sock_tracing_proto= {net/core/filter.c +4
-0+ .func = bpf_get_socket_cookie_sock, + .gpl_only = false, + .ret_type = RET_INTEGER, + .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,In tracing where it gets a sk pointer, the sk could be NULL. A NULL check is required in the helper. Please refer to bpf_skc_to_tcp_sock[_proto] as an example.
Ah, good catch! :)
Quoted 3 lines
This proto is in general also useful for non tracing context where it can get a hold of a sk pointer. (e.g. another similar usage that will have a hold on a sk pointer for
BPF_PROG_TYPE_SK_REUSEPORT[0]).
Agreed.
Quoted 3 lines
In case if you don't need sleepable at this point as Daniel mentioned in another thread. Does it make sense to rename this proto to something like
bpf_get_socket_pointer_cookie_proto?
My understanding is that I could have two helpers definitions and protos, one calling sock_gen_cookie and the other one calling __sock_gen_cookie. Then I could just use:
return prog->aux->sleepable
? bpf_get_socket_pointer_cookie_sleepable_proto
: bpf_get_socket_pointer_cookie_proto;Would that work ?