Skip to content

Commit 0d4fad3

Browse files
yonghong-songAlexei Starovoitov
authored and
Alexei Starovoitov
committed
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: Yonghong Song <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Acked-by: Martin KaFai Lau <[email protected]> Cc: Eric Dumazet <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 5788b3a commit 0d4fad3

File tree

6 files changed

+43
-2
lines changed

6 files changed

+43
-2
lines changed

include/linux/bpf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1653,6 +1653,7 @@ extern const struct bpf_func_proto bpf_skc_to_tcp6_sock_proto;
16531653
extern const struct bpf_func_proto bpf_skc_to_tcp_sock_proto;
16541654
extern const struct bpf_func_proto bpf_skc_to_tcp_timewait_sock_proto;
16551655
extern const struct bpf_func_proto bpf_skc_to_tcp_request_sock_proto;
1656+
extern const struct bpf_func_proto bpf_skc_to_udp6_sock_proto;
16561657

16571658
const struct bpf_func_proto *bpf_tracing_func_proto(
16581659
enum bpf_func_id func_id, const struct bpf_prog *prog);

include/uapi/linux/bpf.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3279,6 +3279,12 @@ union bpf_attr {
32793279
* Dynamically cast a *sk* pointer to a *tcp_request_sock* pointer.
32803280
* Return
32813281
* *sk* if casting is valid, or NULL otherwise.
3282+
*
3283+
* struct udp6_sock *bpf_skc_to_udp6_sock(void *sk)
3284+
* Description
3285+
* Dynamically cast a *sk* pointer to a *udp6_sock* pointer.
3286+
* Return
3287+
* *sk* if casting is valid, or NULL otherwise.
32823288
*/
32833289
#define __BPF_FUNC_MAPPER(FN) \
32843290
FN(unspec), \
@@ -3420,7 +3426,8 @@ union bpf_attr {
34203426
FN(skc_to_tcp6_sock), \
34213427
FN(skc_to_tcp_sock), \
34223428
FN(skc_to_tcp_timewait_sock), \
3423-
FN(skc_to_tcp_request_sock),
3429+
FN(skc_to_tcp_request_sock), \
3430+
FN(skc_to_udp6_sock),
34243431

34253432
/* integer value in 'imm' field of BPF_CALL instruction selects which helper
34263433
* function eBPF program intends to call

kernel/trace/bpf_trace.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1523,6 +1523,8 @@ tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
15231523
return &bpf_skc_to_tcp_timewait_sock_proto;
15241524
case BPF_FUNC_skc_to_tcp_request_sock:
15251525
return &bpf_skc_to_tcp_request_sock_proto;
1526+
case BPF_FUNC_skc_to_udp6_sock:
1527+
return &bpf_skc_to_udp6_sock_proto;
15261528
#endif
15271529
case BPF_FUNC_seq_printf:
15281530
return prog->expected_attach_type == BPF_TRACE_ITER ?

net/core/filter.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9369,3 +9369,25 @@ const struct bpf_func_proto bpf_skc_to_tcp_request_sock_proto = {
93699369
.check_btf_id = check_arg_btf_id,
93709370
.ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_TCP_REQ],
93719371
};
9372+
9373+
BPF_CALL_1(bpf_skc_to_udp6_sock, struct sock *, sk)
9374+
{
9375+
/* udp6_sock type is not generated in dwarf and hence btf,
9376+
* trigger an explicit type generation here.
9377+
*/
9378+
BTF_TYPE_EMIT(struct udp6_sock);
9379+
if (sk_fullsock(sk) && sk->sk_protocol == IPPROTO_UDP &&
9380+
sk->sk_type == SOCK_DGRAM && sk->sk_family == AF_INET6)
9381+
return (unsigned long)sk;
9382+
9383+
return (unsigned long)NULL;
9384+
}
9385+
9386+
const struct bpf_func_proto bpf_skc_to_udp6_sock_proto = {
9387+
.func = bpf_skc_to_udp6_sock,
9388+
.gpl_only = false,
9389+
.ret_type = RET_PTR_TO_BTF_ID_OR_NULL,
9390+
.arg1_type = ARG_PTR_TO_BTF_ID,
9391+
.check_btf_id = check_arg_btf_id,
9392+
.ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_UDP6],
9393+
};

scripts/bpf_helpers_doc.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,7 @@ class PrinterHelpers(Printer):
425425
'struct tcp_sock',
426426
'struct tcp_timewait_sock',
427427
'struct tcp_request_sock',
428+
'struct udp6_sock',
428429

429430
'struct __sk_buff',
430431
'struct sk_msg_md',
@@ -466,6 +467,7 @@ class PrinterHelpers(Printer):
466467
'struct tcp_sock',
467468
'struct tcp_timewait_sock',
468469
'struct tcp_request_sock',
470+
'struct udp6_sock',
469471
}
470472
mapped_types = {
471473
'u8': '__u8',

tools/include/uapi/linux/bpf.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3279,6 +3279,12 @@ union bpf_attr {
32793279
* Dynamically cast a *sk* pointer to a *tcp_request_sock* pointer.
32803280
* Return
32813281
* *sk* if casting is valid, or NULL otherwise.
3282+
*
3283+
* struct udp6_sock *bpf_skc_to_udp6_sock(void *sk)
3284+
* Description
3285+
* Dynamically cast a *sk* pointer to a *udp6_sock* pointer.
3286+
* Return
3287+
* *sk* if casting is valid, or NULL otherwise.
32823288
*/
32833289
#define __BPF_FUNC_MAPPER(FN) \
32843290
FN(unspec), \
@@ -3420,7 +3426,8 @@ union bpf_attr {
34203426
FN(skc_to_tcp6_sock), \
34213427
FN(skc_to_tcp_sock), \
34223428
FN(skc_to_tcp_timewait_sock), \
3423-
FN(skc_to_tcp_request_sock),
3429+
FN(skc_to_tcp_request_sock), \
3430+
FN(skc_to_udp6_sock),
34243431

34253432
/* integer value in 'imm' field of BPF_CALL instruction selects which helper
34263433
* function eBPF program intends to call

0 commit comments

Comments
 (0)