Skip to content

Commit

Permalink
bpf: Expose a bpf_sock_from_file helper to tracing programs
Browse files Browse the repository at this point in the history
eBPF programs can already check whether a file is a socket using
file->f_op == &socket_file_ops but they can not convert file->private_data
into a struct socket with BTF information. For that, we need a new
helper that is essentially just a wrapper for sock_from_file.

sock_from_file can set an err value but this is only set to -ENOTSOCK
when the return value is NULL so it's useless superfluous information.

Signed-off-by: Florent Revest <revest@google.com>
  • Loading branch information
Florent Revest authored and intel-lab-lkp committed Nov 12, 2020
1 parent 09a3dac commit 7fd0725
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
7 changes: 7 additions & 0 deletions include/uapi/linux/bpf.h
Expand Up @@ -3787,6 +3787,12 @@ union bpf_attr {
* *ARG_PTR_TO_BTF_ID* of type *task_struct*.
* Return
* Pointer to the current task.
*
* struct socket *bpf_sock_from_file(struct file *file)
* Description
* If the given file contains a socket, returns the associated socket.
* Return
* A pointer to a struct socket on success, or NULL on failure.
*/
#define __BPF_FUNC_MAPPER(FN) \
FN(unspec), \
Expand Down Expand Up @@ -3948,6 +3954,7 @@ union bpf_attr {
FN(task_storage_get), \
FN(task_storage_delete), \
FN(get_current_task_btf), \
FN(sock_from_file), \
/* */

/* integer value in 'imm' field of BPF_CALL instruction selects which helper
Expand Down
22 changes: 22 additions & 0 deletions kernel/trace/bpf_trace.c
Expand Up @@ -1253,6 +1253,26 @@ const struct bpf_func_proto bpf_snprintf_btf_proto = {
.arg5_type = ARG_ANYTHING,
};

BPF_CALL_1(bpf_sock_from_file, struct file *, file)
{
int err;

return (unsigned long) sock_from_file(file, &err);
}

BTF_ID_LIST(bpf_sock_from_file_btf_ids)
BTF_ID(struct, socket)
BTF_ID(struct, file)

const struct bpf_func_proto bpf_sock_from_file_proto = {
.func = bpf_sock_from_file,
.gpl_only = true,
.ret_type = RET_PTR_TO_BTF_ID_OR_NULL,
.ret_btf_id = &bpf_sock_from_file_btf_ids[0],
.arg1_type = ARG_PTR_TO_BTF_ID,
.arg1_btf_id = &bpf_sock_from_file_btf_ids[1],
};

const struct bpf_func_proto *
bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
{
Expand Down Expand Up @@ -1347,6 +1367,8 @@ bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
return &bpf_per_cpu_ptr_proto;
case BPF_FUNC_bpf_this_cpu_ptr:
return &bpf_this_cpu_ptr_proto;
case BPF_FUNC_sock_from_file:
return &bpf_sock_from_file_proto;
default:
return NULL;
}
Expand Down
4 changes: 4 additions & 0 deletions scripts/bpf_helpers_doc.py
Expand Up @@ -434,6 +434,8 @@ class PrinterHelpers(Printer):
'struct xdp_md',
'struct path',
'struct btf_ptr',
'struct socket',
'struct file',
]
known_types = {
'...',
Expand Down Expand Up @@ -477,6 +479,8 @@ class PrinterHelpers(Printer):
'struct task_struct',
'struct path',
'struct btf_ptr',
'struct socket',
'struct file',
}
mapped_types = {
'u8': '__u8',
Expand Down
7 changes: 7 additions & 0 deletions tools/include/uapi/linux/bpf.h
Expand Up @@ -3787,6 +3787,12 @@ union bpf_attr {
* *ARG_PTR_TO_BTF_ID* of type *task_struct*.
* Return
* Pointer to the current task.
*
* struct socket *bpf_sock_from_file(struct file *file)
* Description
* If the given file contains a socket, returns the associated socket.
* Return
* A pointer to a struct socket on success, or NULL on failure.
*/
#define __BPF_FUNC_MAPPER(FN) \
FN(unspec), \
Expand Down Expand Up @@ -3948,6 +3954,7 @@ union bpf_attr {
FN(task_storage_get), \
FN(task_storage_delete), \
FN(get_current_task_btf), \
FN(sock_from_file), \
/* */

/* integer value in 'imm' field of BPF_CALL instruction selects which helper
Expand Down

0 comments on commit 7fd0725

Please sign in to comment.