History

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

Florent Revest committed 5 years ago

Commit c5dbb891 parent 07881cc5 files · +31 −0Browse files

Unified · Split

5 files
  1. include/linux/bpf.h +1 −0
  2. include/uapi/linux/bpf.h +8 −0
  3. kernel/trace/bpf_trace.c +2 −0
  4. net/core/filter.c +12 −0
  5. tools/include/uapi/linux/bpf.h +8 −0
include/linux/bpf.h+1−0
@@ -1885,6 +1885,7 @@ extern const struct bpf_func_proto bpf_per_cpu_ptr_proto; · 1884 unmodified lines
1885extern const struct bpf_func_proto bpf_this_cpu_ptr_proto;1885extern const struct bpf_func_proto bpf_this_cpu_ptr_proto;
1886extern const struct bpf_func_proto bpf_ktime_get_coarse_ns_proto;1886extern const struct bpf_func_proto bpf_ktime_get_coarse_ns_proto;
1887extern const struct bpf_func_proto bpf_sock_from_file_proto;1887extern const struct bpf_func_proto bpf_sock_from_file_proto;
1888extern const struct bpf_func_proto bpf_get_socket_ptr_cookie_proto;
1888 1889
1889const struct bpf_func_proto *bpf_tracing_func_proto(1890const struct bpf_func_proto *bpf_tracing_func_proto(
1890 enum bpf_func_id func_id, const struct bpf_prog *prog);1891 enum bpf_func_id func_id, const struct bpf_prog *prog);
include/uapi/linux/bpf.h+8−0
@@ -1673,6 +1673,14 @@ union bpf_attr { · 1672 unmodified lines
1673 * Return1673 * Return
1674 * A 8-byte long unique number.1674 * A 8-byte long unique number.
1675 *1675 *
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 *
1676 * u32 bpf_get_socket_uid(struct sk_buff *skb)1684 * u32 bpf_get_socket_uid(struct sk_buff *skb)
1677 * Return1685 * Return
1678 * The owner UID of the socket associated to *skb*. If the socket1686 * The owner UID of the socket associated to *skb*. If the socket
kernel/trace/bpf_trace.c+2−0
@@ -1760,6 +1760,8 @@ tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) · 1759 unmodified lines
1760 return &bpf_sk_storage_delete_tracing_proto;1760 return &bpf_sk_storage_delete_tracing_proto;
1761 case BPF_FUNC_sock_from_file:1761 case BPF_FUNC_sock_from_file:
1762 return &bpf_sock_from_file_proto;1762 return &bpf_sock_from_file_proto;
1763 case BPF_FUNC_get_socket_cookie:
1764 return &bpf_get_socket_ptr_cookie_proto;
1763#endif1765#endif
1764 case BPF_FUNC_seq_printf:1766 case BPF_FUNC_seq_printf:
1765 return prog->expected_attach_type == BPF_TRACE_ITER ?1767 return prog->expected_attach_type == BPF_TRACE_ITER ?
net/core/filter.c+12−0
@@ -4631,6 +4631,18 @@ static const struct bpf_func_proto bpf_get_socket_cookie_sock_proto = { · 4630 unmodified lines
4631 .arg1_type = ARG_PTR_TO_CTX,4631 .arg1_type = ARG_PTR_TO_CTX,
4632};4632};
4633 4633
4634BPF_CALL_1(bpf_get_socket_ptr_cookie, struct sock *, sk)
4635{
4636 return sk ? sock_gen_cookie(sk) : 0;
4637}
4638
4639const 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
4634BPF_CALL_1(bpf_get_socket_cookie_sock_ops, struct bpf_sock_ops_kern *, ctx)4646BPF_CALL_1(bpf_get_socket_cookie_sock_ops, struct bpf_sock_ops_kern *, ctx)
4635{4647{
4636 return __sock_gen_cookie(ctx->sk);4648 return __sock_gen_cookie(ctx->sk);
tools/include/uapi/linux/bpf.h+8−0
@@ -1673,6 +1673,14 @@ union bpf_attr { · 1672 unmodified lines
1673 * Return1673 * Return
1674 * A 8-byte long unique number.1674 * A 8-byte long unique number.
1675 *1675 *
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 *
1676 * u32 bpf_get_socket_uid(struct sk_buff *skb)1684 * u32 bpf_get_socket_uid(struct sk_buff *skb)
1677 * Return1685 * Return
1678 * The owner UID of the socket associated to *skb*. If the socket1686 * The owner UID of the socket associated to *skb*. If the socket

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t