Skip to content

Commit edf54a2

Browse files
mrpreKernel Patches Daemon
authored and
Kernel Patches Daemon
committed
bpf, sockmap: Introduce tracing capability for sockmap
Sockmap has the same high-performance forwarding capability as XDP, but operates at Layer 7. Introduce tracing capability for sockmap, similar to XDP, to trace the execution results of BPF programs without modifying the programs themselves, similar to the existing trace_xdp_redirect{_map}. It is crucial for debugging BPF programs, especially in production environments. Additionally, a header file was added to bpf_trace.h to automatically generate tracepoints. Test results: $ echo "1" > /sys/kernel/tracing/events/sockmap/enable skb: sockmap_redirect: sk=00000000d3266a8d, type=skb, family=2, protocol=6, \ prog_id=73, length=256, action=PASS msg: sockmap_redirect: sk=00000000528c7614, type=msg, family=2, protocol=6, \ prog_id=185, length=5, action=REDIRECT tls: sockmap_redirect: sk=00000000d04d2224, type=skb, family=2, protocol=6, \ prog_id=143, length=35, action=PASS strparser: sockmap_skb_strp_parse: sk=00000000ecab0b30, family=2, protocol=6, \ prog_id=170, size=5 Signed-off-by: Jiayuan Chen <[email protected]>
1 parent a530d65 commit edf54a2

File tree

4 files changed

+97
-1
lines changed

4 files changed

+97
-1
lines changed

MAINTAINERS

+1
Original file line numberDiff line numberDiff line change
@@ -4420,6 +4420,7 @@ L: [email protected]
44204420
44214421
S: Maintained
44224422
F: include/linux/skmsg.h
4423+
F: include/trace/events/sockmap.h
44234424
F: net/core/skmsg.c
44244425
F: net/core/sock_map.c
44254426
F: net/ipv4/tcp_bpf.c

include/linux/bpf_trace.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
#define __LINUX_BPF_TRACE_H__
44

55
#include <trace/events/xdp.h>
6-
6+
#include <trace/events/sockmap.h>
77
#endif /* __LINUX_BPF_TRACE_H__ */

include/trace/events/sockmap.h

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
#undef TRACE_SYSTEM
3+
#define TRACE_SYSTEM sockmap
4+
5+
#if !defined(_TRACE_SOCKMAP_H) || defined(TRACE_HEADER_MULTI_READ)
6+
#define _TRACE_SOCKMAP_H
7+
8+
#include <linux/filter.h>
9+
#include <linux/tracepoint.h>
10+
#include <linux/bpf.h>
11+
#include <linux/skmsg.h>
12+
13+
TRACE_DEFINE_ENUM(__SK_DROP);
14+
TRACE_DEFINE_ENUM(__SK_PASS);
15+
TRACE_DEFINE_ENUM(__SK_REDIRECT);
16+
TRACE_DEFINE_ENUM(__SK_NONE);
17+
18+
#define show_act(x) \
19+
__print_symbolic(x, \
20+
{ __SK_DROP, "DROP" }, \
21+
{ __SK_PASS, "PASS" }, \
22+
{ __SK_REDIRECT, "REDIRECT" }, \
23+
{ __SK_NONE, "NONE" })
24+
25+
#define trace_sockmap_skmsg_redirect(sk, prog, msg, act) \
26+
trace_sockmap_redirect((sk), "msg", (prog), (msg)->sg.size, (act))
27+
28+
#define trace_sockmap_skb_redirect(sk, prog, skb, act) \
29+
trace_sockmap_redirect((sk), "skb", (prog), (skb)->len, (act))
30+
31+
TRACE_EVENT(sockmap_redirect,
32+
TP_PROTO(const struct sock *sk, const char *type,
33+
const struct bpf_prog *prog, int length, int act),
34+
TP_ARGS(sk, type, prog, length, act),
35+
36+
TP_STRUCT__entry(
37+
__field(const void *, sk)
38+
__field(const char *, type)
39+
__field(__u16, family)
40+
__field(__u16, protocol)
41+
__field(int, prog_id)
42+
__field(int, length)
43+
__field(int, act)
44+
),
45+
46+
TP_fast_assign(
47+
__entry->sk = sk;
48+
__entry->type = type;
49+
__entry->family = sk->sk_family;
50+
__entry->protocol = sk->sk_protocol;
51+
__entry->prog_id = prog->aux->id;
52+
__entry->length = length;
53+
__entry->act = act;
54+
),
55+
56+
TP_printk("sk=%p, type=%s, family=%d, protocol=%d, prog_id=%d, length=%d, action=%s",
57+
__entry->sk, __entry->type, __entry->family, __entry->protocol,
58+
__entry->prog_id, __entry->length,
59+
show_act(__entry->act))
60+
);
61+
62+
TRACE_EVENT(sockmap_skb_strp_parse,
63+
TP_PROTO(const struct sock *sk, const struct bpf_prog *prog,
64+
int size),
65+
TP_ARGS(sk, prog, size),
66+
67+
TP_STRUCT__entry(
68+
__field(const void *, sk)
69+
__field(__u16, family)
70+
__field(__u16, protocol)
71+
__field(int, prog_id)
72+
__field(int, size)
73+
),
74+
75+
TP_fast_assign(
76+
__entry->sk = sk;
77+
__entry->family = sk->sk_family;
78+
__entry->protocol = sk->sk_protocol;
79+
__entry->prog_id = prog->aux->id;
80+
__entry->size = size;
81+
),
82+
83+
TP_printk("sk=%p, family=%d, protocol=%d, prog_id=%d, size=%d",
84+
__entry->sk, __entry->family, __entry->protocol,
85+
__entry->prog_id, __entry->size)
86+
);
87+
#endif /* _TRACE_SOCKMAP_H */
88+
89+
#include <trace/define_trace.h>

net/core/skmsg.c

+6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <net/tcp.h>
1010
#include <net/tls.h>
1111
#include <trace/events/sock.h>
12+
#include <trace/events/sockmap.h>
1213

1314
static bool sk_msg_try_coalesce_ok(struct sk_msg *msg, int elem_first_coalesce)
1415
{
@@ -910,6 +911,7 @@ int sk_psock_msg_verdict(struct sock *sk, struct sk_psock *psock,
910911
sock_hold(psock->sk_redir);
911912
}
912913
out:
914+
trace_sockmap_skmsg_redirect(sk, prog, msg, ret);
913915
rcu_read_unlock();
914916
return ret;
915917
}
@@ -981,6 +983,7 @@ int sk_psock_tls_strp_read(struct sk_psock *psock, struct sk_buff *skb)
981983
ret = bpf_prog_run_pin_on_cpu(prog, skb);
982984
ret = sk_psock_map_verd(ret, skb_bpf_redirect_fetch(skb));
983985
skb->sk = NULL;
986+
trace_sockmap_skb_redirect(psock->sk, prog, skb, ret);
984987
}
985988
sk_psock_tls_verdict_apply(skb, psock, ret);
986989
rcu_read_unlock();
@@ -1090,6 +1093,7 @@ static void sk_psock_strp_read(struct strparser *strp, struct sk_buff *skb)
10901093
skb_bpf_set_strparser(skb);
10911094
ret = sk_psock_map_verd(ret, skb_bpf_redirect_fetch(skb));
10921095
skb->sk = NULL;
1096+
trace_sockmap_skb_redirect(sk, prog, skb, ret);
10931097
}
10941098
sk_psock_verdict_apply(psock, skb, ret);
10951099
out:
@@ -1113,6 +1117,7 @@ static int sk_psock_strp_parse(struct strparser *strp, struct sk_buff *skb)
11131117
skb->sk = psock->sk;
11141118
ret = bpf_prog_run_pin_on_cpu(prog, skb);
11151119
skb->sk = NULL;
1120+
trace_sockmap_skb_strp_parse(psock->sk, prog, ret);
11161121
}
11171122
rcu_read_unlock();
11181123
return ret;
@@ -1217,6 +1222,7 @@ static int sk_psock_verdict_recv(struct sock *sk, struct sk_buff *skb)
12171222
skb_bpf_redirect_clear(skb);
12181223
ret = bpf_prog_run_pin_on_cpu(prog, skb);
12191224
ret = sk_psock_map_verd(ret, skb_bpf_redirect_fetch(skb));
1225+
trace_sockmap_skb_redirect(psock->sk, prog, skb, ret);
12201226
}
12211227
ret = sk_psock_verdict_apply(psock, skb, ret);
12221228
if (ret < 0)

0 commit comments

Comments
 (0)