[PATCH net-next v2 1/4] tcp: separate BPF timestamping from tcp_tx_timestamp

Jason Xing
2026-04-04T15:05:06Z

From: Jason Xing <kernelxing@tencent.com>

Add a tcp_bpf_tx_timestamp() inline function as a prep for the later
patches.

Put it under the restriction of CONFIG_CGROUP_BPF.

Add a SKBTX_BPF check to avoid duplicated call of SENDMSG_CB if the
skb was tagged before. If we've already tagged this skb, only update
its tskey. It prevents the tskey from using an old value after more
data are all written into one skb, which is compatible with socket
timestamping.

Note: I didn't add back the process of reading skb from rtx queue which
was introduced by commit 838eb9687691 ("tcp: tcp_tx_timestamp() must look
at the rtx queue") because in BPF timestamping scenario:
1) BPF_SOCK_OPS_TSTAMP_SENDMSG_CB is the starting point for each skb
that needs record.
2) BPF script must correlate sendmsg and the skb in this phase first
and then will be able to record the SCHED/DRV/ACK timestamps to
construct the correct timeline.
Please see how progs/net_timestamping.c works with map.
3) at this point, notify the BPF script is too late to take care of
the timestamp in sendmsg in time.
In conclusion, that doesn't work for BPF timestamping.

Signed-off-by: Yushan Zhou <katrinzhou@tencent.com>
Signed-off-by: Jason Xing <kernelxing@tencent.com>
---
include/net/tcp.h | 20 ++++++++++++++++++++
net/ipv4/tcp.c | 5 +----
2 files changed, 21 insertions(+), 4 deletions(-)

diff --git a/include/net/tcp.h b/include/net/tcp.h
index 565943c34b7e..6705205ff236 100644
--- a/include/net/tcp.h
+++ b/include/net/tcp.h
@@ -2883,12 +2883,32 @@ static inline void bpf_skops_init_skb(struct bpf_sock_ops_kern *skops,
skops->skb = skb;
skops->skb_data_end = skb->data + end_offset;
}
+static inline void tcp_bpf_tx_timestamp(struct sock *sk)
+{
+ struct sk_buff *skb;
+
+ if (!cgroup_bpf_enabled(CGROUP_SOCK_OPS) ||
+ !SK_BPF_CB_FLAG_TEST(sk, SK_BPF_CB_TX_TIMESTAMPING))
+ return;
+
+ skb = tcp_write_queue_tail(sk);
+ if (!skb)
+ return;
+
+ if (!(skb_shinfo(skb)->tx_flags & SKBTX_BPF))
+ bpf_skops_tx_timestamping(sk, skb, BPF_SOCK_OPS_TSTAMP_SENDMSG_CB);
+ else
+ skb_shinfo(skb)->tskey = TCP_SKB_CB(skb)->seq + skb->len - 1;
+}
#else
static inline void bpf_skops_init_skb(struct bpf_sock_ops_kern *skops,
struct sk_buff *skb,
unsigned int end_offset)
{
}
+static inline void tcp_bpf_tx_timestamp(struct sock *sk)
+{
+}
#endif

/* Call BPF_SOCK_OPS program that returns an int. If the return value
diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c
index bd2c3c4587e1..169c3fff4f6d 100644
--- a/net/ipv4/tcp.c
+++ b/net/ipv4/tcp.c
@@ -500,10 +500,6 @@ static void tcp_tx_timestamp(struct sock *sk, struct sockcm_cookie *sockc)
if (tsflags & SOF_TIMESTAMPING_TX_RECORD_MASK)
shinfo->tskey = TCP_SKB_CB(skb)->seq + skb->len - 1;
}
-
- if (cgroup_bpf_enabled(CGROUP_SOCK_OPS) &&
- SK_BPF_CB_FLAG_TEST(sk, SK_BPF_CB_TX_TIMESTAMPING) && skb)
- bpf_skops_tx_timestamping(sk, skb, BPF_SOCK_OPS_TSTAMP_SENDMSG_CB);
}

/* @wake is one when sk_stream_write_space() calls us.
@@ -1417,6 +1413,7 @@ int tcp_sendmsg_locked(struct sock *sk, struct msghdr *msg, size_t size)
out:
if (copied) {
tcp_tx_timestamp(sk, &sockc);
+ tcp_bpf_tx_timestamp(sk);
tcp_push(sk, flags, mss_now, tp->nonagle, size_goal);
}
out_nopush:
--
2.41.3