Skip to contentSkip to conversation

bpf, sockmap: Introduce tracing capability for sockmap

PATCH Superseded Jiayuan Chen 2 participants 5 messages Apr 14, 2025 – Apr 17, 2025

Subsystems (6)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Jiayuan Chen

author maintainer
#0 lore raw

PATCH v3 1/2 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, 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 sockmap programs, especially in production environments.

Additionally, the new header file has to be added to bpf_trace.h to automatically generate tracepoints.

Test results:

$ echo "1" > /sys/kernel/tracing/events/sockmap/enable

msg/skb:
'''
sockmap_redirect: sk=000000000ec02a93, netns=4026531840, inode=318, \
family=2, protocol=6, prog_id=59, len=8192, type=msg, action=REDIRECT, \
redirect_type=ingress

sockmap_redirect: sk=00000000d5d9c931, netns=4026531840, inode=64731, \
family=2, protocol=6, prog_id=91, len=8221, type=skb, action=REDIRECT, \
redirect_type=egress

sockmap_redirect: sk=00000000106fc281, netns=4026531840, inode=64729, \
family=2, protocol=6, prog_id=94, len=8192, type=msg, action=PASS, \
redirect_type=none
'''

strparser:
'''
sockmap_strparser: sk=00000000f15fc1c8, netns=4026531840, inode=52396, \
family=2, protocol=6, prog_id=143, in_len=1000, full_len=10 '''

---
v1 -> v2: Print more valuable information as suggested by the maintainer.

---
 MAINTAINERS                    |   1 +
 include/linux/bpf_trace.h      |   1 +
 include/trace/events/sockmap.h | 158 +++++++++++++++++++++++++++++++++
 net/core/skmsg.c               |   6 ++
 4 files changed, 166 insertions(+)
 create mode 100644 include/trace/events/sockmap.h
MAINTAINERS +1 -0
index a7a1d121a83e..578e16d86853 100644
@@ -4420,6 +4420,7 @@ L:	[email protected]
 L:	[email protected]
 S:	Maintained
 F:	include/linux/skmsg.h
+F:	include/trace/events/sockmap.h
 F:	net/core/skmsg.c
 F:	net/core/sock_map.c
 F:	net/ipv4/tcp_bpf.c
include/linux/bpf_trace.h +1 -0
index ddf896abcfb6..d559be0a79c5 100644
@@ -3,5 +3,6 @@
 #define __LINUX_BPF_TRACE_H__
 
 #include <trace/events/xdp.h>
+#include <trace/events/sockmap.h>
 
 #endif /* __LINUX_BPF_TRACE_H__ */
include/trace/events/sockmap.h new file +158 -0

158 lines not on this page · raw message

net/core/skmsg.c +6 -0
index 276934673066..517596efafa8 100644
@@ -9,6 +9,7 @@
 #include <net/tcp.h>
 #include <net/tls.h>
 #include <trace/events/sock.h>
+#include <trace/events/sockmap.h>
 
 static bool sk_msg_try_coalesce_ok(struct sk_msg *msg, int elem_first_coalesce)
 {
@@ -910,6 +911,7 @@ int sk_psock_msg_verdict(struct sock *sk, struct sk_psock *psock,
 		sock_hold(psock->sk_redir);
 	}
 out:
+	trace_sockmap_skmsg_redirect(sk, prog, msg, ret);
 	rcu_read_unlock();
 	return ret;
 }
@@ -981,6 +983,7 @@ int sk_psock_tls_strp_read(struct sk_psock *psock, struct sk_buff *skb)
 		ret = bpf_prog_run_pin_on_cpu(prog, skb);
 		ret = sk_psock_map_verd(ret, skb_bpf_redirect_fetch(skb));
 		skb->sk = NULL;
+		trace_sockmap_skb_redirect(psock->sk, prog, skb, ret);
 	}
 	sk_psock_tls_verdict_apply(skb, psock, ret);
 	rcu_read_unlock();
@@ -1090,6 +1093,7 @@ static void sk_psock_strp_read(struct strparser *strp, struct sk_buff *skb)
 		skb_bpf_set_strparser(skb);
 		ret = sk_psock_map_verd(ret, skb_bpf_redirect_fetch(skb));
 		skb->sk = NULL;
+		trace_sockmap_skb_redirect(sk, prog, skb, ret);
 	}
 	sk_psock_verdict_apply(psock, skb, ret);
 out:
@@ -1113,6 +1117,7 @@ static int sk_psock_strp_parse(struct strparser *strp, struct sk_buff *skb)
 		skb->sk = psock->sk;
 		ret = bpf_prog_run_pin_on_cpu(prog, skb);
 		skb->sk = NULL;
+		trace_sockmap_skb_strp_parse(psock->sk, prog, skb, ret);
 	}
 	rcu_read_unlock();
 	return ret;
@@ -1217,6 +1222,7 @@ static int sk_psock_verdict_recv(struct sock *sk, struct sk_buff *skb)
 		skb_bpf_redirect_clear(skb);
 		ret = bpf_prog_run_pin_on_cpu(prog, skb);
 		ret = sk_psock_map_verd(ret, skb_bpf_redirect_fetch(skb));
+		trace_sockmap_skb_redirect(psock->sk, prog, skb, ret);
 	}
 	ret = sk_psock_verdict_apply(psock, skb, ret);
 	if (ret < 0)
Signature · 1 line
2.47.1
author maintainer
#1 lore raw

PATCH v3 2/2 bpf: relocates the BPF net tracepoint definitions

This commit relocates the BPF tracepoint definitions for XDP and sockmap from the kernel directory to net/bpf.

This ensures that these tracepoints are controlled by the CONFIG_NET, avoiding unnecessary function definitions when the CONFIG_NET is disabled. Additionally, it prevents build failures caused by the use of net module functions when CONFIG_NET is not enabled.

---
 kernel/bpf/core.c       | 7 -------
 net/bpf/Makefile        | 1 +
 net/bpf/bpf_net_trace.c | 8 ++++++++
 3 files changed, 9 insertions(+), 7 deletions(-)
 create mode 100644 net/bpf/bpf_net_trace.c
kernel/bpf/core.c +0 -7
index ba6b6118cf50..54e570f62606 100644
@@ -3180,10 +3180,3 @@ late_initcall(bpf_global_ma_init);
 
 DEFINE_STATIC_KEY_FALSE(bpf_stats_enabled_key);
 EXPORT_SYMBOL(bpf_stats_enabled_key);
-
-/* All definitions of tracepoints related to BPF. */
-#define CREATE_TRACE_POINTS
-#include <linux/bpf_trace.h>
-
-EXPORT_TRACEPOINT_SYMBOL_GPL(xdp_exception);
-EXPORT_TRACEPOINT_SYMBOL_GPL(xdp_bulk_tx);
net/bpf/Makefile +1 -0
index 1ebe270bde23..e95453053159 100644
@@ -1,5 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0-only
 obj-$(CONFIG_BPF_SYSCALL)	:= test_run.o
+obj-$(CONFIG_BPF_SYSCALL)	+= bpf_net_trace.o
 ifeq ($(CONFIG_BPF_JIT),y)
 obj-$(CONFIG_BPF_SYSCALL)	+= bpf_dummy_struct_ops.o
 endif
net/bpf/bpf_net_trace.c new file +8 -0
new file mode 100644
index 000000000000..e7c0537dbffd
@@ -0,0 +1,8 @@
+// SPDX-License-Identifier: GPL-2.0
+
+/* All definitions of net tracepoints related to BPF. */
+#define CREATE_TRACE_POINTS
+#include <linux/bpf_trace.h>
+
+EXPORT_TRACEPOINT_SYMBOL_GPL(xdp_exception);
+EXPORT_TRACEPOINT_SYMBOL_GPL(xdp_bulk_tx);
Signature · 1 line
2.47.1
#2 lore raw
In reply to Jiayuan Chen

include/trace/events/sockmap.h line 19

Quoted 8 lines · Jiayuan Chen
include/trace/events/sockmap.h new file +8 -0
+#ifndef __TRACE_SOCKMAP_HELPER_ONCE_ONLY
+#define __TRACE_SOCKMAP_HELPER_ONCE_ONLY
+
+enum sockmap_direct_type {
+	SOCKMAP_REDIR_NONE	= 0,
+	SOCKMAP_REDIR_INGRESS,
+	SOCKMAP_REDIR_EGRESS,
+};

I am curious why you need to define them here since you already pass 'ingress' as a parameter? Is it possible to reuse the BPF_F_INGRESS bit?

Thanks!

author maintainer
#3 lore raw
Quoted 14 lines · Cong Wang
Quoted 10 lines · Jiayuan Chen

+#ifndef __TRACE_SOCKMAP_HELPER_ONCE_ONLY
+#define __TRACE_SOCKMAP_HELPER_ONCE_ONLY
+
+enum sockmap_direct_type {
+ SOCKMAP_REDIR_NONE = 0,
+ SOCKMAP_REDIR_INGRESS,
+ SOCKMAP_REDIR_EGRESS,
+};

Quoted 4 lines

I am curious why you need to define them here since you already pass 'ingress' as a parameter? Is it possible to reuse the BPF_F_INGRESS bit? Thanks!

The lowest bit of skb->_redir being 0 indicates EGRESS, so we cannot use the built-in __print_flag for output in this case, since it requires the corresponding bit to be set to 1.

We could certainly do this instead:
'''
if (act != REDIRECT)
redir = "none"
else if (flag & BPF_F_INGRESS)
redir = "ingress"
else
redir = "egress"
'''
However, as Steven mentioned earlier, using an enum instead would be better here for trace_event.

Of course, we could directly print the hexadecimal value of _redir, but that would result in poor readability.

Thanks~

Reviewed-by on v3 1/2
#4 lore raw
In reply to Jiayuan Chen
Quoted 37 lines · Jiayuan Chen

Sockmap has the same high-performance forwarding capability as XDP, but operates at Layer 7.

Introduce tracing capability for sockmap, 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 sockmap programs, especially in production environments.

Additionally, the new header file has to be added to bpf_trace.h to automatically generate tracepoints.

Test results:

$ echo "1" > /sys/kernel/tracing/events/sockmap/enable

msg/skb:
'''
sockmap_redirect: sk=000000000ec02a93, netns=4026531840, inode=318, \
family=2, protocol=6, prog_id=59, len=8192, type=msg, action=REDIRECT, \
redirect_type=ingress

sockmap_redirect: sk=00000000d5d9c931, netns=4026531840, inode=64731, \
family=2, protocol=6, prog_id=91, len=8221, type=skb, action=REDIRECT, \
redirect_type=egress

sockmap_redirect: sk=00000000106fc281, netns=4026531840, inode=64729, \
family=2, protocol=6, prog_id=94, len=8192, type=msg, action=PASS, \
redirect_type=none
'''

strparser:
'''
sockmap_strparser: sk=00000000f15fc1c8, netns=4026531840, inode=52396, \
family=2, protocol=6, prog_id=143, in_len=1000, full_len=10 '''

Quoted 5 lines

Thanks!