Commit 0d4fad3e authored by Yonghong Song's avatar Yonghong Song Committed by Alexei Starovoitov

bpf: Add bpf_skc_to_udp6_sock() helper

The helper is used in tracing programs to cast a socket
pointer to a udp6_sock pointer.
The return value could be NULL if the casting is illegal.
Signed-off-by: default avatarYonghong Song <yhs@fb.com>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
Acked-by: default avatarMartin KaFai Lau <kafai@fb.com>
Cc: Eric Dumazet <edumazet@google.com>
Link: https://lore.kernel.org/bpf/20200623230815.3988481-1-yhs@fb.com
parent 5788b3a0
...@@ -1653,6 +1653,7 @@ extern const struct bpf_func_proto bpf_skc_to_tcp6_sock_proto; ...@@ -1653,6 +1653,7 @@ extern const struct bpf_func_proto bpf_skc_to_tcp6_sock_proto;
extern const struct bpf_func_proto bpf_skc_to_tcp_sock_proto; extern const struct bpf_func_proto bpf_skc_to_tcp_sock_proto;
extern const struct bpf_func_proto bpf_skc_to_tcp_timewait_sock_proto; extern const struct bpf_func_proto bpf_skc_to_tcp_timewait_sock_proto;
extern const struct bpf_func_proto bpf_skc_to_tcp_request_sock_proto; extern const struct bpf_func_proto bpf_skc_to_tcp_request_sock_proto;
extern const struct bpf_func_proto bpf_skc_to_udp6_sock_proto;
const struct bpf_func_proto *bpf_tracing_func_proto( const struct bpf_func_proto *bpf_tracing_func_proto(
enum bpf_func_id func_id, const struct bpf_prog *prog); enum bpf_func_id func_id, const struct bpf_prog *prog);
......
...@@ -3279,6 +3279,12 @@ union bpf_attr { ...@@ -3279,6 +3279,12 @@ union bpf_attr {
* Dynamically cast a *sk* pointer to a *tcp_request_sock* pointer. * Dynamically cast a *sk* pointer to a *tcp_request_sock* pointer.
* Return * Return
* *sk* if casting is valid, or NULL otherwise. * *sk* if casting is valid, or NULL otherwise.
*
* struct udp6_sock *bpf_skc_to_udp6_sock(void *sk)
* Description
* Dynamically cast a *sk* pointer to a *udp6_sock* pointer.
* Return
* *sk* if casting is valid, or NULL otherwise.
*/ */
#define __BPF_FUNC_MAPPER(FN) \ #define __BPF_FUNC_MAPPER(FN) \
FN(unspec), \ FN(unspec), \
...@@ -3420,7 +3426,8 @@ union bpf_attr { ...@@ -3420,7 +3426,8 @@ union bpf_attr {
FN(skc_to_tcp6_sock), \ FN(skc_to_tcp6_sock), \
FN(skc_to_tcp_sock), \ FN(skc_to_tcp_sock), \
FN(skc_to_tcp_timewait_sock), \ FN(skc_to_tcp_timewait_sock), \
FN(skc_to_tcp_request_sock), FN(skc_to_tcp_request_sock), \
FN(skc_to_udp6_sock),
/* integer value in 'imm' field of BPF_CALL instruction selects which helper /* integer value in 'imm' field of BPF_CALL instruction selects which helper
* function eBPF program intends to call * function eBPF program intends to call
......
...@@ -1523,6 +1523,8 @@ tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) ...@@ -1523,6 +1523,8 @@ tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
return &bpf_skc_to_tcp_timewait_sock_proto; return &bpf_skc_to_tcp_timewait_sock_proto;
case BPF_FUNC_skc_to_tcp_request_sock: case BPF_FUNC_skc_to_tcp_request_sock:
return &bpf_skc_to_tcp_request_sock_proto; return &bpf_skc_to_tcp_request_sock_proto;
case BPF_FUNC_skc_to_udp6_sock:
return &bpf_skc_to_udp6_sock_proto;
#endif #endif
case BPF_FUNC_seq_printf: case BPF_FUNC_seq_printf:
return prog->expected_attach_type == BPF_TRACE_ITER ? return prog->expected_attach_type == BPF_TRACE_ITER ?
......
...@@ -9369,3 +9369,25 @@ const struct bpf_func_proto bpf_skc_to_tcp_request_sock_proto = { ...@@ -9369,3 +9369,25 @@ const struct bpf_func_proto bpf_skc_to_tcp_request_sock_proto = {
.check_btf_id = check_arg_btf_id, .check_btf_id = check_arg_btf_id,
.ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_TCP_REQ], .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_TCP_REQ],
}; };
BPF_CALL_1(bpf_skc_to_udp6_sock, struct sock *, sk)
{
/* udp6_sock type is not generated in dwarf and hence btf,
* trigger an explicit type generation here.
*/
BTF_TYPE_EMIT(struct udp6_sock);
if (sk_fullsock(sk) && sk->sk_protocol == IPPROTO_UDP &&
sk->sk_type == SOCK_DGRAM && sk->sk_family == AF_INET6)
return (unsigned long)sk;
return (unsigned long)NULL;
}
const struct bpf_func_proto bpf_skc_to_udp6_sock_proto = {
.func = bpf_skc_to_udp6_sock,
.gpl_only = false,
.ret_type = RET_PTR_TO_BTF_ID_OR_NULL,
.arg1_type = ARG_PTR_TO_BTF_ID,
.check_btf_id = check_arg_btf_id,
.ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_UDP6],
};
...@@ -425,6 +425,7 @@ class PrinterHelpers(Printer): ...@@ -425,6 +425,7 @@ class PrinterHelpers(Printer):
'struct tcp_sock', 'struct tcp_sock',
'struct tcp_timewait_sock', 'struct tcp_timewait_sock',
'struct tcp_request_sock', 'struct tcp_request_sock',
'struct udp6_sock',
'struct __sk_buff', 'struct __sk_buff',
'struct sk_msg_md', 'struct sk_msg_md',
...@@ -466,6 +467,7 @@ class PrinterHelpers(Printer): ...@@ -466,6 +467,7 @@ class PrinterHelpers(Printer):
'struct tcp_sock', 'struct tcp_sock',
'struct tcp_timewait_sock', 'struct tcp_timewait_sock',
'struct tcp_request_sock', 'struct tcp_request_sock',
'struct udp6_sock',
} }
mapped_types = { mapped_types = {
'u8': '__u8', 'u8': '__u8',
......
...@@ -3279,6 +3279,12 @@ union bpf_attr { ...@@ -3279,6 +3279,12 @@ union bpf_attr {
* Dynamically cast a *sk* pointer to a *tcp_request_sock* pointer. * Dynamically cast a *sk* pointer to a *tcp_request_sock* pointer.
* Return * Return
* *sk* if casting is valid, or NULL otherwise. * *sk* if casting is valid, or NULL otherwise.
*
* struct udp6_sock *bpf_skc_to_udp6_sock(void *sk)
* Description
* Dynamically cast a *sk* pointer to a *udp6_sock* pointer.
* Return
* *sk* if casting is valid, or NULL otherwise.
*/ */
#define __BPF_FUNC_MAPPER(FN) \ #define __BPF_FUNC_MAPPER(FN) \
FN(unspec), \ FN(unspec), \
...@@ -3420,7 +3426,8 @@ union bpf_attr { ...@@ -3420,7 +3426,8 @@ union bpf_attr {
FN(skc_to_tcp6_sock), \ FN(skc_to_tcp6_sock), \
FN(skc_to_tcp_sock), \ FN(skc_to_tcp_sock), \
FN(skc_to_tcp_timewait_sock), \ FN(skc_to_tcp_timewait_sock), \
FN(skc_to_tcp_request_sock), FN(skc_to_tcp_request_sock), \
FN(skc_to_udp6_sock),
/* integer value in 'imm' field of BPF_CALL instruction selects which helper /* integer value in 'imm' field of BPF_CALL instruction selects which helper
* function eBPF program intends to call * function eBPF program intends to call
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment