Subject: [PATCH net-next 5/6] bpf: enable bpf timestamping rx in TCP layer

From: Jason Xing <[email-masked]>
Date: Mon, 18 May 2026 16:23:43 +0800

From: Jason Xing <[email-masked]>

Add two if statements to accurately isolate bpf timestamping and so
timestamping. They can work respectively.

As to so_timestamping, only add a loose condition via report flags
to avoid duplicate strict checks that is done in tcp_recv_timestamp()
and performance impact. If the loose condition is hit,
tcp_recv_timestamp() is able to handle the exact case and doesn't
hamper the existing timestamping feature.

Make it work in TCP protocol.

Signed-off-by: Jason Xing <[email-masked]>
---
 net/ipv4/tcp.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c
index 21ece4c71612..64c69bb3578a 100644
--- a/net/ipv4/tcp.c
+++ b/net/ipv4/tcp.c
@@ -2949,8 +2949,18 @@ int tcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int flags)
 	release_sock(sk);
 
 	if ((cmsg_flags | msg->msg_get_inq) && ret >= 0) {
-		if (cmsg_flags & TCP_CMSG_TS)
-			tcp_recv_timestamp(msg, sk, &tss);
+		if (cmsg_flags & TCP_CMSG_TS) {
+			u32 tsflags = READ_ONCE(sk->sk_tsflags);
+
+			if (cgroup_bpf_enabled(CGROUP_SOCK_OPS) &&
+			    SK_BPF_CB_FLAG_TEST(sk, SK_BPF_CB_RX_TIMESTAMPING))
+				bpf_skops_rx_timestamping(sk, &tss,
+							  BPF_SOCK_OPS_TSTAMP_RCV_CB);
+			if (sock_flag(sk, SOCK_RCVTSTAMP) ||
+			    tsflags & SOF_TIMESTAMPING_SOFTWARE ||
+			    tsflags & SOF_TIMESTAMPING_RAW_HARDWARE)
+				tcp_recv_timestamp(msg, sk, &tss);
+		}
 		if ((cmsg_flags & TCP_CMSG_INQ) | msg->msg_get_inq) {
 			msg->msg_inq = tcp_inq_hint(sk);
 			if (cmsg_flags & TCP_CMSG_INQ)
-- 
2.43.7