bpf-timetamp: support rx side
Subsystems (9)
cover bpf-timetamp: support rx side
From: Jason Xing <[email protected]>
Previously the series[1] has already supported tx side for BPF timestamping, now it's time to support rx side.
[1]: https://git.kernel.org/pub/scm/linux/kernel/git/netdev/net-next.git/commit/?id=68b92ac494eb
Jason Xing (6):
bpf: Add bpf_ktime_get_real_ns() kfunc
net: export sock_disable_timestamp() declaration
bpf: support bpf_setsockopt for bpf timestamping rx feature
bpf: add BPF_SOCK_OPS_TSTAMP_RCV_CB callback
bpf: enable bpf timestamping rx in TCP layer selftests/bpf: Add RX latency tests for bpf timestamping
include/net/sock.h | 12 +++- include/uapi/linux/bpf.h | 10 ++- kernel/bpf/helpers.c | 6 ++ net/core/filter.c | 8 +++ net/core/sock.c | 20 +++++- net/ipv4/tcp.c | 14 +++- tools/include/uapi/linux/bpf.h | 5 ++ .../bpf/prog_tests/net_timestamping.c | 69 ++++++++++++++++++- .../selftests/bpf/progs/net_timestamping.c | 35 ++++++++++ 9 files changed, 172 insertions(+), 7 deletions(-)
Signature · 1 line
2.43.7
PATCH 1/6 bpf: Add bpf_ktime_get_real_ns() kfunc
From: Jason Xing <[email protected]>
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.
--- kernel/bpf/helpers.c | 6 ++++++ 1 file changed, 6 insertions(+)
kernel/bpf/helpers.c +6 -0
@@ -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 = {
Signature · 1 line
2.43.7
Re: [PATCH net-next 1/6] bpf: Add bpf_ktime_get_real_ns() kfunc
Quoted 11 lines · Jason Xing
From: Jason Xing <[email protected]>
Currently BPF programs can obtain timestamps via
bpf_ktime_get_ns(), which returnsCLOCK_MONOTONICtime. However, theskb->tstampfield populated by the network stack usesktime_get_real()which is in theCLOCK_REALTIMEdomain.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.
Quoted 8 lines
However, prior to this, when a BPF program tries to measure RX packet delay by comparing
skb->tstampwithbpf_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 currentCLOCK_REALTIMEtime. This allows BPF programs to perform accurate delay calculations without clock domain mismatch issue.
I support adding this helper, because have also hit this issue[1]. Our ugly workaround is to use the TAI clock and adjust for the current TAI offset.
[1] https://github.com/xdp-project/bpf-examples/tree/main/netstacklat
Cc Simon Sundberg <[email protected]>
Quoted 29 lines
Signed-off-by Jason Xing <[email protected]>--- kernel/bpf/helpers.c | 6 ++++++ 1 file changed, 6 insertions(+)kernel/bpf/helpers.c +6
-0@@ -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 = {
Re: [PATCH net-next 1/6] bpf: Add bpf_ktime_get_real_ns() kfunc
Quoted 17 lines · Jesper Dangaard Brouer
Quoted 15 lines · Jason Xing
From: Jason Xing <[email protected]>
Currently BPF programs can obtain timestamps via
bpf_ktime_get_ns(), which returnsCLOCK_MONOTONICtime. However, theskb->tstampfield populated by the network stack usesktime_get_real()which is in theCLOCK_REALTIMEdomain.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->tstampwithbpf_ktime_get_ns(), the result is incorrect because the two clocks have different epochs.
Quoted 8 lines
Quoted 4 lines
Introduce a new BPF kfunc
bpf_ktime_get_real_ns()that returns the currentCLOCK_REALTIMEtime. This allows BPF programs to perform accurate delay calculations without clock domain mismatch issue.I support adding this helper, because have also hit this issue[1]. Our ugly workaround is to use the TAI clock and adjust for the current TAI offset.
Right. Thanks :)
Quoted 2 lines
[1] https://github.com/xdp-project/bpf-examples/tree/main/netstacklat
The tool is useful as long as we enable net_enable_timetamp(). Yes, bpf/so timestamping is one of methods to do so.
Thanks,
Jason
Quoted 33 lines
Cc Simon Sundberg <[email protected]>
Quoted 29 lines
Signed-off-by Jason Xing <[email protected]>--- kernel/bpf/helpers.c | 6 ++++++ 1 file changed, 6 insertions(+)kernel/bpf/helpers.c +6
-0@@ -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 = {
PATCH 2/6 net: export sock_disable_timestamp() declaration
In reply to Jason XingFrom: Jason Xing <[email protected]>
In the series, sock_disable_timestamp() will be shortly used by bpf timestamping rx feature to dynamically turn off the global time record function. So remove the static label.
--- include/net/sock.h | 1 + net/core/sock.c | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-)
include/net/sock.h +1 -0
@@ -3047,6 +3047,7 @@ static inline bool sk_listener_or_tw(const struct sock *sk) }void sock_enable_timestamp (struct sock *sk,enum sock_flags flag); +void sock_disable_timestamp (struct sock *sk,unsigned long flags);int sock_recv_errqueue (struct sock *sk,struct msghdr *msg,int len,int level,int type);
net/core/sock.c +1 -1
@@ -474,7 +474,7 @@ static bool sock_needs_netstamp(const struct sock *sk) } } -static void sock_disable_timestamp (struct sock *sk,unsigned long flags) +void sock_disable_timestamp (struct sock *sk,unsigned long flags) {if (sk->sk_flags & flags) { sk->sk_flags &= ~flags;
Signature · 1 line
2.43.7
[email protected]
AI reviewRe: [PATCH net-next 2/6] net: export sock_disable_timestamp() declaration
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] Calling `bpf_setsockopt` to enable or disable `SK_BPF_CB_RX_TIMESTAMPING` from BPF hooks executing…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Calling bpf_setsockopt to enable or disable SK_BPF_CB_RX_TIMESTAMPING from BPF hooks executing in softirq will trigger a kernel panic (sleep in atomic context) if CONFIG_JUMP_LABEL is not enabled.
- [Low] API asymmetry between sock_enable_timestamp and sock_disable_timestamp creates a dangerous bitmask trap.
--
commit ae551b79a5c9f8c77421dc360627b1e40e3ceaec
Author: Jason Xing <[email protected]>
Subject: net: export sock_disable_timestamp() declaration
This patch removes the static modifier from sock_disable_timestamp() and adds its declaration to include/net/sock.h. This change exposes the function to the rest of the kernel, specifically allowing the BPF subsystem to dynamically turn off the global time record function.
include/net/sock.h static inline bool sk_listener_or_tw(const struct sock *sk) · line 3041
Quoted 9 lines
include/net/sock.h +1
-0@@ -3038,6 +3038,7 @@ static inline bool sk_listener_or_tw(const struct sock *sk) }void sock_enable_timestamp (struct sock *sk,enum sock_flags flag); +void sock_disable_timestamp (struct sock *sk,unsigned long flags);
This isn't a bug, but does exporting these two functions with asymmetrical signatures create a bitmask trap?
The newly exported sock_disable_timestamp() expects a bitmask (unsigned long flags), while sock_enable_timestamp() expects an enum index (enum sock_flags flag).
If a future developer mistakenly passes an enum index (like SOCK_TIMESTAMP which has value 7) to sock_disable_timestamp(), wouldn't it silently clear multiple unrelated socket flags (bits 0, 1, and 2) instead of the intended flag?
Quoted 2 lines
include/net/sock.h +0
-0int sock_recv_errqueue (struct sock *sk,struct msghdr *msg,int len,int level,int type);
[ ... ]
net/core/sock.c static bool sock_needs_netstamp(const struct sock *sk) · line 480
Quoted 13 lines
net/core/sock.c +1
-1@@ -474,7 +474,7 @@ static bool sock_needs_netstamp(const struct sock *sk) } } -static void sock_disable_timestamp (struct sock *sk,unsigned long flags) +void sock_disable_timestamp (struct sock *sk,unsigned long flags) {if (sk->sk_flags & flags) { sk->sk_flags &= ~flags;
If BPF programs running in an atomic softirq context call bpf_setsockopt() to disable SK_BPF_CB_RX_TIMESTAMPING, could this trigger a panic?
Looking at net_disable_timestamp() which is called immediately after this bitmask update, if CONFIG_JUMP_LABEL is not enabled, the code falls back to calling static_branch_dec() on netstamp_needed_key.
This expands to static_key_slow_dec(), which unconditionally acquires jump_label_mutex and cpus_read_lock().
Since acquiring a sleeping mutex inside a softirq context triggers a sleep-in-atomic panic, will this cause a deterministic crash when users dynamically attach these BPF programs?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Re: [PATCH net-next 2/6] net: export sock_disable_timestamp() declaration
Quoted 28 lines · On Tue, May 19, 2026 at 4:25 PM <[email protected]> wrote:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Callingbpf_setsockoptto enable or disableSK_BPF_CB_RX_TIMESTAMPINGfrom BPF hooks executing in softirq will trigger a kernel panic (sleep in atomic context) ifCONFIG_JUMP_LABELis not enabled.
- [Low] API asymmetry betweensock_enable_timestampandsock_disable_timestampcreates a dangerous bitmask trap.
--commit
ae551b79a5c9f8c77421dc360627b1e40e3ceaec
Author: Jason Xing <[email protected]>
Subject: net: exportsock_disable_timestamp()declarationThis patch removes the static modifier from
sock_disable_timestamp()and adds its declaration toinclude/net/sock.h. This change exposes the function to the rest of the kernel, specifically allowing the BPF subsystem to dynamically turn off the global time record function.include/net/sock.h static inline bool sk_listener_or_tw(const struct sock *sk) · line 3041
Quoted 9 lines
include/net/sock.h +1
-0@@ -3038,6 +3038,7 @@ static inline bool sk_listener_or_tw(const struct sock *sk) }void sock_enable_timestamp (struct sock *sk,enum sock_flags flag); +void sock_disable_timestamp (struct sock *sk,unsigned long flags);This isn't a bug, but does exporting these two functions with asymmetrical signatures create a bitmask trap?
Quoted 8 lines
The newly exported
sock_disable_timestamp()expects a bitmask (unsigned long flags), whilesock_enable_timestamp()expects an enum index (enumsock_flagsflag).If a future developer mistakenly passes an enum index (like
SOCK_TIMESTAMPwhich has value 7) tosock_disable_timestamp(), wouldn't it silently clear multiple unrelated socket flags (bits 0, 1, and 2) instead of the intended flag?
You said what if someone makes a mistake... At least so far we're able to handle this.
The existing impl was deliberately designed. See how __sk_destruct() works with the function.
Quoted 23 lines
Quoted 3 lines
include/net/sock.h +0
-0int sock_recv_errqueue (struct sock *sk,struct msghdr *msg,int len,int level,int type);[ ... ]
net/core/sock.c static bool sock_needs_netstamp(const struct sock *sk) · line 480
Quoted 13 lines
net/core/sock.c +1
-1@@ -474,7 +474,7 @@ static bool sock_needs_netstamp(const struct sock *sk) } } -static void sock_disable_timestamp (struct sock *sk,unsigned long flags) +void sock_disable_timestamp (struct sock *sk,unsigned long flags) {if (sk->sk_flags & flags) { sk->sk_flags &= ~flags;If BPF programs running in an atomic softirq context call
bpf_setsockopt()to disableSK_BPF_CB_RX_TIMESTAMPING, could this trigger a panic?
Quoted 6 lines
Looking at
net_disable_timestamp()which is called immediately after this bitmask update, ifCONFIG_JUMP_LABELis not enabled, the code falls back to callingstatic_branch_dec()onnetstamp_needed_key.This expands to
static_key_slow_dec(), which unconditionally acquiresjump_label_mutexandcpus_read_lock().
No. When !CONFIG_JUMP_LABEL is met, the following code is the right place that you need to look into:
static inline void static_key_slow_dec(struct static_key *key)
{
STATIC_KEY_CHECK_USE(key);
atomic_dec(&key->enabled);
}So in this circumstance, no sleep at all. You were analyzing another one :(
Quoted 7 lines
Since acquiring a sleeping mutex inside a softirq context triggers a sleep-in-atomic panic, will this cause a deterministic crash when users dynamically attach these BPF programs?
--
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
PATCH 3/6 bpf: support bpf_setsockopt for bpf timestamping rx feature
In reply to Jason XingFrom: Jason Xing <[email protected]>
Add SK_BPF_CB_RX_TIMESTAMPING callback flag to enable RX timestamping via bpf_setsockopt(SK_BPF_CB_FLAGS).
Add SOCK_BPF_TIMESTAMPING_RX into enum sock_flags that is used as centralized management of net_enable/disable_timestamp. Note that only one of them (timestamp, so_timestamping and bpf timestamping rx) can enable and disable the global time record of skbs.
In addition, include SOCK_BPF_TIMESTAMPING_RX in SK_FLAGS_TIMESTAMP so that __sk_destruct() calling sock_disable_timestamp() to thoroughly turn off the global time record.
--- include/net/sock.h | 5 ++++- include/uapi/linux/bpf.h | 5 +++-- net/core/filter.c | 8 ++++++++ 3 files changed, 15 insertions(+), 3 deletions(-)
include/net/sock.h +4 -1
@@ -1022,9 +1022,12 @@ enum sock_flags {SOCK_RCVMARK ,/* Receive SO_MARK ancillary data with packet */ SOCK_RCVPRIORITY ,/* Receive SO_PRIORITY ancillary data with packet */ SOCK_TIMESTAMPING_ANY ,/* Copy of sk_tsflags & TSFLAGS_ANY */ + SOCK_BPF_TIMESTAMPING_RX, /* BPF RX timestamping enabled */ }; -#define SK_FLAGS_TIMESTAMP ((1UL << SOCK_TIMESTAMP) | (1UL << SOCK_TIMESTAMPING_RX_SOFTWARE)) +#define SK_FLAGS_TIMESTAMP ((1UL << SOCK_TIMESTAMP) | \ + (1UL << SOCK_TIMESTAMPING_RX_SOFTWARE) | \ + (1UL << SOCK_BPF_TIMESTAMPING_RX))/* * The highest bit of sk_tsflags is reserved for kernel-internal * SOCKCM_FLAG_TS_OPT_ID. There is a check in core/sock.c to control that
include/uapi/linux/bpf.h +3 -2
@@ -7029,8 +7029,9 @@ enum {enum {SK_BPF_CB_TX_TIMESTAMPING =1 <<0 , -SK_BPF_CB_MASK = (SK_BPF_CB_TX_TIMESTAMPING -1 ) | -SK_BPF_CB_TX_TIMESTAMPING +SK_BPF_CB_RX_TIMESTAMPING =1 <<1 , +SK_BPF_CB_MASK = (SK_BPF_CB_RX_TIMESTAMPING -1 ) | +SK_BPF_CB_RX_TIMESTAMPING };/* List of known BPF sock_ops operators.
net/core/filter.c +8 -0
@@ -5304,6 +5304,14 @@ static int sk_bpf_set_get_cb_flags(struct sock *sk, char *optval, bool getopt)if (sk_bpf_cb_flags & ~SK_BPF_CB_MASK )return -EINVAL ; +if ((sk_bpf_cb_flags ^ sk->sk_bpf_cb_flags) &SK_BPF_CB_RX_TIMESTAMPING ) { +if (sk_bpf_cb_flags &SK_BPF_CB_RX_TIMESTAMPING ) +sock_enable_timestamp (sk,SOCK_BPF_TIMESTAMPING_RX ); +else +sock_disable_timestamp (sk, + (1UL <<SOCK_BPF_TIMESTAMPING_RX )); + } + sk->sk_bpf_cb_flags = sk_bpf_cb_flags;return 0 ;
Signature · 1 line
2.43.7
PATCH 4/6 bpf: add BPF_SOCK_OPS_TSTAMP_RCV_CB callback
In reply to Jason XingFrom: Jason Xing <[email protected]>
This is the prep patch adding BPF_SOCK_OPS_TSTAMP_RCV_CB cb and the rx tunnel to allow kernel to report timestamps.
It's possible to have both software and hardware timestamps in the last skb from this recv syscall, so the tunnel bpf_skops_rx_timestamping() supports four slots to record and report.
--- include/net/sock.h | 6 ++++++ include/uapi/linux/bpf.h | 5 +++++ net/core/sock.c | 18 ++++++++++++++++++ tools/include/uapi/linux/bpf.h | 5 +++++ 4 files changed, 34 insertions(+)
include/net/sock.h +6 -0
@@ -3138,10 +3138,16 @@ int sock_set_timestamping(struct sock *sk, int optname,#if defined(CONFIG_CGROUP_BPF )void bpf_skops_tx_timestamping (struct sock *sk,struct sk_buff *skb,int op); +void bpf_skops_rx_timestamping (struct sock *sk, +struct scm_timestamping_internal *tss,int op);#else static inline void bpf_skops_tx_timestamping (struct sock *sk,struct sk_buff *skb,int op) { } +static inline void bpf_skops_rx_timestamping (struct sock *sk, +struct scm_timestamping_internal *tss,int op) +{ +}#endif void sock_no_linger (struct sock *sk);void sock_set_keepalive (struct sock *sk);
include/uapi/linux/bpf.h +5 -0
@@ -7169,6 +7169,11 @@ enum {* sendmsg timestamp with corresponding * tskey. */ + BPF_SOCK_OPS_TSTAMP_RCV_CB, /* Called in tcp_recvmsg() to record +* sw/hw timestamp of the last skb +* after receiving all the data when +* SK_BPF_CB_RX_TIMESTAMPING is on. +*/ };/* List of TCP states. There is a build check in net/ipv4/tcp.c to detect
net/core/sock.c +18 -0
@@ -952,6 +952,24 @@ void bpf_skops_tx_timestamping(struct sock *sk, struct sk_buff *skb, int op)bpf_skops_init_skb (&sock_ops, skb,0 );__cgroup_bpf_run_filter_sock_ops (sk, &sock_ops,CGROUP_SOCK_OPS ); } + +void bpf_skops_rx_timestamping (struct sock *sk, +struct scm_timestamping_internal *tss,int op) +{ +struct bpf_sock_ops_kern sock_ops; +u64 sw_tstamp =ktime_to_ns (tss->ts[0 ]); +u64 hw_tstamp =ktime_to_ns (tss->ts[2 ]); + +memset (&sock_ops,0 , offsetof(struct bpf_sock_ops_kern , temp)); + sock_ops.op = op; + sock_ops.is_fullsock =1 ; + sock_ops.sk = sk; + sock_ops.args[0 ] = (u32 )sw_tstamp; + sock_ops.args[1 ] = (u32)(sw_tstamp >>32 ); + sock_ops.args[2 ] = (u32 )hw_tstamp; + sock_ops.args[3 ] = (u32)(hw_tstamp >>32 ); +__cgroup_bpf_run_filter_sock_ops (sk, &sock_ops,CGROUP_SOCK_OPS ); +}#endif void sock_set_keepalive (struct sock *sk)
tools/include/uapi/linux/bpf.h +5 -0
@@ -7168,6 +7168,11 @@ enum {* sendmsg timestamp with corresponding * tskey. */ + BPF_SOCK_OPS_TSTAMP_RCV_CB, /* Called in tcp_recvmsg() to record +* sw/hw timestamp of the last skb +* after receiving all the data when +* SK_BPF_CB_RX_TIMESTAMPING is on. +*/ };/* List of TCP states. There is a build check in net/ipv4/tcp.c to detect
Signature · 1 line
2.43.7
PATCH 5/6 bpf: enable bpf timestamping rx in TCP layer
In reply to Jason XingFrom: Jason Xing <[email protected]>
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.
--- net/ipv4/tcp.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-)
net/ipv4/tcp.c +12 -2
@@ -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 )
Signature · 1 line
2.43.7
Re: [PATCH net-next 5/6] bpf: enable bpf timestamping rx in TCP layer
Quoted 25 lines · Jason Xing
From: Jason Xing <[email protected]>
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 intcp_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 protected]>--- net/ipv4/tcp.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-)net/ipv4/tcp.c +0
-0@@ -2949,8 +2949,18 @@ int tcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int flags)release_sock (sk);
net/ipv4/tcp.c int tcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int flags) · line 2958
Quoted 10 lines
net/ipv4/tcp.c +7
-2if ((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 );
Does this mean I can enable timestamp reading per cgroup?
In Simon's netstacklat[1] tool we are forced process all RX timestamp (hooking fentry/tcp_recv_timestamp), and then we have a BPF filter[2] on the cgroup IDs that we are interested in (which is a significant overhead, as this is deployed at Cloudflare production scale).
[1] https://github.com/xdp-project/bpf-examples/tree/main/netstacklat
[2] https://github.com/xdp-project/bpf-examples/blob/main/netstacklat/netstacklat.bpf.c#L484-L488
Quoted 8 lines
net/ipv4/tcp.c +5
-0+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 )
Re: [PATCH net-next 5/6] bpf: enable bpf timestamping rx in TCP layer
Quoted 29 lines · Jesper Dangaard Brouer
Quoted 25 lines · Jason Xing
From: Jason Xing <[email protected]>
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 intcp_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 protected]>--- net/ipv4/tcp.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-)net/ipv4/tcp.c +0
-0@@ -2949,8 +2949,18 @@ int tcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int flags)release_sock (sk);
Quoted 12 lines
net/ipv4/tcp.c int tcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int flags) · line 2958
Quoted 10 lines
net/ipv4/tcp.c +7
-2if ((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 );Does this mean I can enable timestamp reading per cgroup?
Yes, I think so, but I didn't try. One of the natures of sockopt feature is supporting cgroup attach. cgroup_bpf_prog_attach()/cgroup_bpf_link_attach() is probably something that you're looking for.
IIUC, you can attach the prog onto the cgroup where all the sockets are set using the bpf timestamping function. So the current impl is cleaner and has better isolation (to filter out those unmatched flows).
Quoted 5 lines
In Simon's netstacklat[1] tool we are forced process all RX timestamp (hooking fentry/tcp_recv_timestamp), and then we have a BPF filter[2] on the cgroup IDs that we are interested in (which is a significant overhead, as this is deployed at Cloudflare production scale).
I can feel the pain when filtering in this kind of relatively hot path, which is what I'm trying to avoid internally. What I've done in production (to cover those old kernels) is to just let the kernel print the information, that's it, and there is an agent continuously gathering the data, doing the match and computing latency. But it's overall complicated.
Many thanks here, I'm always interested in hearing more useful and real requirements and fancy ideas on how to monitor the latency :) Now I'm still struggling to port the internal functions to bpf timestamping.
Thanks,
Jason
Quoted 17 lines
[1] https://github.com/xdp-project/bpf-examples/tree/main/netstacklat
[2]
https://github.com/xdp-project/bpf-examples/blob/main/netstacklat/netstacklat.bpf.c#L484-L488Quoted 8 lines
net/ipv4/tcp.c +5
-0+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 )
Re: [PATCH net-next 5/6] bpf: enable bpf timestamping rx in TCP layer
Quoted 43 lines · Jason Xing
Quoted 29 lines · Jesper Dangaard Brouer
Quoted 25 lines · Jason Xing
From: Jason Xing <[email protected]>
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 intcp_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 protected]>--- net/ipv4/tcp.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-)net/ipv4/tcp.c +0
-0@@ -2949,8 +2949,18 @@ int tcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int flags)release_sock (sk);Quoted 12 lines
net/ipv4/tcp.c int tcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int flags) · line 2958
Quoted 10 lines
net/ipv4/tcp.c +7
-2if ((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 );Does this mean I can enable timestamp reading per cgroup?
Quoted 5 lines
Yes, I think so, but I didn't try. One of the natures of sockopt feature is supporting cgroup attach. cgroup_bpf_prog_attach()/cgroup_bpf_link_attach() is probably something that you're looking for.
Sound good
Quoted 11 lines
IIUC, you can attach the prog onto the cgroup where all the sockets are set using the bpf timestamping function. So the current impl is cleaner and has better isolation (to filter out those unmatched flows).
Quoted 5 lines
In Simon's netstacklat[1] tool we are forced process all RX timestamp (hooking fentry/tcp_recv_timestamp), and then we have a BPF filter[2] on the cgroup IDs that we are interested in (which is a significant overhead, as this is deployed at Cloudflare production scale).
Quoted 7 lines
I can feel the pain when filtering in this kind of relatively hot path, which is what I'm trying to avoid internally. What I've done in production (to cover those old kernels) is to just let the kernel print the information, that's it, and there is an agent continuously gathering the data, doing the match and computing latency. But it's overall complicated.
I hope you don't mean your internal/old approach was using printk and then analyzing this data.
Quoted 2 lines
Many thanks here, I'm always interested in hearing more useful and real requirements and fancy ideas on how to monitor the latency :) Now
Simon Sundberg <[email protected]> have many more fancy ideas on how to monitor the latency. The netstacklat tool is part of Simon's PhD thesis:
- https://doi.org/10.59217/qklv6836
And we even gotten a paper accepted on netstacklat:
- https://kau.diva-portal.org/smash/record.jsf?pid=diva2%3A2034009&dswid=3032
Quoted 3 lines
I'm still struggling to port the internal functions to bpf timestamping.
Good luck, feel free to get inspired by our netstacklat tool. Key point is to let BPF store latency histograms and then let userspace periodically consume these - as heatmaps.
We are proposing to add 'netstacklat' as a new libbpf-tools utility
- https://github.com/iovisor/bcc/issues/5510
Quoted 17 lines
Quoted 17 lines
[1] https://github.com/xdp-project/bpf-examples/tree/main/netstacklat
[2]
https://github.com/xdp-project/bpf-examples/blob/main/netstacklat/netstacklat.bpf.c#L484-L488Quoted 8 lines
net/ipv4/tcp.c +5
-0+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 )
Re: [PATCH net-next 5/6] bpf: enable bpf timestamping rx in TCP layer
Quoted 74 lines · Jesper Dangaard Brouer
Quoted 43 lines · Jason Xing
Quoted 29 lines · Jesper Dangaard Brouer
Quoted 25 lines · Jason Xing
From: Jason Xing <[email protected]>
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 intcp_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 protected]>--- net/ipv4/tcp.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-)net/ipv4/tcp.c +0
-0@@ -2949,8 +2949,18 @@ int tcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int flags)release_sock (sk);Quoted 12 lines
net/ipv4/tcp.c int tcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int flags) · line 2958
Quoted 10 lines
net/ipv4/tcp.c +7
-2if ((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 );Does this mean I can enable timestamp reading per cgroup?
Quoted 5 lines
Yes, I think so, but I didn't try. One of the natures of sockopt feature is supporting cgroup attach. cgroup_bpf_prog_attach()/cgroup_bpf_link_attach() is probably something that you're looking for.
Sound good
Quoted 11 lines
IIUC, you can attach the prog onto the cgroup where all the sockets are set using the bpf timestamping function. So the current impl is cleaner and has better isolation (to filter out those unmatched flows).
Quoted 5 lines
In Simon's netstacklat[1] tool we are forced process all RX timestamp (hooking fentry/tcp_recv_timestamp), and then we have a BPF filter[2] on the cgroup IDs that we are interested in (which is a significant overhead, as this is deployed at Cloudflare production scale).
Quoted 7 lines
I can feel the pain when filtering in this kind of relatively hot path, which is what I'm trying to avoid internally. What I've done in production (to cover those old kernels) is to just let the kernel print the information, that's it, and there is an agent continuously gathering the data, doing the match and computing latency. But it's overall complicated.
Quoted 2 lines
I hope you don't mean your internal/old approach was using printk and then analyzing this data.
Of course not :)
The internal approach is to cover the old kernels but doesn't mean the approach is old :P
Instead, the internal kernel module is super efficient and I'm trying to ship bpf with such an ability. The fact is we've already deployed in production: 7x24 running, zero sampling.
Please see page 24 where there is a brief introduction on how to deal with the log part:
https://lpc.events/event/19/contributions/2055/#preview:3846
I believe this is the promising direction (ring buffer + lightweight kernel + heavy agent) we're taking.
The headache part is that I need to provide an agent written in BPF to do the heavy process.
Quoted 4 lines
Quoted 3 lines
Many thanks here, I'm always interested in hearing more useful and real requirements and fancy ideas on how to monitor the latency :) Now
Quoted 8 lines
Simon Sundberg <[email protected]> have many more fancy ideas on how to monitor the latency.
The netstacklat tool is part of Simon's PhD thesis:
- https://doi.org/10.59217/qklv6836And we even gotten a paper accepted on netstacklat:
-
https://kau.diva-portal.org/smash/record.jsf?pid=diva2%3A2034009&dswid=3032
Sorry, I cannot access this link. Could you give me the title of this paper?
Quoted 9 lines
Quoted 5 lines
I'm still struggling to port the internal functions to bpf timestamping.
Good luck, feel free to get inspired by our netstacklat tool. Key point is to let BPF store latency histograms and then let userspace periodically consume these - as heatmaps.
Thanks. I'll dig deeper into it.
Quoted 3 lines
We are proposing to add 'netstacklat' as a new libbpf-tools utility
- https://github.com/iovisor/bcc/issues/5510
Great!
Thanks,
Jason
Quoted 20 lines
Quoted 19 lines
Quoted 19 lines
[1] https://github.com/xdp-project/bpf-examples/tree/main/netstacklat
[2]
https://github.com/xdp-project/bpf-examples/blob/main/netstacklat/netstacklat.bpf.c#L484-L488Quoted 8 lines
net/ipv4/tcp.c +5
-0+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 )
Re: [PATCH net-next 5/6] bpf: enable bpf timestamping rx in TCP layer
Quoted 106 lines · Jason Xing
Quoted 74 lines · Jesper Dangaard Brouer
Quoted 43 lines · Jason Xing
Quoted 29 lines · Jesper Dangaard Brouer
Quoted 25 lines · Jason Xing
From: Jason Xing <[email protected]>
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 intcp_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 protected]>--- net/ipv4/tcp.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-)net/ipv4/tcp.c +0
-0@@ -2949,8 +2949,18 @@ int tcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int flags)release_sock (sk);Quoted 12 lines
net/ipv4/tcp.c int tcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int flags) · line 2958
Quoted 10 lines
net/ipv4/tcp.c +7
-2if ((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 );Does this mean I can enable timestamp reading per cgroup?
Quoted 5 lines
Yes, I think so, but I didn't try. One of the natures of sockopt feature is supporting cgroup attach. cgroup_bpf_prog_attach()/cgroup_bpf_link_attach() is probably something that you're looking for.
Sound good
Quoted 11 lines
IIUC, you can attach the prog onto the cgroup where all the sockets are set using the bpf timestamping function. So the current impl is cleaner and has better isolation (to filter out those unmatched flows).
Quoted 5 lines
In Simon's netstacklat[1] tool we are forced process all RX timestamp (hooking fentry/tcp_recv_timestamp), and then we have a BPF filter[2] on the cgroup IDs that we are interested in (which is a significant overhead, as this is deployed at Cloudflare production scale).
Quoted 7 lines
I can feel the pain when filtering in this kind of relatively hot path, which is what I'm trying to avoid internally. What I've done in production (to cover those old kernels) is to just let the kernel print the information, that's it, and there is an agent continuously gathering the data, doing the match and computing latency. But it's overall complicated.
Quoted 2 lines
I hope you don't mean your internal/old approach was using printk and then analyzing this data.
Of course not :)
The internal approach is to cover the old kernels but doesn't mean the approach is old :P
Instead, the internal kernel module is super efficient and I'm trying to ship bpf with such an ability. The fact is we've already deployed in production: 7x24 running, zero sampling.
Please see page 24 where there is a brief introduction on how to deal with the log part:
https://lpc.events/event/19/contributions/2055/#preview:3846
I believe this is the promising direction (ring buffer + lightweight kernel + heavy agent) we're taking.The headache part is that I need to provide an agent written in BPF to do the heavy process.
Quoted 9 lines
Quoted 3 lines
Many thanks here, I'm always interested in hearing more useful and real requirements and fancy ideas on how to monitor the latency :) Now
Simon Sundberg <[email protected]> have many more fancy ideas on how to monitor the latency.
The netstacklat tool is part of Simon's PhD thesis:
- https://doi.org/10.59217/qklv6836
Quoted 5 lines
Quoted 3 lines
And we even gotten a paper accepted on netstacklat:
-
https://kau.diva-portal.org/smash/record.jsf?pid=diva2%3A2034009&dswid=3032Sorry, I cannot access this link. Could you give me the title of this paper?
Waiting at the Front Door - Continuous Monitoring of Latency in the Host Network Stack
Oh, I guess it hasn't been officially published right? This is the reason why I have no way to know the content.
Thanks,
Jason
Quoted 42 lines
Quoted 10 lines
Quoted 6 lines
I'm still struggling to port the internal functions to bpf timestamping.
Good luck, feel free to get inspired by our netstacklat tool. Key point is to let BPF store latency histograms and then let userspace periodically consume these - as heatmaps.
Thanks. I'll dig deeper into it.
Quoted 3 lines
We are proposing to add 'netstacklat' as a new libbpf-tools utility
- https://github.com/iovisor/bcc/issues/5510Great!
Thanks,
JasonQuoted 20 lines
Quoted 19 lines
Quoted 19 lines
[1] https://github.com/xdp-project/bpf-examples/tree/main/netstacklat
[2]
https://github.com/xdp-project/bpf-examples/blob/main/netstacklat/netstacklat.bpf.c#L484-L488Quoted 8 lines
net/ipv4/tcp.c +5
-0+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 )
Re: [PATCH net-next 5/6] bpf: enable bpf timestamping rx in TCP layer
Quoted 111 lines · Jason Xing
Quoted 110 lines · Jason Xing
Quoted 74 lines · Jesper Dangaard Brouer
Quoted 43 lines · Jason Xing
Quoted 29 lines · Jesper Dangaard Brouer
Quoted 25 lines · Jason Xing
From: Jason Xing <[email protected]>
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 intcp_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 protected]>--- net/ipv4/tcp.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-)net/ipv4/tcp.c +0
-0@@ -2949,8 +2949,18 @@ int tcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int flags)release_sock (sk);Quoted 12 lines
net/ipv4/tcp.c int tcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int flags) · line 2958
Quoted 10 lines
net/ipv4/tcp.c +7
-2if ((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 );Does this mean I can enable timestamp reading per cgroup?
Quoted 5 lines
Yes, I think so, but I didn't try. One of the natures of sockopt feature is supporting cgroup attach. cgroup_bpf_prog_attach()/cgroup_bpf_link_attach() is probably something that you're looking for.
Sound good
Quoted 11 lines
IIUC, you can attach the prog onto the cgroup where all the sockets are set using the bpf timestamping function. So the current impl is cleaner and has better isolation (to filter out those unmatched flows).
Quoted 5 lines
In Simon's netstacklat[1] tool we are forced process all RX timestamp (hooking fentry/tcp_recv_timestamp), and then we have a BPF filter[2] on the cgroup IDs that we are interested in (which is a significant overhead, as this is deployed at Cloudflare production scale).
Quoted 7 lines
I can feel the pain when filtering in this kind of relatively hot path, which is what I'm trying to avoid internally. What I've done in production (to cover those old kernels) is to just let the kernel print the information, that's it, and there is an agent continuously gathering the data, doing the match and computing latency. But it's overall complicated.
Quoted 2 lines
I hope you don't mean your internal/old approach was using printk and then analyzing this data.
Of course not :)
The internal approach is to cover the old kernels but doesn't mean the approach is old :P
Instead, the internal kernel module is super efficient and I'm trying to ship bpf with such an ability. The fact is we've already deployed in production: 7x24 running, zero sampling.
Please see page 24 where there is a brief introduction on how to deal with the log part:
https://lpc.events/event/19/contributions/2055/#preview:3846
I believe this is the promising direction (ring buffer + lightweight kernel + heavy agent) we're taking.The headache part is that I need to provide an agent written in BPF to do the heavy process.
Quoted 12 lines
Quoted 3 lines
Many thanks here, I'm always interested in hearing more useful and real requirements and fancy ideas on how to monitor the latency :) Now
Simon Sundberg <[email protected]> have many more fancy ideas on how to monitor the latency.
The netstacklat tool is part of Simon's PhD thesis:
- https://doi.org/10.59217/qklv6836And we even gotten a paper accepted on netstacklat:
-
https://kau.diva-portal.org/smash/record.jsf?pid=diva2%3A2034009&dswid=3032
Quoted 7 lines
Quoted 1 line
Sorry, I cannot access this link. Could you give me the title of this paper?
Waiting at the Front Door - Continuous Monitoring of Latency in the Host Network Stack
Oh, I guess it hasn't been officially published right? This is the reason why I have no way to know the content.
No, it's not published yet; I'll send you a copy off-list :)
-Toke
Re: [PATCH net-next 5/6] bpf: enable bpf timestamping rx in TCP layer
Quoted 115 lines · Toke Høiland-Jørgensen
Quoted 113 lines · Jason Xing
Quoted 111 lines · Jason Xing
Quoted 74 lines · Jesper Dangaard Brouer
Quoted 43 lines · Jason Xing
Quoted 29 lines · Jesper Dangaard Brouer
Quoted 25 lines · Jason Xing
From: Jason Xing <[email protected]>
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 intcp_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 protected]>--- net/ipv4/tcp.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-)net/ipv4/tcp.c +0
-0@@ -2949,8 +2949,18 @@ int tcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int flags)release_sock (sk);Quoted 12 lines
net/ipv4/tcp.c int tcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int flags) · line 2958
Quoted 10 lines
net/ipv4/tcp.c +7
-2if ((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 );Does this mean I can enable timestamp reading per cgroup?
Quoted 5 lines
Yes, I think so, but I didn't try. One of the natures of sockopt feature is supporting cgroup attach. cgroup_bpf_prog_attach()/cgroup_bpf_link_attach() is probably something that you're looking for.
Sound good
Quoted 11 lines
IIUC, you can attach the prog onto the cgroup where all the sockets are set using the bpf timestamping function. So the current impl is cleaner and has better isolation (to filter out those unmatched flows).
Quoted 5 lines
In Simon's netstacklat[1] tool we are forced process all RX timestamp (hooking fentry/tcp_recv_timestamp), and then we have a BPF filter[2] on the cgroup IDs that we are interested in (which is a significant overhead, as this is deployed at Cloudflare production scale).
Quoted 7 lines
I can feel the pain when filtering in this kind of relatively hot path, which is what I'm trying to avoid internally. What I've done in production (to cover those old kernels) is to just let the kernel print the information, that's it, and there is an agent continuously gathering the data, doing the match and computing latency. But it's overall complicated.
Quoted 2 lines
I hope you don't mean your internal/old approach was using printk and then analyzing this data.
Of course not :)
The internal approach is to cover the old kernels but doesn't mean the approach is old :P
Instead, the internal kernel module is super efficient and I'm trying to ship bpf with such an ability. The fact is we've already deployed in production: 7x24 running, zero sampling.
Please see page 24 where there is a brief introduction on how to deal with the log part:
https://lpc.events/event/19/contributions/2055/#preview:3846
I believe this is the promising direction (ring buffer + lightweight kernel + heavy agent) we're taking.The headache part is that I need to provide an agent written in BPF to do the heavy process.
Quoted 4 lines
Quoted 3 lines
Many thanks here, I'm always interested in hearing more useful and real requirements and fancy ideas on how to monitor the latency :) Now
Quoted 8 lines
Simon Sundberg <[email protected]> have many more fancy ideas on how to monitor the latency.
The netstacklat tool is part of Simon's PhD thesis:
- https://doi.org/10.59217/qklv6836And we even gotten a paper accepted on netstacklat:
-
https://kau.diva-portal.org/smash/record.jsf?pid=diva2%3A2034009&dswid=3032Sorry, I cannot access this link. Could you give me the title of this paper?
Quoted 7 lines
Quoted 5 lines
Waiting at the Front Door - Continuous Monitoring of Latency in the Host Network Stack
Oh, I guess it hasn't been officially published right? This is the reason why I have no way to know the content.
No, it's not published yet; I'll send you a copy off-list :)
In case anyone else is interested, the paper is now on Arxiv:
https://arxiv.org/abs/2606.02057
-Toke
Re: [PATCH net-next 5/6] bpf: enable bpf timestamping rx in TCP layer
In reply to Jason XingQuoted 25 lines · Jason Xing
From: Jason Xing <[email protected]>
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 intcp_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 protected]>--- net/ipv4/tcp.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-)net/ipv4/tcp.c +0
-0@@ -2949,8 +2949,18 @@ int tcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int flags)release_sock (sk);
net/ipv4/tcp.c int tcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int flags) · line 2958
Quoted 10 lines
net/ipv4/tcp.c +7
-2if ((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 );
What about tcp_zc_finalize_rx_tstamp? Do you not want the rx tstamp for tcp rx zc?
Re: [PATCH net-next 5/6] bpf: enable bpf timestamping rx in TCP layer
Quoted 27 lines · Stanislav Fomichev
Quoted 25 lines · Jason Xing
From: Jason Xing <[email protected]>
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 intcp_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 protected]>--- net/ipv4/tcp.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-)net/ipv4/tcp.c +0
-0@@ -2949,8 +2949,18 @@ int tcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int flags)release_sock (sk);
Quoted 13 lines
net/ipv4/tcp.c int tcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int flags) · line 2958
Quoted 10 lines
net/ipv4/tcp.c +7
-2if ((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 );What about
tcp_zc_finalize_rx_tstamp? Do you not want the rx tstamp for tcp rx zc?
Zerocopy support should not be complicated, I suppose. Let me find a benchmark to test it locally.
Thanks,
Jason
[email protected]
AI reviewRe: [PATCH net-next 5/6] bpf: enable bpf timestamping rx in TCP layer
In reply to Jason XingThank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider: - [High] BPF sock_ops hook is called without holding the socket lock, violating core locking rules and…
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] BPF sock_ops hook is called without holding the socket lock, violating core locking rules and risking data corruption.
- [Medium] Missing BPF RX timestamping for zero-copy receive (tcp_zerocopy_receive).
- [Medium] MSG_PEEK causes event duplication, corrupting BPF statistics.
- [Medium] Data race due to lockless read of sk->sk_bpf_cb_flags.
--
commit bdfc9a5e79a8f2a8060029cde75cfb38b923754a
Author: Jason Xing <[email protected]>
bpf: enable bpf timestamping rx in TCP layer
This commit introduces BPF RX timestamping for TCP by adding a BPF hook to tcp_recvmsg(). It evaluates timestamping flags and conditionally calls bpf_skops_rx_timestamping().
Quoted 5 lines
net/ipv4/tcp.c +0
-0
net/ipv4/tcp.c int tcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int flags) · line 2956
Quoted 10 lines
net/ipv4/tcp.c +5
-2@@ -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 ))
Since the socket lock was released above, is it safe to read sk->sk_bpf_cb_flags here without READ_ONCE()? It appears bpf_sock_ops_cb_flags_set() could modify this concurrently, potentially causing a data race.
net/ipv4/tcp.c int tcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int flags) · line 2958
Quoted 2 lines
net/ipv4/tcp.c +2
-0+bpf_skops_rx_timestamping (sk, &tss, +BPF_SOCK_OPS_TSTAMP_RCV_CB );
Does executing the BPF_PROG_TYPE_SOCK_OPS program here without holding the socket lock risk data races? Since the program receives a full socket context and can call helpers like bpf_setsockopt(), this might trigger assertions in sock_owned_by_me(sk) or corrupt socket state.
If MSG_PEEK is passed in the flags, will this cause the BPF program to be invoked multiple times for the same skb? The callback doesn't seem to receive arguments indicating a peek, which might artificially inflate packet counts and skew latency metrics.
net/ipv4/tcp.c int tcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int flags) · line 2962
Quoted 4 lines
net/ipv4/tcp.c +4
-0+if (sock_flag (sk,SOCK_RCVTSTAMP ) || + tsflags &SOF_TIMESTAMPING_SOFTWARE || + tsflags &SOF_TIMESTAMPING_RAW_HARDWARE ) +tcp_recv_timestamp (msg, sk, &tss);
Will packets processed via the zero-copy receive path miss these RX timestamp events? High-performance applications using tcp_zerocopy_receive() go through tcp_zc_finalize_rx_tstamp(), which directly calls tcp_recv_timestamp() but seems to omit this new BPF callback.
Quoted 4 lines
net/ipv4/tcp.c +1
-0+ }if ((cmsg_flags &TCP_CMSG_INQ ) | msg->msg_get_inq) { msg->msg_inq =tcp_inq_hint (sk);if (cmsg_flags &TCP_CMSG_INQ )
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
Re: [PATCH net-next 5/6] bpf: enable bpf timestamping rx in TCP layer
Quoted 36 lines · On 5/19/26 4:25 PM, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] BPFsock_opshook is called without holding the socket lock, violating core locking rules and risking data corruption.
- [Medium] Missing BPF RX timestamping for zero-copy receive (tcp_zerocopy_receive).
- [Medium]MSG_PEEKcauses event duplication, corrupting BPF statistics.
- [Medium] Data race due to lockless read ofsk->sk_bpf_cb_flags.
--commit
bdfc9a5e79a8f2a8060029cde75cfb38b923754a
Author: Jason Xing <[email protected]>bpf: enable bpf timestamping rx in TCP layer
This commit introduces BPF RX timestamping for TCP by adding a BPF hook to
tcp_recvmsg(). It evaluates timestamping flags and conditionally callsbpf_skops_rx_timestamping().Quoted 5 lines
net/ipv4/tcp.c +0
-0net/ipv4/tcp.c int tcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int flags) · line 2956
Quoted 10 lines
net/ipv4/tcp.c +5
-2@@ -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 ))Since the socket lock was released above, is it safe to read
sk->sk_bpf_cb_flagshere withoutREAD_ONCE()? It appearsbpf_sock_ops_cb_flags_set()could modify this concurrently, potentially causing a data race.
Quoted 6 lines
net/ipv4/tcp.c int tcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int flags) · line 2958
Quoted 2 lines
net/ipv4/tcp.c +2
-0+bpf_skops_rx_timestamping (sk, &tss, +BPF_SOCK_OPS_TSTAMP_RCV_CB );Does executing the
BPF_PROG_TYPE_SOCK_OPSprogram here without holding the socket lock risk data races? Since the program receives a full socket context and can call helpers likebpf_setsockopt(), this might trigger assertions insock_owned_by_me(sk)or corrupt socket state.
For this point above, I think it's not true since bpf_sock_ops_setsockopt will return EOPNOTSUPP when
bpf_sock->op <= BPF_SOCK_OPS_WRITE_HDR_OPT_CB
Quoted 19 lines
If
MSG_PEEKis passed in the flags, will this cause the BPF program to be invoked multiple times for the same skb? The callback doesn't seem to receive arguments indicating a peek, which might artificially inflate packet counts and skew latency metrics.net/ipv4/tcp.c int tcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int flags) · line 2962
Quoted 4 lines
net/ipv4/tcp.c +4
-0+if (sock_flag (sk,SOCK_RCVTSTAMP ) || + tsflags &SOF_TIMESTAMPING_SOFTWARE || + tsflags &SOF_TIMESTAMPING_RAW_HARDWARE ) +tcp_recv_timestamp (msg, sk, &tss);Will packets processed via the zero-copy receive path miss these RX timestamp events? High-performance applications using
tcp_zerocopy_receive()go throughtcp_zc_finalize_rx_tstamp(), which directly callstcp_recv_timestamp()but seems to omit this new BPF callback.Quoted 4 lines
net/ipv4/tcp.c +1
-0+ }if ((cmsg_flags &TCP_CMSG_INQ ) | msg->msg_get_inq) { msg->msg_inq =tcp_inq_hint (sk);if (cmsg_flags &TCP_CMSG_INQ )
Re: [PATCH net-next 5/6] bpf: enable bpf timestamping rx in TCP layer
In reply to [email protected]Quoted 24 lines · On Tue, May 19, 2026 at 4:25 PM <[email protected]> wrote:
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] BPFsock_opshook is called without holding the socket lock, violating core locking rules and risking data corruption.
- [Medium] Missing BPF RX timestamping for zero-copy receive (tcp_zerocopy_receive).
- [Medium]MSG_PEEKcauses event duplication, corrupting BPF statistics.
- [Medium] Data race due to lockless read ofsk->sk_bpf_cb_flags.
--commit
bdfc9a5e79a8f2a8060029cde75cfb38b923754a
Author: Jason Xing <[email protected]>bpf: enable bpf timestamping rx in TCP layer
This commit introduces BPF RX timestamping for TCP by adding a BPF hook to
tcp_recvmsg(). It evaluates timestamping flags and conditionally callsbpf_skops_rx_timestamping().Quoted 7 lines
net/ipv4/tcp.c +0
-0@@ -2949,8 +2949,18 @@ int tcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int flags)release_sock (sk);
Quoted 13 lines
net/ipv4/tcp.c int tcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int flags) · line 2956
Quoted 8 lines
net/ipv4/tcp.c +5
-2if ((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 ))Since the socket lock was released above, is it safe to read
sk->sk_bpf_cb_flagshere withoutREAD_ONCE()? It appearsbpf_sock_ops_cb_flags_set()could modify this concurrently, potentially causing a data race.
Good catch. I'll add the pair then.
Quoted 8 lines
net/ipv4/tcp.c int tcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int flags) · line 2958
Quoted 3 lines
net/ipv4/tcp.c +2
-0+bpf_skops_rx_timestamping (sk, &tss, +BPF_SOCK_OPS_TSTAMP_RCV_CB );Does executing the
BPF_PROG_TYPE_SOCK_OPSprogram here without holding the socket lock risk data races? Since the program receives a full socket context and can call helpers likebpf_setsockopt(), this might trigger assertions insock_owned_by_me(sk)or corrupt socket state.
IIUC, we have such protections which are reflected in bpf_test_access_bpf_calls().
Quoted 5 lines
If
MSG_PEEKis passed in the flags, will this cause the BPF program to be invoked multiple times for the same skb? The callback doesn't seem to receive arguments indicating a peek, which might artificially inflate packet counts and skew latency metrics.
This is the pre-existing behavior. Yes, it can happen, but I don't regard it as a problem.
Quoted 10 lines
net/ipv4/tcp.c int tcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int flags) · line 2962
Quoted 5 lines
net/ipv4/tcp.c +4
-0+if (sock_flag (sk,SOCK_RCVTSTAMP ) || + tsflags &SOF_TIMESTAMPING_SOFTWARE || + tsflags &SOF_TIMESTAMPING_RAW_HARDWARE ) +tcp_recv_timestamp (msg, sk, &tss);Will packets processed via the zero-copy receive path miss these RX timestamp events? High-performance applications using
tcp_zerocopy_receive()go throughtcp_zc_finalize_rx_tstamp(), which directly callstcp_recv_timestamp()but seems to omit this new BPF callback.
Agreed. I'll support zc mode as Stan pointed out earlier.
Thanks,
Jason
Quoted 8 lines
net/ipv4/tcp.c int tcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int flags) · line 2966
Quoted 5 lines
net/ipv4/tcp.c +1
-0+ }if ((cmsg_flags &TCP_CMSG_INQ ) | msg->msg_get_inq) { msg->msg_inq =tcp_inq_hint (sk);if (cmsg_flags &TCP_CMSG_INQ )--
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
PATCH 6/6 selftests/bpf: Add RX latency tests for bpf timestamping
In reply to Jason XingFrom: Jason Xing <[email protected]>
The client sends data, and the server reads it via read(), which triggers tcp_recvmsg() internally. And then the computation is done in the BPF_SOCK_OPS_TSTAMP_RCV_CB callback. Extend test_tcp() to cover the new BPF RX timestamping path.
One more crucial thing is to add usleep to allow the delay of workqueue turning on global time record of each skb in recv path, or else the test might fail due to no timestamp in the skb.
Verify that SO_TIMESTAMPING RX (software) and BPF RX timestamping can work simultaneously on the same socket without conflict. When enable_socket_timestamping is set, enable SOF_TIMESTAMPING_SOFTWARE | SOF_TIMESTAMPING_RX_SOFTWARE on afd and use recvmsg() to verify the SCM_TIMESTAMPING cmsg contains a valid software RX timestamp.
This exercises the mixed mode where all 4 timestamping paths (SO TX on cfd, SO RX on afd, BPF TX on cfd, BPF RX on afd) run together. The standalone mode (enable_socket_timestamping=false) keeps only BPF TX and BPF RX active using read().
--- .../bpf/prog_tests/net_timestamping.c | 69 ++++++++++++++++++- .../selftests/bpf/progs/net_timestamping.c | 35 ++++++++++ 2 files changed, 103 insertions(+), 1 deletion(-)
tools/testing/selftests/bpf/prog_tests/net_timestamping.c +68 -1
@@ -143,11 +143,45 @@ static void test_socket_timestamping(int fd)SK_TS_ACK =0 ; } +static bool recv_verify_rx_timestamp (int fd,char *buf,int len) +{ +struct scm_timestamping *tss =NULL ; +char ctrl[1024 ]; +struct msghdr msg = {}; +struct iovec iov; +struct cmsghdr *cm; +int ret; + + iov.iov_base = buf; + iov.iov_len = len; + msg.msg_iov = &iov; + msg.msg_iovlen =1 ; + msg.msg_control = ctrl; + msg.msg_controllen =sizeof (ctrl); + + ret =recvmsg (fd, &msg,0 ); +if (!ASSERT_EQ (ret, len,"recvmsg from client" )) +return false; + +for (cm =CMSG_FIRSTHDR (&msg); cm; cm =CMSG_NXTHDR (&msg, cm)) { +if (cm->cmsg_level ==SOL_SOCKET && + cm->cmsg_type ==SCM_TIMESTAMPING ) { + tss = (void *)CMSG_DATA (cm); +break ; + } + } + +ASSERT_TRUE (tss !=NULL ,"SCM_TIMESTAMPING cmsg present" ); +ASSERT_TRUE (tss && (tss->ts[0 ].tv_sec || tss->ts[0 ].tv_nsec), +"rx sw timestamp non-zero" ); +return true; +} +static void test_tcp (int family,bool enable_socket_timestamping) {struct net_timestamping__bss *bss;char buf[cfg_payload_len]; -int sfd =-1 , cfd =-1 ; +int sfd =-1 , cfd =-1 , afd =-1 ;unsigned int sock_opt;struct netns_obj *ns;int cg_fd;
@@ -187,7 +221,27 @@ static void test_tcp(int family, bool enable_socket_timestamping)if (!ASSERT_OK_FD (cfd,"connect_to_fd_server" )) goto out; + afd =accept (sfd,NULL ,NULL ); +if (!ASSERT_OK_FD (afd,"accept" )) + goto out; + +/* net_enable_timestamp() defers the static key update via +* schedule_work() when CONFIG_JUMP_LABEL is set. Give the +* workqueue a chance to run so that netstamp_needed_key is +* active and skb->tstamp gets populated in the receive path. +*/ +usleep (10000 ); +if (enable_socket_timestamping) { +unsigned int rx_opt; + + rx_opt =SOF_TIMESTAMPING_SOFTWARE | +SOF_TIMESTAMPING_RX_SOFTWARE ; + ret =setsockopt (afd,SOL_SOCKET ,SO_TIMESTAMPING , + (char *)&rx_opt,sizeof (rx_opt)); +if (!ASSERT_OK (ret,"setsockopt SO_TIMESTAMPING RX on afd" )) + goto out; + sock_opt =SOF_TIMESTAMPING_SOFTWARE |SOF_TIMESTAMPING_OPT_ID | SOF_TIMESTAMPING_TX_SCHED |
@@ -207,6 +261,15 @@ static void test_tcp(int family, bool enable_socket_timestamping)if (!ASSERT_EQ (ret,sizeof (buf),"send to server" )) goto out; +if (enable_socket_timestamping) { +if (!recv_verify_rx_timestamp (afd, buf,sizeof (buf))) + goto out; + }else { + ret =read (afd, buf,sizeof (buf)); +if (!ASSERT_EQ (ret,sizeof (buf),"recv from client" )) + goto out; + } +if (enable_socket_timestamping)test_socket_timestamping (cfd);
@@ -215,8 +278,12 @@ static void test_tcp(int family, bool enable_socket_timestamping)ASSERT_EQ (bss->nr_sched,1 ,"nr_sched" );ASSERT_EQ (bss->nr_txsw,1 ,"nr_txsw" );ASSERT_EQ (bss->nr_ack,1 ,"nr_ack" ); +ASSERT_EQ (bss->nr_passive,1 ,"nr_passive" ); +ASSERT_GE (bss->nr_rcv,1 ,"nr_rcv" ); out: +if (afd >=0 ) +close (afd);if (sfd >=0 )close (sfd);if (cfd >=0 )
tools/testing/selftests/bpf/progs/net_timestamping.c +35 -0
@@ -6,6 +6,8 @@#include "bpf_kfuncs.h" #include <errno.h> +extern u64 bpf_ktime_get_real_ns(void) __ksym; +__u32 monitored_pid =0 ;int nr_active;
@@ -14,6 +16,7 @@ int nr_passive;int nr_sched;int nr_txsw;int nr_ack; +int nr_rcv;struct sk_stg {__u64 sendmsg_ns;/* record ts when sendmsg is called */
@@ -65,6 +68,22 @@ static int bpf_test_sockopt(void *ctx, const struct sock *sk, int expected)return 0 ; } +static int bpf_test_rx_sockopt (void *ctx,const struct sock *sk,int expected) +{ +int tmp, new =SK_BPF_CB_RX_TIMESTAMPING ; +int opt =SK_BPF_CB_FLAGS ; +int level =SOL_SOCKET ; + +if (bpf_setsockopt (ctx, level, opt, &new,sizeof (new)) != expected) +return 1 ; + +if (bpf_getsockopt (ctx, level, opt, &tmp,sizeof (tmp)) != expected || + (!expected && tmp != new)) +return 1 ; + +return 0 ; +} +static bool bpf_test_access_sockopt (void *ctx,const struct sock *sk) {if (bpf_test_sockopt (ctx, sk, -EOPNOTSUPP ))
@@ -224,6 +243,9 @@ int skops_sockopt(struct bpf_sock_ops *skops)case BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB : nr_active += !bpf_test_sockopt (skops, sk,0 );break ; +case BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB : + nr_passive += !bpf_test_rx_sockopt (skops, sk,0 ); +break ;case BPF_SOCK_OPS_TSTAMP_SENDMSG_CB :if (bpf_test_delay (skops, sk)) nr_snd +=1 ;
@@ -240,6 +262,19 @@ int skops_sockopt(struct bpf_sock_ops *skops)if (bpf_test_delay (skops, sk)) nr_ack +=1 ;break ; +case BPF_SOCK_OPS_TSTAMP_RCV_CB : { +u64 sw_tstamp, now, delay; + + sw_tstamp = (u64 )skops->args[0 ] | ((u64 )skops->args[1 ] <<32 ); +if (!sw_tstamp) +break ; + + now =bpf_ktime_get_real_ns (); + delay = now - sw_tstamp; +if (delay < delay_tolerance_nsec) + nr_rcv +=1 ; +break ; + } }return 1 ;
Signature · 1 line
2.43.7
[email protected]
AI reviewRe: [PATCH net-next 6/6] selftests/bpf: Add RX latency tests for bpf timestamping
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] The return values of `ASSERT_TRUE` are ignored in `recv_verify_rx_timestamp`, causing it to…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] The return values of ASSERT_TRUE are ignored in recv_verify_rx_timestamp, causing it to unconditionally return true on successful recvmsg.
- [Medium] Using a fixed usleep(10000) to wait for a workqueue to run is prone to test flakiness on heavily loaded CI systems.
--
commit 3f1fdeab36943dce908082eb563628a5f8ca353b
Author: Jason Xing <[email protected]>
selftests/bpf: Add RX latency tests for bpf timestamping
This commit extends the test_tcp() selftest to cover the new BPF RX timestamping path. It tests a mixed mode where SO TX/RX and BPF TX/RX timestamping operate simultaneously, verifying they do not conflict.
Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/net_timestamping.c +0
-0
[ ... ]
Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/net_timestamping.c +2
-0@@ -143,11 +143,45 @@ static void test_socket_timestamping(int fd)SK_TS_ACK =0 ; } +static bool recv_verify_rx_timestamp (int fd,char *buf,int len) +{
[ ... ]
tools/testing/selftests/bpf/prog_tests/net_timestamping.c static void test_socket_timestamping(int fd) · line 178
Quoted 13 lines
tools/testing/selftests/bpf/prog_tests/net_timestamping.c +13
-0+for (cm =CMSG_FIRSTHDR (&msg); cm; cm =CMSG_NXTHDR (&msg, cm)) { +if (cm->cmsg_level ==SOL_SOCKET && + cm->cmsg_type ==SCM_TIMESTAMPING ) { + tss = (void *)CMSG_DATA (cm); +break ; + } + } + +ASSERT_TRUE (tss !=NULL ,"SCM_TIMESTAMPING cmsg present" ); +ASSERT_TRUE (tss && (tss->ts[0 ].tv_sec || tss->ts[0 ].tv_nsec), +"rx sw timestamp non-zero" ); +return true; +}
Since the return values of ASSERT_TRUE() are ignored here, will this function unconditionally return true even if the assertions fail?
If the caller expects a false return to abort early, this could cause the test to proceed regardless of the assertion results.
[ ... ]
Quoted 1 line
tools/testing/selftests/bpf/prog_tests/net_timestamping.c +0
-0@@ -187,7 +221,27 @@ static void test_tcp(int family, bool enable_socket_timestamping)
[ ... ]
tools/testing/selftests/bpf/prog_tests/net_timestamping.c static void test_tcp(int family, bool enable_socket_timestamping) · line 233
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/net_timestamping.c +10
-0+ afd =accept (sfd,NULL ,NULL ); +if (!ASSERT_OK_FD (afd,"accept" )) + goto out; + +/* net_enable_timestamp() defers the static key update via +* schedule_work() when CONFIG_JUMP_LABEL is set. Give the +* workqueue a chance to run so that netstamp_needed_key is +* active and skb->tstamp gets populated in the receive path. +*/ +usleep (10000 );
Could this fixed 10ms sleep cause test flakiness on heavily loaded CI systems?
If the workqueue is delayed for longer than 10ms, the static key might not be active in time, which would leave skb->tstamp unpopulated and cause the test to fail intermittently.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
Re: [PATCH net-next 6/6] selftests/bpf: Add RX latency tests for bpf timestamping
Quoted 48 lines · On Tue, May 19, 2026 at 4:25 PM <[email protected]> wrote:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] The return values ofASSERT_TRUEare ignored inrecv_verify_rx_timestamp, causing it to unconditionally returntrueon successfulrecvmsg.
- [Medium] Using a fixedusleep(10000)to wait for a workqueue to run is prone to test flakiness on heavily loaded CI systems.
--commit
3f1fdeab36943dce908082eb563628a5f8ca353b
Author: Jason Xing <[email protected]>selftests/bpf: Add RX latency tests for bpf timestamping
This commit extends the
test_tcp()selftest to cover the new BPF RX timestamping path. It tests a mixed mode where SO TX/RX and BPF TX/RX timestamping operate simultaneously, verifying they do not conflict.Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/net_timestamping.c +0
-0[ ... ]
Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/net_timestamping.c +2
-0@@ -143,11 +143,45 @@ static void test_socket_timestamping(int fd)SK_TS_ACK =0 ; } +static bool recv_verify_rx_timestamp (int fd,char *buf,int len) +{[ ... ]
tools/testing/selftests/bpf/prog_tests/net_timestamping.c static void test_socket_timestamping(int fd) · line 178
Quoted 13 lines
tools/testing/selftests/bpf/prog_tests/net_timestamping.c +13
-0+for (cm =CMSG_FIRSTHDR (&msg); cm; cm =CMSG_NXTHDR (&msg, cm)) { +if (cm->cmsg_level ==SOL_SOCKET && + cm->cmsg_type ==SCM_TIMESTAMPING ) { + tss = (void *)CMSG_DATA (cm); +break ; + } + } + +ASSERT_TRUE (tss !=NULL ,"SCM_TIMESTAMPING cmsg present" ); +ASSERT_TRUE (tss && (tss->ts[0 ].tv_sec || tss->ts[0 ].tv_nsec), +"rx sw timestamp non-zero" ); +return true; +}Since the return values of
ASSERT_TRUE()are ignored here, will this function unconditionally return true even if the assertions fail?
Quoted 2 lines
If the caller expects a false return to abort early, this could cause the test to proceed regardless of the assertion results.
Even though it doesn't affect the result, I will add checks here:
if (!ASSERT_TRUE(tss != NULL, "SCM_TIMESTAMPING cmsg present"))
return false;
if (!ASSERT_TRUE(tss->ts[0].tv_sec || tss->ts[0].tv_nsec, "rx sw
timestamp non-zero"))
return false;Quoted 18 lines
[ ... ]
Quoted 1 line
tools/testing/selftests/bpf/prog_tests/net_timestamping.c +0
-0@@ -187,7 +221,27 @@ static void test_tcp(int family, bool enable_socket_timestamping)[ ... ]
tools/testing/selftests/bpf/prog_tests/net_timestamping.c static void test_tcp(int family, bool enable_socket_timestamping) · line 233
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/net_timestamping.c +10
-0+ afd =accept (sfd,NULL ,NULL ); +if (!ASSERT_OK_FD (afd,"accept" )) + goto out; + +/* net_enable_timestamp() defers the static key update via +* schedule_work() when CONFIG_JUMP_LABEL is set. Give the +* workqueue a chance to run so that netstamp_needed_key is +* active and skb->tstamp gets populated in the receive path. +*/ +usleep (10000 );
Quoted 6 lines
Could this fixed 10ms sleep cause test flakiness on heavily loaded CI systems?
If the workqueue is delayed for longer than 10ms, the static key might not be active in time, which would leave
skb->tstampunpopulated and cause the test to fail intermittently.
From my limited knowledge about CI tests, 10ms is probably enough...
Thanks,
Jason
Quoted 3 lines
--
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
Quoted 2 lines · Jason Xing
From: Jason Xing <[email protected]>
You have a spelling mistake in the subject/title.
timetamp -> timestamp
--Jesper
Quoted 8 lines · Jesper Dangaard Brouer
Quoted 2 lines · Jason Xing
From: Jason Xing <[email protected]>
You have a spelling mistake in the subject/title.
timetamp -> timestamp
Oh, right, sorry, this is not the first time I made this mistake... missing 's'... :(
Thanks,
Jason