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 1/6] bpf: Add bpf_ktime_get_real_ns() kfunc
Date: Mon, 18 May 2026 16:23:39 +0800 [thread overview]
Message-ID: <20260518082344.96647-2-kerneljasonxing@gmail.com> (raw)
In-Reply-To: <20260518082344.96647-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
next prev parent reply other threads:[~2026-05-18 8:24 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-18 8:23 [PATCH net-next 0/6] bpf-timetamp: support rx side Jason Xing
2026-05-18 8:23 ` Jason Xing [this message]
2026-05-18 11:57 ` [PATCH net-next 1/6] bpf: Add bpf_ktime_get_real_ns() kfunc Jesper Dangaard Brouer
2026-05-18 12:35 ` Jason Xing
2026-05-18 8:23 ` [PATCH net-next 2/6] net: export sock_disable_timestamp() declaration Jason Xing
2026-05-19 8:25 ` sashiko-bot
2026-05-19 11:50 ` Jason Xing
2026-05-18 8:23 ` [PATCH net-next 3/6] bpf: support bpf_setsockopt for bpf timestamping rx feature Jason Xing
2026-05-18 8:23 ` [PATCH net-next 4/6] bpf: add BPF_SOCK_OPS_TSTAMP_RCV_CB callback Jason Xing
2026-05-18 8:23 ` [PATCH net-next 5/6] bpf: enable bpf timestamping rx in TCP layer Jason Xing
2026-05-18 13:01 ` Jesper Dangaard Brouer
2026-05-18 13:53 ` Jason Xing
2026-05-18 16:40 ` Jesper Dangaard Brouer
2026-05-18 23:16 ` Jason Xing
2026-05-18 23:24 ` Jason Xing
2026-05-19 9:57 ` Toke Høiland-Jørgensen
2026-06-03 11:07 ` Toke Høiland-Jørgensen
2026-05-18 15:34 ` Stanislav Fomichev
2026-05-18 23:56 ` Jason Xing
2026-05-19 8:25 ` sashiko-bot
2026-05-19 10:31 ` Jiayuan Chen
2026-05-19 12:26 ` Jason Xing
2026-05-18 8:23 ` [PATCH net-next 6/6] selftests/bpf: Add RX latency tests for bpf timestamping Jason Xing
2026-05-19 8:25 ` sashiko-bot
2026-05-19 12:05 ` Jason Xing
2026-05-18 11:46 ` [PATCH net-next 0/6] bpf-timetamp: support rx side Jesper Dangaard Brouer
2026-05-18 12:32 ` 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=20260518082344.96647-2-kerneljasonxing@gmail.com \
--to=kerneljasonxing@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=davem@davemloft.net \
--cc=eddyz87@gmail.com \
--cc=edumazet@google.com \
--cc=horms@kernel.org \
--cc=john.fastabend@gmail.com \
--cc=jolsa@kernel.org \
--cc=kernelxing@tencent.com \
--cc=kuba@kernel.org \
--cc=kuniyu@google.com \
--cc=martin.lau@linux.dev \
--cc=memxor@gmail.com \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=sdf@fomichev.me \
--cc=song@kernel.org \
--cc=willemb@google.com \
--cc=yonghong.song@linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.