Skip to content

Commit c5dbb89

Browse files
FlorentRevestAlexei Starovoitov
authored andcommitted
bpf: Expose bpf_get_socket_cookie to tracing programs
This needs a new helper that: - can work in a sleepable context (using sock_gen_cookie) - takes a struct sock pointer and checks that it's not NULL Signed-off-by: Florent Revest <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Acked-by: KP Singh <[email protected]> Acked-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 07881cc commit c5dbb89

File tree

5 files changed

+31
-0
lines changed

5 files changed

+31
-0
lines changed

include/linux/bpf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1885,6 +1885,7 @@ extern const struct bpf_func_proto bpf_per_cpu_ptr_proto;
18851885
extern const struct bpf_func_proto bpf_this_cpu_ptr_proto;
18861886
extern const struct bpf_func_proto bpf_ktime_get_coarse_ns_proto;
18871887
extern const struct bpf_func_proto bpf_sock_from_file_proto;
1888+
extern const struct bpf_func_proto bpf_get_socket_ptr_cookie_proto;
18881889

18891890
const struct bpf_func_proto *bpf_tracing_func_proto(
18901891
enum bpf_func_id func_id, const struct bpf_prog *prog);

include/uapi/linux/bpf.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1673,6 +1673,14 @@ union bpf_attr {
16731673
* Return
16741674
* A 8-byte long unique number.
16751675
*
1676+
* u64 bpf_get_socket_cookie(struct sock *sk)
1677+
* Description
1678+
* Equivalent to **bpf_get_socket_cookie**\ () helper that accepts
1679+
* *sk*, but gets socket from a BTF **struct sock**. This helper
1680+
* also works for sleepable programs.
1681+
* Return
1682+
* A 8-byte long unique number or 0 if *sk* is NULL.
1683+
*
16761684
* u32 bpf_get_socket_uid(struct sk_buff *skb)
16771685
* Return
16781686
* The owner UID of the socket associated to *skb*. If the socket

kernel/trace/bpf_trace.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1760,6 +1760,8 @@ tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
17601760
return &bpf_sk_storage_delete_tracing_proto;
17611761
case BPF_FUNC_sock_from_file:
17621762
return &bpf_sock_from_file_proto;
1763+
case BPF_FUNC_get_socket_cookie:
1764+
return &bpf_get_socket_ptr_cookie_proto;
17631765
#endif
17641766
case BPF_FUNC_seq_printf:
17651767
return prog->expected_attach_type == BPF_TRACE_ITER ?

net/core/filter.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4631,6 +4631,18 @@ static const struct bpf_func_proto bpf_get_socket_cookie_sock_proto = {
46314631
.arg1_type = ARG_PTR_TO_CTX,
46324632
};
46334633

4634+
BPF_CALL_1(bpf_get_socket_ptr_cookie, struct sock *, sk)
4635+
{
4636+
return sk ? sock_gen_cookie(sk) : 0;
4637+
}
4638+
4639+
const struct bpf_func_proto bpf_get_socket_ptr_cookie_proto = {
4640+
.func = bpf_get_socket_ptr_cookie,
4641+
.gpl_only = false,
4642+
.ret_type = RET_INTEGER,
4643+
.arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
4644+
};
4645+
46344646
BPF_CALL_1(bpf_get_socket_cookie_sock_ops, struct bpf_sock_ops_kern *, ctx)
46354647
{
46364648
return __sock_gen_cookie(ctx->sk);

tools/include/uapi/linux/bpf.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1673,6 +1673,14 @@ union bpf_attr {
16731673
* Return
16741674
* A 8-byte long unique number.
16751675
*
1676+
* u64 bpf_get_socket_cookie(struct sock *sk)
1677+
* Description
1678+
* Equivalent to **bpf_get_socket_cookie**\ () helper that accepts
1679+
* *sk*, but gets socket from a BTF **struct sock**. This helper
1680+
* also works for sleepable programs.
1681+
* Return
1682+
* A 8-byte long unique number or 0 if *sk* is NULL.
1683+
*
16761684
* u32 bpf_get_socket_uid(struct sk_buff *skb)
16771685
* Return
16781686
* The owner UID of the socket associated to *skb*. If the socket

0 commit comments

Comments
 (0)