Add a new field sk_start_time to 'struct sock' to support different types of sockets (TCP/UDP/ICMP...) to record the pre-set start time value and then propagates it into skbs. Start time might vary across different protocols, so for TCP the patch picks the entry of tcp_sendmsg_locked as the start time because 1) it already holds the socket lock, 2) it nearly reflects when to trigger a sendmsg syscall from applications. The value for TCP is a real (wall-clock) time from ktime_get_real() so it stays comparable with the timestamps produced elsewhere in the stack and can be consumed directly by a BPF prog using newly added bpf_ktime_get_real_ns()). A later patch copies this value onto every skb entailed by the TCP write queue. Signed-off-by: Jason Xing --- include/net/sock.h | 2 ++ net/ipv4/tcp.c | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/include/net/sock.h b/include/net/sock.h index 51185222aac2..782ad5fc8ff5 100644 --- a/include/net/sock.h +++ b/include/net/sock.h @@ -314,6 +314,7 @@ struct sk_filter; * @mptcp_retransmit_timer: mptcp retransmit timer * @sk_stamp: time stamp of last packet received * @sk_stamp_seq: lock for accessing sk_stamp on 32 bit architectures only + * @sk_start_time: start time recorded for SK_BPF_CB_TIMESTAMPING_V2 * @sk_tsflags: SO_TIMESTAMPING flags * @sk_bpf_cb_flags: used in bpf_setsockopt() * @sk_use_task_frag: allow sk_page_frag() to use current->task_frag. @@ -552,6 +553,7 @@ struct sock { #if BITS_PER_LONG==32 seqlock_t sk_stamp_seq; #endif + ktime_t sk_start_time; int sk_disconnects; union { diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c index b4237d0e994d..28730a3e4473 100644 --- a/net/ipv4/tcp.c +++ b/net/ipv4/tcp.c @@ -1129,6 +1129,10 @@ int tcp_sendmsg_locked(struct sock *sk, struct msghdr *msg, size_t size) flags = msg->msg_flags; + if (static_branch_unlikely(&bpfts_v2_needed_key) && + SK_BPF_CB_FLAG_TEST(sk, SK_BPF_CB_TIMESTAMPING_V2)) + sk->sk_start_time = ktime_get_real(); + sockc = (struct sockcm_cookie){ .tsflags = READ_ONCE(sk->sk_tsflags) }; if (msg->msg_controllen) { sockc_err = sock_cmsg_send(sk, msg, &sockc); -- 2.43.7