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
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+8−0
@@ -1673,6 +1673,14 @@ union bpf_attr { · 1672 unmodified lines
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+2−0
@@ -1760,6 +1760,8 @@ tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) · 1759 unmodified lines
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+12−0
@@ -4631,6 +4631,18 @@ static const struct bpf_func_proto bpf_get_socket_cookie_sock_proto = { · 4630 unmodified lines
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+8−0
@@ -1673,6 +1673,14 @@ union bpf_attr { · 1672 unmodified lines
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

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