bpf-timestamp: support rx side
Subsystems (9)
v2 cover bpf-timestamp: 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
---
v2
1. support tcp rx zc case (Stan)
2. add if-statements for ASSERT_TRUE (sashiko)
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 | 10 +++ tools/include/uapi/linux/bpf.h | 5 ++ .../bpf/prog_tests/net_timestamping.c | 71 ++++++++++++++++++- .../selftests/bpf/progs/net_timestamping.c | 35 +++++++++ 9 files changed, 172 insertions(+), 5 deletions(-)
Signature · 1 line
2.43.7
PATCH v2 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
PATCH v2 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
PATCH v2 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 v2 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 v2 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 | 10 ++++++++++ 1 file changed, 10 insertions(+)
net/ipv4/tcp.c +10 -0
@@ -2353,6 +2353,16 @@ void tcp_recv_timestamp(struct msghdr *msg, const struct sock *sk,int new_tstamp =sock_flag (sk,SOCK_TSTAMP_NEW );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 ((struct sock *)sk, tss, +BPF_SOCK_OPS_TSTAMP_RCV_CB ); + +if (!sock_flag (sk,SOCK_RCVTSTAMP ) && + !(tsflags &SOF_TIMESTAMPING_SOFTWARE ) && + !(tsflags &SOF_TIMESTAMPING_RAW_HARDWARE )) +return ; +if (tss->ts[0 ]) {if (sock_flag (sk,SOCK_RCVTSTAMP )) {struct timespec64 tv =ktime_to_timespec64 (tss->ts[0 ]);
Signature · 1 line
2.43.7
PATCH v2 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 | 71 ++++++++++++++++++- .../selftests/bpf/progs/net_timestamping.c | 35 +++++++++ 2 files changed, 105 insertions(+), 1 deletion(-)
tools/testing/selftests/bpf/prog_tests/net_timestamping.c +70 -1
@@ -143,11 +143,47 @@ 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 ; + } + } + + if (!ASSERT_TRUE(tss != NULL, "SCM_TIMESTAMPING cmsg present") || + !ASSERT_TRUE(tss->ts[0].tv_sec || tss->ts[0].tv_nsec, "rx sw + timestamp non-zero")) + return false; + + 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 +223,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 +263,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 +280,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
Kuniyuki Iwashima
maintainerRe: [PATCH net-next v2 5/6] bpf: enable bpf timestamping rx in TCP layer
In reply to Jason Xingnet/ipv4/tcp.c void tcp_recv_timestamp(struct msghdr *msg, const struct sock *sk,
Quoted 24 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 | 10 ++++++++++ 1 file changed, 10 insertions(+)net/ipv4/tcp.c +0
-0@@ -2353,6 +2353,16 @@ void tcp_recv_timestamp(struct msghdr *msg, const struct sock *sk,
Simply drop const from here rather than casting to non-const for bpf_skops_rx_timestamping().
sk in all callers of tcp_recv_timestamp() is not const.
Quoted 19 lines
net/ipv4/tcp.c +10
-1int new_tstamp =sock_flag (sk,SOCK_TSTAMP_NEW );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 ((struct sock *)sk, tss, +BPF_SOCK_OPS_TSTAMP_RCV_CB ); + +if (!sock_flag (sk,SOCK_RCVTSTAMP ) && + !(tsflags &SOF_TIMESTAMPING_SOFTWARE ) && + !(tsflags &SOF_TIMESTAMPING_RAW_HARDWARE )) +return ; +if (tss->ts[0 ]) {if (sock_flag (sk,SOCK_RCVTSTAMP )) {struct timespec64 tv =ktime_to_timespec64 (tss->ts[0 ]); --2.43.7
Kuniyuki Iwashima
maintainerQuoted 1 line
[PATCH net-next v2 0/6] bpf-timestamp: support rx side
nit: This should target to bpf-next.
Quoted 37 lines · Jason Xing
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
---
v21. support tcp rx zc case (Stan)
2. add if-statements forASSERT_TRUE(sashiko)Jason Xing (6):
bpf: Addbpf_ktime_get_real_ns()kfunc
net: exportsock_disable_timestamp()declaration
bpf: supportbpf_setsockoptfor bpf timestamping rx feature
bpf: addBPF_SOCK_OPS_TSTAMP_RCV_CBcallback
bpf: enable bpf timestamping rx in TCP layer selftests/bpf: Add RX latency tests for bpf timestampinginclude/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 | 10 +++ tools/include/uapi/linux/bpf.h | 5 ++ .../bpf/prog_tests/net_timestamping.c | 71 ++++++++++++++++++- .../selftests/bpf/progs/net_timestamping.c | 35 +++++++++ 9 files changed, 172 insertions(+), 5 deletions(-)--
2.43.7
Kuniyuki Iwashima
maintainerRe: [PATCH net-next v2 3/6] bpf: support bpf_setsockopt for bpf timestamping rx feature
In reply to Jason XingQuoted 27 lines · Jason Xing
From: Jason Xing <[email protected]>
Add
SK_BPF_CB_RX_TIMESTAMPINGcallback flag to enable RX timestamping viabpf_setsockopt(SK_BPF_CB_FLAGS).Add
SOCK_BPF_TIMESTAMPING_RXinto enumsock_flagsthat is used as centralized management of net_enable/disable_timestamp. Note that only one of them (timestamp,so_timestampingand bpf timestamping rx) can enable and disable the global time record of skbs.In addition, include
SOCK_BPF_TIMESTAMPING_RXinSK_FLAGS_TIMESTAMPso that__sk_destruct()callingsock_disable_timestamp()to thoroughly turn off the global time record.Signed-off-by Jason Xing <[email protected]>--- 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 +0
-0
include/net/sock.h enum sock_flags { · line 1025
Quoted 4 lines
include/net/sock.h +1
-0@@ -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 */
Just a note, this will be the last bit on 32bit kernel.
Quoted 47 lines
include/net/sock.h +3
-1}; -#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
-1@@ -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 ; --2.43.7
Re: [PATCH net-next v2 5/6] bpf: enable bpf timestamping rx in TCP layer
In reply to Kuniyuki IwashimaQuoted 30 lines · Kuniyuki Iwashima
net/ipv4/tcp.c void tcp_recv_timestamp(struct msghdr *msg, const struct sock *sk,
Quoted 24 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 | 10 ++++++++++ 1 file changed, 10 insertions(+)net/ipv4/tcp.c +0
-0@@ -2353,6 +2353,16 @@ void tcp_recv_timestamp(struct msghdr *msg, const struct sock *sk,Simply drop const from here rather than casting to non-const for
bpf_skops_rx_timestamping().
Quoted 1 line
sk in all callers of
tcp_recv_timestamp()is not const.
Will do that. Thanks.
Quoted 21 lines
Quoted 21 lines
net/ipv4/tcp.c +10
-1int new_tstamp =sock_flag (sk,SOCK_TSTAMP_NEW );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 ((struct sock *)sk, tss, +BPF_SOCK_OPS_TSTAMP_RCV_CB ); + +if (!sock_flag (sk,SOCK_RCVTSTAMP ) && + !(tsflags &SOF_TIMESTAMPING_SOFTWARE ) && + !(tsflags &SOF_TIMESTAMPING_RAW_HARDWARE )) +return ; +if (tss->ts[0 ]) {if (sock_flag (sk,SOCK_RCVTSTAMP )) {struct timespec64 tv =ktime_to_timespec64 (tss->ts[0 ]); --2.43.7
Quoted 4 lines · Kuniyuki Iwashima
Quoted 2 lines
[PATCH net-next v2 0/6] bpf-timestamp: support rx side
nit: This should target to bpf-next.
Fair enough. Will rebase.
Quoted 40 lines · Jason Xing
Quoted 37 lines
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
---
v21. support tcp rx zc case (Stan)
2. add if-statements forASSERT_TRUE(sashiko)Jason Xing (6):
bpf: Addbpf_ktime_get_real_ns()kfunc
net: exportsock_disable_timestamp()declaration
bpf: supportbpf_setsockoptfor bpf timestamping rx feature
bpf: addBPF_SOCK_OPS_TSTAMP_RCV_CBcallback
bpf: enable bpf timestamping rx in TCP layer selftests/bpf: Add RX latency tests for bpf timestampinginclude/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 | 10 +++ tools/include/uapi/linux/bpf.h | 5 ++ .../bpf/prog_tests/net_timestamping.c | 71 ++++++++++++++++++- .../selftests/bpf/progs/net_timestamping.c | 35 +++++++++ 9 files changed, 172 insertions(+), 5 deletions(-)--
2.43.7
[email protected]
AI reviewRe: [PATCH net-next v2 2/6] net: export sock_disable_timestamp() declaration
In reply to Jason XingThank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Critical] Lockless modification of `sk->sk_flags` from a BPF context will race with user context socket…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Critical] Lockless modification of sk->sk_flags from a BPF context will race with user context socket modifications, corrupting critical socket flags.
- [High] Concurrent lockless calls to sock_disable_timestamp result in a Time-of-Check to Time-of-Use (TOCTOU) race that unbalances the global timestamp reference counter. --
commit 070236e8d76283be57936f36bb4d9ae98296d6e1
Author: Jason Xing <[email protected]>
net: export sock_disable_timestamp() declaration
This commit removes the static label from sock_disable_timestamp() and exports its declaration. This change allows the BPF timestamping RX feature to dynamically turn off the global time record function.
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;
[Severity: Critical]
Since this function is being exported for use by the BPF timestamping rx feature, will it be called from a lockless context?
If the BPF_SOCK_OPS_TSTAMP_RCV_CB hook calls this outside the socket lock (for instance, after tcp_recvmsg() drops lock_sock(sk)), this non-atomic read-modify-write of sk->sk_flags could race with user context socket modifications like setsockopt().
Could this concurrent access cause torn reads and writes, permanently corrupting sk_flags and potentially leading to use-after-free issues if lifecycle flags like SOCK_DEAD are lost?
[Severity: High]
Could exporting this for lockless BPF contexts also result in a Time-of-Check to Time-of-Use race that unbalances the global network timestamping reference count?
If multiple threads execute recvmsg() on the same socket concurrently without the socket lock, they might both evaluate "if (sk->sk_flags & flags)" as true, clear the flags, and then subsequently call net_disable_timestamp().
Would this double-decrement unbalance the global counter and prematurely disable network timestamping for all sockets on the system?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
[email protected]
AI reviewRe: [PATCH net-next v2 3/6] bpf: support bpf_setsockopt for bpf timestamping rx feature
In reply to Jason XingThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Lockless invocation of `sk_bpf_set_get_cb_flags` causes `sk_flags` corruption and global netstamp…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Lockless invocation of sk_bpf_set_get_cb_flags causes sk_flags corruption and global netstamp refcount leaks via sock_enable_timestamp() / sock_disable_timestamp(). --
commit 3f400ad3640a19655fbde51eedae40506ebf95a8
Author: Jason Xing <[email protected]>
bpf: support bpf_setsockopt for bpf timestamping rx feature
Add SK_BPF_CB_RX_TIMESTAMPING callback flag to enable RX timestamping via bpf_setsockopt. It updates sk_flags centrally to manage the global network timestamp static keys.
Quoted 5 lines
net/core/filter.c +0
-0
net/core/filter.c static int sk_bpf_set_get_cb_flags(struct sock *sk, char *optval, bool getopt) · line 5313
Quoted 10 lines
net/core/filter.c +7
-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 )); + }
[Severity: High]
Could this lead to socket flags corruption if called locklessly?
sk_bpf_set_get_cb_flags() can be reached without the socket lock held, such as through the following call chain:
bpf_unlocked_sk_setsockopt()
__bpf_setsockopt()
sk_bpf_set_get_cb_flags()
Since sock_enable_timestamp() and sock_disable_timestamp() use non-atomic bitwise operations to modify sk->sk_flags, concurrent execution might race and accidentally drop critical flags like SOCK_DEAD or SOCK_DONE.
Additionally, because the checks against previous_flags in these functions are not atomic with the bitwise updates, could a race here lead to unbalanced net_enable_timestamp() or net_disable_timestamp() calls, permanently skewing the global netstamp static key refcount?
Quoted 4 lines
net/core/filter.c +1
-0+ sk->sk_bpf_cb_flags = sk_bpf_cb_flags;return 0 ;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
[email protected]
AI reviewRe: [PATCH net-next v2 4/6] bpf: add BPF_SOCK_OPS_TSTAMP_RCV_CB callback
In reply to Jason XingThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] Multi-line comments added in `bpf.h` violate BPF subsystem style rules. --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] Multi-line comments added in bpf.h violate BPF subsystem style rules. --
commit 70849c3ed58c6a550a61d9e2bed8d76cf14bd58a
Author: Jason Xing <[email protected]>
bpf: add BPF_SOCK_OPS_TSTAMP_RCV_CB callback
This commit adds the BPF_SOCK_OPS_TSTAMP_RCV_CB callback and a receive tunnel function to allow the kernel to report both software and hardware timestamps.
include/uapi/linux/bpf.h enum { · line 7176
Quoted 13 lines
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. +*/
[Severity: Low]
This isn't a bug, but does this multi-line comment follow the preferred subsystem style? Should the opening /* be placed on its own line?
[ ... ]
tools/include/uapi/linux/bpf.h enum { · line 7175
Quoted 13 lines
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. +*/
[Severity: Low]
This isn't a bug, but the same multi-line comment style observation applies here. Should the opening /* be placed on its own line?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
[email protected]
AI reviewRe: [PATCH net-next v2 6/6] selftests/bpf: Add RX latency tests for bpf timestamping
In reply to Jason XingThank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] Using a fixed 10ms sleep to synchronize with a kernel workqueue introduces a race condition that…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] Using a fixed 10ms sleep to synchronize with a kernel workqueue introduces a race condition that can lead to flaky tests.
- [Low] Unescaped newline in string literal causes compilation failure.
--
commit fd07aa65d96b1e91b17cb391aadbc4cf4af4b414
Author: Jason Xing <[email protected]>
selftests/bpf: Add RX latency tests for bpf timestamping
This patch extends the test_tcp() functionality to cover the new BPF RX timestamping path. It adds new assertions, workqueue synchronization, and mixed timestamping modes to verify SO_TIMESTAMPING and BPF RX can function together without 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,47 @@ 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 176
Quoted 3 lines
tools/testing/selftests/bpf/prog_tests/net_timestamping.c +3
-0+ if (!ASSERT_TRUE(tss != NULL, "SCM_TIMESTAMPING cmsg present") || + !ASSERT_TRUE(tss->ts[0].tv_sec || tss->ts[0].tv_nsec, "rx sw + timestamp non-zero"))
[Severity: Low]
Does this unescaped newline in the string literal cause a compilation failure?
The error message string is split across lines without a trailing backslash or string concatenation, which results in invalid syntax in C.
Quoted 7 lines
tools/testing/selftests/bpf/prog_tests/net_timestamping.c +5
-0+return false; + +return true; +} +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 235
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 );
[Severity: Medium]
Will this hardcoded 10ms sleep cause the test to become flaky?
Using fixed delays to synchronize with asynchronous kernel workqueues can lead to race conditions. In slow VMs or heavily loaded CI systems, the workqueue might take longer than 10ms to run, leaving skb->tstamp unpopulated and causing the test to fail intermittently.
Could a polling mechanism with a reasonable timeout be used here instead?
Quoted 3 lines
tools/testing/selftests/bpf/prog_tests/net_timestamping.c +2
-0+if (enable_socket_timestamping) { +unsigned int rx_opt;
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6