public inbox for netdev@vger.kernel.org 
 help / color / mirror / Atom feed
From: Jason Xing <kerneljasonxing@gmail•com>
To: davem@davemloft•net, edumazet@google•com, kuba@kernel•org,
	pabeni@redhat•com, horms@kernel•org, willemb@google•com,
	kuniyu@google•com, ast@kernel•org, daniel@iogearbox•net,
	andrii@kernel•org, martin.lau@linux•dev, eddyz87@gmail•com,
	memxor@gmail•com, song@kernel•org, yonghong.song@linux•dev,
	jolsa@kernel•org, john.fastabend@gmail•com, sdf@fomichev•me
Cc: netdev@vger•kernel.org, bpf@vger•kernel.org,
	Jason Xing <kernelxing@tencent•com>
Subject: [PATCH net-next v2 1/6] bpf: Add bpf_ktime_get_real_ns() kfunc
Date: Thu, 21 May 2026 21:52:39 +0800	[thread overview]
Message-ID: <20260521135244.40869-2-kerneljasonxing@gmail.com> (raw)
In-Reply-To: <20260521135244.40869-1-kerneljasonxing@gmail.com>

From: Jason Xing <kernelxing@tencent•com>

Currently BPF programs can obtain timestamps via bpf_ktime_get_ns(),
which returns CLOCK_MONOTONIC time. However, the skb->tstamp field
populated by the network stack uses ktime_get_real() which is in the
CLOCK_REALTIME domain.

In the series, kernel reports the software/hardware timestamps through
sockopt and then userspace bpf application gets them and calculate the
delta only through an unified time unit.

However, prior to this, when a BPF program tries to measure RX packet
delay by comparing skb->tstamp with bpf_ktime_get_ns(), the result
is incorrect because the two clocks have different epochs.

Introduce a new BPF kfunc bpf_ktime_get_real_ns() that returns the
current CLOCK_REALTIME time. This allows BPF programs to perform
accurate delay calculations without clock domain mismatch issue.

Signed-off-by: Jason Xing <kernelxing@tencent•com>
---
 kernel/bpf/helpers.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
index 2bb60200c266..863645d096ef 100644
--- a/kernel/bpf/helpers.c
+++ b/kernel/bpf/helpers.c
@@ -2317,6 +2317,11 @@ void bpf_rb_root_free(const struct btf_field *field, void *rb_root,
 
 __bpf_kfunc_start_defs();
 
+__bpf_kfunc u64 bpf_ktime_get_real_ns(void)
+{
+	return ktime_get_real_fast_ns();
+}
+
 /**
  * bpf_obj_new() - allocate an object described by program BTF
  * @local_type_id__k: type ID in program BTF
@@ -4859,6 +4864,7 @@ BTF_ID_FLAGS(func, bpf_task_work_schedule_resume, KF_IMPLICIT_ARGS)
 BTF_ID_FLAGS(func, bpf_dynptr_from_file)
 BTF_ID_FLAGS(func, bpf_dynptr_file_discard)
 BTF_ID_FLAGS(func, bpf_timer_cancel_async)
+BTF_ID_FLAGS(func, bpf_ktime_get_real_ns)
 BTF_KFUNCS_END(common_btf_ids)
 
 static const struct btf_kfunc_id_set common_kfunc_set = {
-- 
2.43.7


  reply	other threads:[~2026-05-21 13:53 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-21 13:52 [PATCH net-next v2 0/6] bpf-timestamp: support rx side Jason Xing
2026-05-21 13:52 ` Jason Xing [this message]
2026-05-21 13:52 ` [PATCH net-next v2 2/6] net: export sock_disable_timestamp() declaration Jason Xing
2026-05-21 13:52 ` [PATCH net-next v2 3/6] bpf: support bpf_setsockopt for bpf timestamping rx feature Jason Xing
2026-05-23  4:02   ` Kuniyuki Iwashima
2026-05-21 13:52 ` [PATCH net-next v2 4/6] bpf: add BPF_SOCK_OPS_TSTAMP_RCV_CB callback Jason Xing
2026-05-21 13:52 ` [PATCH net-next v2 5/6] bpf: enable bpf timestamping rx in TCP layer Jason Xing
2026-05-23  3:45   ` Kuniyuki Iwashima
2026-05-28  0:50     ` Jason Xing
2026-05-21 13:52 ` [PATCH net-next v2 6/6] selftests/bpf: Add RX latency tests for bpf timestamping Jason Xing
2026-05-23  3:47 ` [PATCH net-next v2 0/6] bpf-timestamp: support rx side Kuniyuki Iwashima
2026-05-28  0:50   ` Jason Xing

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260521135244.40869-2-kerneljasonxing@gmail.com \
    --to=kerneljasonxing@gmail$(echo .)com \
    --cc=andrii@kernel$(echo .)org \
    --cc=ast@kernel$(echo .)org \
    --cc=bpf@vger$(echo .)kernel.org \
    --cc=daniel@iogearbox$(echo .)net \
    --cc=davem@davemloft$(echo .)net \
    --cc=eddyz87@gmail$(echo .)com \
    --cc=edumazet@google$(echo .)com \
    --cc=horms@kernel$(echo .)org \
    --cc=john.fastabend@gmail$(echo .)com \
    --cc=jolsa@kernel$(echo .)org \
    --cc=kernelxing@tencent$(echo .)com \
    --cc=kuba@kernel$(echo .)org \
    --cc=kuniyu@google$(echo .)com \
    --cc=martin.lau@linux$(echo .)dev \
    --cc=memxor@gmail$(echo .)com \
    --cc=netdev@vger$(echo .)kernel.org \
    --cc=pabeni@redhat$(echo .)com \
    --cc=sdf@fomichev$(echo .)me \
    --cc=song@kernel$(echo .)org \
    --cc=willemb@google$(echo .)com \
    --cc=yonghong.song@linux$(echo .)dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox