From patchwork Sat Oct 12 04:06:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Xing X-Patchwork-Id: 13833342 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-pl1-f173.google.com (mail-pl1-f173.google.com [209.85.214.173]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DDCA7A41; Sat, 12 Oct 2024 04:07:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728706027; cv=none; b=KpZWQe0eTXSNz2Inm+cF64gnMXFsAzqYcA02VU9PjAmKRZqa/1kKEv4YaLqsEUzGghOz9bnoBYBCCbepiqqceSF16AAKIucqQAgNg3Lk+cWdpKcoWbaMEkNY6OVz+bq5/tsN8++iGqrxfwIJqQop7CGCXsroydjnjBROo2rqwis= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728706027; c=relaxed/simple; bh=wJnv0tTqU0CMlRstJzGHN2WlH8u1NBlKQiQvV5YyzXk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=VpDLQNnn5wrzQYrNw66WkoUBsxVx72PZZ38B1Arnxpg2AqDil0u8u2SlVGwYUSGg2dsyCphH5PRVnWU7g0LnHjak0qFMppguk9jiqZ6Mej8verbGhhM/MDpnQnITClMxUzsJzG5GXazJdMaVrAY8arVwsp9+NYHAN4UbOSzieWQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=VoY6tOEz; arc=none smtp.client-ip=209.85.214.173 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="VoY6tOEz" Received: by mail-pl1-f173.google.com with SMTP id d9443c01a7336-20ca1b6a80aso11020035ad.2; Fri, 11 Oct 2024 21:07:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728706025; x=1729310825; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=uhkJToL5ye2pJS5PepNqyb+cP4g8tXstix29oTK5pK0=; b=VoY6tOEzdcb8Qb8iQgMpuVPAc2RH5aOwDB9K57ru4xOI0w3gMCWM/5UxBAAkvYCfNw +GeelSGptKt5tQ+4MJMYg7bmb1IVmd6BlX2n/R661pbUb11IKGXQBCJgqgYJEmL3PpSW jDAFIvOdlVQ/wx+/nujMX0FaAGAAMw8jGp3gl/UynPYYh/6IEvDQH1z5KzR6RA+ecGVE VU28TFuyD9uyYKS38ClJMPkYmY2JFDUtvEiP/25x0UpV7m99g6rdEimLUNzQfN3RtnTn yT4Hkac8CsTGbdzJzsiVU6F12IpO6LpdmruXPkxMAsWmpwiMDPRI99IomiK7XKJAJSR4 oKcA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728706025; x=1729310825; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=uhkJToL5ye2pJS5PepNqyb+cP4g8tXstix29oTK5pK0=; b=IND1unF2XaLKB7zK/6PxdN+pBOmwRansWeb+qh+oJwlxelIYjK31ENO1r8fpfGcdBO SDITb7eLlbrRfWa89iLnkpuBmjQDwPY+epPtdfVLbmrluQ6/H/yfoxsmb3f5sJXI6FZW qeTXJx8qIbaa+gr9+Su1u2PNkzlvwHuqfy+bXN5rr2rUPAlkRZizdWmqmlmTLw6JQ/tD Yr/e8WjZJrwzBw4EjnJ5Dvnt3aoJT9cK8/lYyEb19MlZclMaz6jeCu/w9+qCgJ83FfUJ oDSL278ZVqCrridniLsjYuO4mpSWLaHECnkM6MFqevf+GEAzu8LJRmLRIlDCkDuaIL+S 7wfg== X-Forwarded-Encrypted: i=1; AJvYcCVHRO7AePR3Z8TF3ItxGX24xw+lSrjIp9ipn/bKGupNJrVRGh7Hwv+5icvJkVS1ZXfKjGsSDyc=@vger.kernel.org X-Gm-Message-State: AOJu0Yx08nGj78Nce3U8b8faHJCxjX1wOCn4a4P+S02owo0r7vhdq7Xn 5RvZ3R4u+ZfzQWUXUjL3spn1v6FXYGlqlpHQS70qUgdENBEM1AT4 X-Google-Smtp-Source: AGHT+IHOLIcJ60eI2w8jPC3Z8ZS0BpXVxWlubAW59YeZgPgao9Z5i1/92kajeuVfEF93/Yq+L2qu7A== X-Received: by 2002:a17:903:22d1:b0:20c:79f1:fee9 with SMTP id d9443c01a7336-20cbb183470mr34035755ad.11.1728706024915; Fri, 11 Oct 2024 21:07:04 -0700 (PDT) Received: from KERNELXING-MB0.tencent.com ([43.132.141.25]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-20c8c21301dsm30939685ad.199.2024.10.11.21.07.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Oct 2024 21:07:04 -0700 (PDT) From: Jason Xing To: davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, dsahern@kernel.org, willemdebruijn.kernel@gmail.com, willemb@google.com, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com, jolsa@kernel.org Cc: bpf@vger.kernel.org, netdev@vger.kernel.org, Jason Xing Subject: [PATCH net-next v2 01/12] net-timestamp: introduce socket tsflag requestors Date: Sat, 12 Oct 2024 12:06:40 +0800 Message-Id: <20241012040651.95616-2-kerneljasonxing@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20241012040651.95616-1-kerneljasonxing@gmail.com> References: <20241012040651.95616-1-kerneljasonxing@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org From: Jason Xing We need a separate tsflag to control bpf extension feature so that we will not affect the behaviors of existing applications. The idea of introducing requestors for better extension (not only serving bpf extension) comes from Vadim Fedorenko. Signed-off-by: Jason Xing --- include/net/ip.h | 2 +- include/net/sock.h | 15 +++++++++++---- net/can/j1939/socket.c | 2 +- net/core/skbuff.c | 5 +++-- net/core/sock.c | 8 ++++---- net/ipv4/ip_output.c | 2 +- net/ipv4/ip_sockglue.c | 2 +- net/ipv4/tcp.c | 2 +- net/ipv6/ip6_output.c | 2 +- net/ipv6/ping.c | 2 +- net/ipv6/raw.c | 2 +- net/ipv6/udp.c | 2 +- net/sctp/socket.c | 2 +- net/socket.c | 4 ++-- 14 files changed, 30 insertions(+), 22 deletions(-) diff --git a/include/net/ip.h b/include/net/ip.h index bab084df1567..b0a836aebc33 100644 --- a/include/net/ip.h +++ b/include/net/ip.h @@ -96,7 +96,7 @@ static inline void ipcm_init_sk(struct ipcm_cookie *ipcm, ipcm_init(ipcm); ipcm->sockc.mark = READ_ONCE(inet->sk.sk_mark); - ipcm->sockc.tsflags = READ_ONCE(inet->sk.sk_tsflags); + ipcm->sockc.tsflags = READ_ONCE(inet->sk.sk_tsflags[SOCKETOPT_TS_REQUESTOR]); ipcm->oif = READ_ONCE(inet->sk.sk_bound_dev_if); ipcm->addr = inet->inet_saddr; ipcm->protocol = inet->inet_num; diff --git a/include/net/sock.h b/include/net/sock.h index b32f1424ecc5..8cf278c957b3 100644 --- a/include/net/sock.h +++ b/include/net/sock.h @@ -234,6 +234,13 @@ struct sock_common { struct bpf_local_storage; struct sk_filter; +enum { + SOCKETOPT_TS_REQUESTOR = 0, + BPFPROG_TS_REQUESTOR, + + __MAX_TS_REQUESTOR, +}; + /** * struct sock - network layer representation of sockets * @__sk_common: shared layout with inet_timewait_sock @@ -444,7 +451,7 @@ struct sock { socket_lock_t sk_lock; u32 sk_reserved_mem; int sk_forward_alloc; - u32 sk_tsflags; + u32 sk_tsflags[__MAX_TS_REQUESTOR]; __cacheline_group_end(sock_write_rxtx); __cacheline_group_begin(sock_write_tx); @@ -1809,7 +1816,7 @@ static inline void sockcm_init(struct sockcm_cookie *sockc, const struct sock *sk) { *sockc = (struct sockcm_cookie) { - .tsflags = READ_ONCE(sk->sk_tsflags) + .tsflags = READ_ONCE(sk->sk_tsflags[SOCKETOPT_TS_REQUESTOR]) }; } @@ -2617,7 +2624,7 @@ static inline void sock_recv_timestamp(struct msghdr *msg, struct sock *sk, struct sk_buff *skb) { struct skb_shared_hwtstamps *hwtstamps = skb_hwtstamps(skb); - u32 tsflags = READ_ONCE(sk->sk_tsflags); + u32 tsflags = READ_ONCE(sk->sk_tsflags[SOCKETOPT_TS_REQUESTOR]); ktime_t kt = skb->tstamp; /* * generate control messages if @@ -2652,7 +2659,7 @@ static inline void sock_recv_cmsgs(struct msghdr *msg, struct sock *sk, SOF_TIMESTAMPING_RAW_HARDWARE) if (sk->sk_flags & FLAGS_RECV_CMSGS || - READ_ONCE(sk->sk_tsflags) & TSFLAGS_ANY) + READ_ONCE(sk->sk_tsflags[SOCKETOPT_TS_REQUESTOR]) & TSFLAGS_ANY) __sock_recv_cmsgs(msg, sk, skb); else if (unlikely(sock_flag(sk, SOCK_TIMESTAMP))) sock_write_timestamp(sk, skb->tstamp); diff --git a/net/can/j1939/socket.c b/net/can/j1939/socket.c index 305dd72c844c..8f5799930a93 100644 --- a/net/can/j1939/socket.c +++ b/net/can/j1939/socket.c @@ -996,7 +996,7 @@ static void __j1939_sk_errqueue(struct j1939_session *session, struct sock *sk, if (!(jsk->state & J1939_SOCK_ERRQUEUE)) return; - tsflags = READ_ONCE(sk->sk_tsflags); + tsflags = READ_ONCE(sk->sk_tsflags[SOCKETOPT_TS_REQUESTOR]); switch (type) { case J1939_ERRQUEUE_TX_ACK: if (!(tsflags & SOF_TIMESTAMPING_TX_ACK)) diff --git a/net/core/skbuff.c b/net/core/skbuff.c index 00afeb90c23a..ab0a59f1e14d 100644 --- a/net/core/skbuff.c +++ b/net/core/skbuff.c @@ -5490,7 +5490,8 @@ static void __skb_complete_tx_timestamp(struct sk_buff *skb, serr->ee.ee_info = tstype; serr->opt_stats = opt_stats; serr->header.h4.iif = skb->dev ? skb->dev->ifindex : 0; - if (READ_ONCE(sk->sk_tsflags) & SOF_TIMESTAMPING_OPT_ID) { + if (READ_ONCE(sk->sk_tsflags[SOCKETOPT_TS_REQUESTOR]) & + SOF_TIMESTAMPING_OPT_ID) { serr->ee.ee_data = skb_shinfo(skb)->tskey; if (sk_is_tcp(sk)) serr->ee.ee_data -= atomic_read(&sk->sk_tskey); @@ -5551,7 +5552,7 @@ void __skb_tstamp_tx(struct sk_buff *orig_skb, if (!sk) return; - tsflags = READ_ONCE(sk->sk_tsflags); + tsflags = READ_ONCE(sk->sk_tsflags[SOCKETOPT_TS_REQUESTOR]); if (!hwtstamps && !(tsflags & SOF_TIMESTAMPING_OPT_TX_SWHW) && skb_shinfo(orig_skb)->tx_flags & SKBTX_IN_PROGRESS) return; diff --git a/net/core/sock.c b/net/core/sock.c index 083d438d8b6f..52c8c5a5ba27 100644 --- a/net/core/sock.c +++ b/net/core/sock.c @@ -908,7 +908,7 @@ int sock_set_timestamping(struct sock *sk, int optname, return -EINVAL; if (val & SOF_TIMESTAMPING_OPT_ID && - !(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID)) { + !(sk->sk_tsflags[SOCKETOPT_TS_REQUESTOR] & SOF_TIMESTAMPING_OPT_ID)) { if (sk_is_tcp(sk)) { if ((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN)) @@ -932,7 +932,7 @@ int sock_set_timestamping(struct sock *sk, int optname, return ret; } - WRITE_ONCE(sk->sk_tsflags, val); + WRITE_ONCE(sk->sk_tsflags[SOCKETOPT_TS_REQUESTOR], val); sock_valbool_flag(sk, SOCK_TSTAMP_NEW, optname == SO_TIMESTAMPING_NEW); if (val & SOF_TIMESTAMPING_RX_SOFTWARE) @@ -1797,7 +1797,7 @@ int sk_getsockopt(struct sock *sk, int level, int optname, * Don't change the beviour for the old case SO_TIMESTAMPING_OLD. */ if (optname == SO_TIMESTAMPING_OLD || sock_flag(sk, SOCK_TSTAMP_NEW)) { - v.timestamping.flags = READ_ONCE(sk->sk_tsflags); + v.timestamping.flags = READ_ONCE(sk->sk_tsflags[SOCKETOPT_TS_REQUESTOR]); v.timestamping.bind_phc = READ_ONCE(sk->sk_bind_phc); } break; @@ -2930,7 +2930,7 @@ int __sock_cmsg_send(struct sock *sk, struct cmsghdr *cmsg, case SCM_TS_OPT_ID: if (sk_is_tcp(sk)) return -EINVAL; - tsflags = READ_ONCE(sk->sk_tsflags); + tsflags = READ_ONCE(sk->sk_tsflags[SOCKETOPT_TS_REQUESTOR]); if (!(tsflags & SOF_TIMESTAMPING_OPT_ID)) return -EINVAL; if (cmsg->cmsg_len != CMSG_LEN(sizeof(u32))) diff --git a/net/ipv4/ip_output.c b/net/ipv4/ip_output.c index e5c55a95063d..ded504458d5d 100644 --- a/net/ipv4/ip_output.c +++ b/net/ipv4/ip_output.c @@ -1050,7 +1050,7 @@ static int __ip_append_data(struct sock *sk, cork->length += length; if (cork->tx_flags & SKBTX_ANY_TSTAMP && - READ_ONCE(sk->sk_tsflags) & SOF_TIMESTAMPING_OPT_ID) { + READ_ONCE(sk->sk_tsflags[SOCKETOPT_TS_REQUESTOR]) & SOF_TIMESTAMPING_OPT_ID) { if (cork->flags & IPCORK_TS_OPT_ID) { tskey = cork->ts_opt_id; } else { diff --git a/net/ipv4/ip_sockglue.c b/net/ipv4/ip_sockglue.c index cf377377b52d..fac8f593c43a 100644 --- a/net/ipv4/ip_sockglue.c +++ b/net/ipv4/ip_sockglue.c @@ -509,7 +509,7 @@ static bool ipv4_datagram_support_cmsg(const struct sock *sk, * or without payload (SOF_TIMESTAMPING_OPT_TSONLY). */ info = PKTINFO_SKB_CB(skb); - if (!(READ_ONCE(sk->sk_tsflags) & SOF_TIMESTAMPING_OPT_CMSG) || + if (!(READ_ONCE(sk->sk_tsflags[SOCKETOPT_TS_REQUESTOR]) & SOF_TIMESTAMPING_OPT_CMSG) || !info->ipi_ifindex) return false; diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c index 82cc4a5633ce..6c8968eb4427 100644 --- a/net/ipv4/tcp.c +++ b/net/ipv4/tcp.c @@ -2245,7 +2245,7 @@ void tcp_recv_timestamp(struct msghdr *msg, const struct sock *sk, struct scm_timestamping_internal *tss) { int new_tstamp = sock_flag(sk, SOCK_TSTAMP_NEW); - u32 tsflags = READ_ONCE(sk->sk_tsflags); + u32 tsflags = READ_ONCE(sk->sk_tsflags[SOCKETOPT_TS_REQUESTOR]); bool has_timestamping = false; if (tss->ts[0].tv_sec || tss->ts[0].tv_nsec) { diff --git a/net/ipv6/ip6_output.c b/net/ipv6/ip6_output.c index 205673179b3c..c983e0ca6f72 100644 --- a/net/ipv6/ip6_output.c +++ b/net/ipv6/ip6_output.c @@ -1547,7 +1547,7 @@ static int __ip6_append_data(struct sock *sk, } if (cork->tx_flags & SKBTX_ANY_TSTAMP && - READ_ONCE(sk->sk_tsflags) & SOF_TIMESTAMPING_OPT_ID) { + READ_ONCE(sk->sk_tsflags[SOCKETOPT_TS_REQUESTOR]) & SOF_TIMESTAMPING_OPT_ID) { if (cork->flags & IPCORK_TS_OPT_ID) { tskey = cork->ts_opt_id; } else { diff --git a/net/ipv6/ping.c b/net/ipv6/ping.c index 88b3fcacd4f9..0080b7c3a475 100644 --- a/net/ipv6/ping.c +++ b/net/ipv6/ping.c @@ -119,7 +119,7 @@ static int ping_v6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) return -EINVAL; ipcm6_init_sk(&ipc6, sk); - ipc6.sockc.tsflags = READ_ONCE(sk->sk_tsflags); + ipc6.sockc.tsflags = READ_ONCE(sk->sk_tsflags[SOCKETOPT_TS_REQUESTOR]); ipc6.sockc.mark = READ_ONCE(sk->sk_mark); fl6.flowi6_oif = oif; diff --git a/net/ipv6/raw.c b/net/ipv6/raw.c index 8476a3944a88..cd02aa02d813 100644 --- a/net/ipv6/raw.c +++ b/net/ipv6/raw.c @@ -778,7 +778,7 @@ static int rawv6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) fl6.flowi6_uid = sk->sk_uid; ipcm6_init(&ipc6); - ipc6.sockc.tsflags = READ_ONCE(sk->sk_tsflags); + ipc6.sockc.tsflags = READ_ONCE(sk->sk_tsflags[SOCKETOPT_TS_REQUESTOR]); ipc6.sockc.mark = fl6.flowi6_mark; if (sin6) { diff --git a/net/ipv6/udp.c b/net/ipv6/udp.c index 52dfbb2ff1a8..008cc0282338 100644 --- a/net/ipv6/udp.c +++ b/net/ipv6/udp.c @@ -1349,7 +1349,7 @@ int udpv6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) ipcm6_init(&ipc6); ipc6.gso_size = READ_ONCE(up->gso_size); - ipc6.sockc.tsflags = READ_ONCE(sk->sk_tsflags); + ipc6.sockc.tsflags = READ_ONCE(sk->sk_tsflags[SOCKETOPT_TS_REQUESTOR]); ipc6.sockc.mark = READ_ONCE(sk->sk_mark); /* destination address check */ diff --git a/net/sctp/socket.c b/net/sctp/socket.c index 078bcb3858c7..f66f21d6363e 100644 --- a/net/sctp/socket.c +++ b/net/sctp/socket.c @@ -9463,7 +9463,7 @@ void sctp_copy_sock(struct sock *newsk, struct sock *sk, newsk->sk_type = sk->sk_type; newsk->sk_bound_dev_if = sk->sk_bound_dev_if; newsk->sk_flags = sk->sk_flags; - newsk->sk_tsflags = sk->sk_tsflags; + memcpy(newsk->sk_tsflags, sk->sk_tsflags, sizeof(u32) * __MAX_TS_REQUESTOR); newsk->sk_no_check_tx = sk->sk_no_check_tx; newsk->sk_no_check_rx = sk->sk_no_check_rx; newsk->sk_reuse = sk->sk_reuse; diff --git a/net/socket.c b/net/socket.c index 3b1b65b9f471..24619a27909a 100644 --- a/net/socket.c +++ b/net/socket.c @@ -845,7 +845,7 @@ static bool skb_is_swtx_tstamp(const struct sk_buff *skb, int false_tstamp) static ktime_t get_timestamp(struct sock *sk, struct sk_buff *skb, int *if_index) { - bool cycles = READ_ONCE(sk->sk_tsflags) & SOF_TIMESTAMPING_BIND_PHC; + bool cycles = READ_ONCE(sk->sk_tsflags[SOCKETOPT_TS_REQUESTOR]) & SOF_TIMESTAMPING_BIND_PHC; struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb); struct net_device *orig_dev; ktime_t hwtstamp; @@ -944,7 +944,7 @@ void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk, } memset(&tss, 0, sizeof(tss)); - tsflags = READ_ONCE(sk->sk_tsflags); + tsflags = READ_ONCE(sk->sk_tsflags[SOCKETOPT_TS_REQUESTOR]); if ((tsflags & SOF_TIMESTAMPING_SOFTWARE && (tsflags & SOF_TIMESTAMPING_RX_SOFTWARE || skb_is_err_queue(skb) || From patchwork Sat Oct 12 04:06:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Xing X-Patchwork-Id: 13833343 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-pl1-f171.google.com (mail-pl1-f171.google.com [209.85.214.171]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BE6EDA41; Sat, 12 Oct 2024 04:07:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728706032; cv=none; b=fQISq5n6IglILDObQYkO3r9EDbnIH9whxsamDKhT/MfFKIDOJ1IY/rCAxakv737LpPt45IVCgY7HoX2eQTI/XRSVSCi7P1lnM4N4g0dzfDv1lMco8mQnaQkzEi6n82XGyk6kSDVTJjC3yxvvwEVy0dUD9nCelPG5Zsk44eCjbP8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728706032; c=relaxed/simple; bh=3mR+esWzAtVtdfZr/yWT9xIPWWL2YtmTjDBcWlF0aJY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=AtRINVFtw3FWbWbOwQ/HrfeI5bOLGjEoNyB4Bz2P75CkTdtPe1I6jSRLFxXQDhKHs1z1CZ6rLipWI0Z2/0LIfDC2PtQ4J9klR8sXxVrZE5Vm3L7U+8747ZzlF2t1HCWfq+o606d8eO25qbreimEPCogL4JDykAQTfthvOrVzlV4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=ZUKmfZEG; arc=none smtp.client-ip=209.85.214.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="ZUKmfZEG" Received: by mail-pl1-f171.google.com with SMTP id d9443c01a7336-20b7259be6fso28621085ad.0; Fri, 11 Oct 2024 21:07:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728706030; x=1729310830; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=oRDW78W3vNOph0PBEFxQBqyMlXJV2fT4fp+c01EtsxA=; b=ZUKmfZEGGRjwtMXtq0YeVNhonksNfguNW3zpmc1KoOs4Jsv8cl5GqRijSgXv+IjdXN EY4W1QBbONMOakA0l7Q21XbHvDso9WCEAdMkN7IenBapOhM/d84SVraFzK790THGnCUH 8U8VpIQFSLpHRLKn/WNbe5nO3B+bDZLt5MAm2nOeLybTuYSsGDiYY+pkBwiJOSHRzobP 2QPxTWVbG5tcBAYTr9ETtyF8baY960EeJm+ckmrMnfckcaGA3sJ/MobTkKn+FvxbX/vn Ai7e2mjdbBSuE0bTXTP7qfh0zw7lJ919EqEdE04Mj5VkFy+f5yEzKtWWJGj5JAsQyO11 ZQmQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728706030; x=1729310830; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=oRDW78W3vNOph0PBEFxQBqyMlXJV2fT4fp+c01EtsxA=; b=f4yYxfmzP+LFM2iytN8bGmNiLHeELumKJZmTF///Xu5rBQyxg6mXv7KbV6YBmk14xr RCdrhB3YLWMBy6gHDjcodri7VZDmuE6jIgXtg319r6f/P/pNfWrle8cIZcL6pB9dIfB6 KgeqDkGb5Out7icYgyPxAg5R18KvK2A5Wdz/6zgIZTAsRQyBqeSszZXoRPkUR7X3rfO5 9xPIBs2IJAI69INl2LdkzJv17QTYvk4DqutnlNPmh9Q18z/Tj24xI6SMsDILAYg6dtlm IIg+uFORV8X9S2Ilucub13n6Wptetc07FRJBjvfnZTp6Vej17fH8m+VD4kTSuJBllHPe 4eGQ== X-Forwarded-Encrypted: i=1; AJvYcCXl0y4oXfiASEs9RhIlPY59h8kdHJ2MHaBTYmZ04SdsdyY51ABk4KS7XJ5U0lZxb0+HC7dVXI4=@vger.kernel.org X-Gm-Message-State: AOJu0Yw6D8KxDCsY3vGvCu7L2dbm3ipXRbhN4XVOYC1ToZsXlsp0nUE2 ED0chsrnQXJ0R/3Yby6WEYPVF8hWUza+v8s7/kvviKPOT9nxjo9G X-Google-Smtp-Source: AGHT+IGsunNmXPH6ajgdh5vJdT/YifQG5BM/cJwAIcSh720+aq4JKNF5IWUOSCFesz0UWY/PZJjlIw== X-Received: by 2002:a17:902:f60b:b0:20c:b401:7489 with SMTP id d9443c01a7336-20cb40175f9mr36272395ad.24.1728706029807; Fri, 11 Oct 2024 21:07:09 -0700 (PDT) Received: from KERNELXING-MB0.tencent.com ([43.132.141.25]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-20c8c21301dsm30939685ad.199.2024.10.11.21.07.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Oct 2024 21:07:09 -0700 (PDT) From: Jason Xing To: davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, dsahern@kernel.org, willemdebruijn.kernel@gmail.com, willemb@google.com, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com, jolsa@kernel.org Cc: bpf@vger.kernel.org, netdev@vger.kernel.org, Jason Xing Subject: [PATCH net-next v2 02/12] net-timestamp: open gate for bpf_setsockopt Date: Sat, 12 Oct 2024 12:06:41 +0800 Message-Id: <20241012040651.95616-3-kerneljasonxing@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20241012040651.95616-1-kerneljasonxing@gmail.com> References: <20241012040651.95616-1-kerneljasonxing@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org From: Jason Xing For now, we support bpf_setsockopt only TX timestamps flags. Users can use something like this in bpf program to turn on the feature: flags = SOF_TIMESTAMPING_TX_SCHED; bpf_setsockopt(skops, SOL_SOCKET, SO_TIMESTAMPING, &flags, sizeof(flags)); Later, I will support each Tx flags one by one based on this. Signed-off-by: Jason Xing --- include/net/sock.h | 2 ++ net/core/filter.c | 27 +++++++++++++++++++++++++++ net/core/sock.c | 35 ++++++++++++++++++++++++----------- 3 files changed, 53 insertions(+), 11 deletions(-) diff --git a/include/net/sock.h b/include/net/sock.h index 8cf278c957b3..66ecd78f1dfe 100644 --- a/include/net/sock.h +++ b/include/net/sock.h @@ -2890,6 +2890,8 @@ void sock_def_readable(struct sock *sk); int sock_bindtoindex(struct sock *sk, int ifindex, bool lock_sk); void sock_set_timestamp(struct sock *sk, int optname, bool valbool); +int sock_get_timestamping(struct so_timestamping *timestamping, + sockptr_t optval, unsigned int optlen); int sock_set_timestamping(struct sock *sk, int optname, struct so_timestamping timestamping); diff --git a/net/core/filter.c b/net/core/filter.c index bd0d08bf76bb..996426095bd9 100644 --- a/net/core/filter.c +++ b/net/core/filter.c @@ -5204,10 +5204,30 @@ static const struct bpf_func_proto bpf_get_socket_uid_proto = { .arg1_type = ARG_PTR_TO_CTX, }; +static int bpf_sock_set_timestamping(struct sock *sk, + struct so_timestamping *timestamping) +{ + u32 flags = timestamping->flags; + + if (flags & ~SOF_TIMESTAMPING_MASK) + return -EINVAL; + + if (!(flags & (SOF_TIMESTAMPING_TX_SCHED | SOF_TIMESTAMPING_TX_SOFTWARE | + SOF_TIMESTAMPING_TX_ACK))) + return -EINVAL; + + WRITE_ONCE(sk->sk_tsflags[BPFPROG_TS_REQUESTOR], flags); + + return 0; +} + static int sol_socket_sockopt(struct sock *sk, int optname, char *optval, int *optlen, bool getopt) { + struct so_timestamping ts; + int ret = 0; + switch (optname) { case SO_REUSEADDR: case SO_SNDBUF: @@ -5225,6 +5245,13 @@ static int sol_socket_sockopt(struct sock *sk, int optname, break; case SO_BINDTODEVICE: break; + case SO_TIMESTAMPING_NEW: + case SO_TIMESTAMPING_OLD: + ret = sock_get_timestamping(&ts, KERNEL_SOCKPTR(optval), + *optlen); + if (!ret) + ret = bpf_sock_set_timestamping(sk, &ts); + return ret; default: return -EINVAL; } diff --git a/net/core/sock.c b/net/core/sock.c index 52c8c5a5ba27..a6e0d51a5f72 100644 --- a/net/core/sock.c +++ b/net/core/sock.c @@ -894,6 +894,27 @@ static int sock_timestamping_bind_phc(struct sock *sk, int phc_index) return 0; } +int sock_get_timestamping(struct so_timestamping *timestamping, + sockptr_t optval, unsigned int optlen) +{ + int val; + + if (copy_from_sockptr(&val, optval, sizeof(val))) + return -EFAULT; + + if (optlen == sizeof(*timestamping)) { + if (copy_from_sockptr(timestamping, optval, + sizeof(*timestamping))) { + return -EFAULT; + } + } else { + memset(timestamping, 0, sizeof(*timestamping)); + timestamping->flags = val; + } + + return 0; +} + int sock_set_timestamping(struct sock *sk, int optname, struct so_timestamping timestamping) { @@ -1402,17 +1423,9 @@ int sk_setsockopt(struct sock *sk, int level, int optname, case SO_TIMESTAMPING_NEW: case SO_TIMESTAMPING_OLD: - if (optlen == sizeof(timestamping)) { - if (copy_from_sockptr(×tamping, optval, - sizeof(timestamping))) { - ret = -EFAULT; - break; - } - } else { - memset(×tamping, 0, sizeof(timestamping)); - timestamping.flags = val; - } - ret = sock_set_timestamping(sk, optname, timestamping); + ret = sock_get_timestamping(×tamping, optval, optlen); + if (!ret) + ret = sock_set_timestamping(sk, optname, timestamping); break; case SO_RCVLOWAT: From patchwork Sat Oct 12 04:06:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Xing X-Patchwork-Id: 13833344 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-pl1-f173.google.com (mail-pl1-f173.google.com [209.85.214.173]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AA8CCA41; Sat, 12 Oct 2024 04:07:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728706037; cv=none; b=i4pP/9EVQcLqOfzaYOnMPVxaRHgHFH62urxlSmmOUM9R9XIAz2mCGW9HCpqh0mNPrknTKO4nOXXJYcJB0rgLLEHahc42k43CQWUP9hfH7nMjRAE816sdhZ29uyqTxvuYJn11+HttY3dPQcIozx8vV5QmCqADSxZpg4yGk+onMuc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728706037; c=relaxed/simple; bh=qMTb6Yx/idR+PoJPwqe6W83I/MrE1JYLd5TNr/QChh4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=rI7Lqj4NCT9B5wVsMvMbqryd0PvcdLZHnZsgXP7+oE9gx2MozRwF9Gr6UI/mHMSHZnfRsYaXzr8U+/mpcMRpn4BatSk1cODgAsmul3iMk4mqc5xKgu9RipYKxfaEZzVrETXjzcAI18sdUn0LEu8/RWjLSECzdH4d8uKgrh3GHTw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=lUlDydMk; arc=none smtp.client-ip=209.85.214.173 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="lUlDydMk" Received: by mail-pl1-f173.google.com with SMTP id d9443c01a7336-20bb610be6aso27909505ad.1; Fri, 11 Oct 2024 21:07:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728706035; x=1729310835; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=tJnps7CANHVZ0rSjZN72iSKjT69c16RYHWcaWeFxSnI=; b=lUlDydMk2hNAdxLQCLPqQOq4qG+eih0p1H+hc9WfaAfQu0nDvbcdRKWwCjyx/nV6i0 YXHitiqaPxco2JS9s7kZc7IFQ6TyG3coIZ8MlfRBdf98SMBtDrtzv/P1jimkPF17txVI yvtn+l0XT94hhZASXUOCrNayTdjMld/NB3PoMDwBMXSqw9R+1dM7m+WSrV1lfq6sFvye eKg5mcjjpZneuoLi4ortlpM8tU6FfbHIpbHVMg5qqRf/owc4hhV1Q0hF9pJD/JCTyUNa AndTYUPPJJyX2l0NECAFdPLCNUag2aatHCFHinitMAgsCg0W6NczbhnSNdB1uPIUQrwu WDeg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728706035; x=1729310835; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=tJnps7CANHVZ0rSjZN72iSKjT69c16RYHWcaWeFxSnI=; b=Tq3LHf2hMoy9RC8PjsYdnNiiWYU/zOOC1acoPFVgjpTBCtwUBTaHEUVufBy/ExG1vU M8rW8lt7ka+FdD8/nUH0ahucj9OirgNO7eFf+Y02pxJddbsg8txXJB3e0VqRPRQ3ZdxY ZN7cyh7qb4F3fc/fsjUf3idVmGfsqb/wfmzxFiYzp8Hg7y1DPmJPRSYozqQlL8xnU9Nm kv1x07MVaIEXO/9HrId38dqjmtK+Hq7yLr4TLm8dkiPPaeSZXk18ym3rxOfLYJol+NnG LRQh/Xy2e2guFnCJ5YaHh+AqBCaCr9VXzttS6zbbDq3wkADDuRzfkhpr7sBmicvu9Ssk M7rQ== X-Forwarded-Encrypted: i=1; AJvYcCVfnZOHv50VpBEchXWyibKmY8KbO0FmFyqgHZf/kDAT708AhLosUBcHVsMl6VDnqXRwi8QFlJ4=@vger.kernel.org X-Gm-Message-State: AOJu0Yw2OVf2PXgq08q6r4Zt1rALY5x+LPFsoTyMvD5zYTm5za4N7V1U 8L/js25bfwEMl4BlROCmvZiWf2UjSboCU3/SxG0ch6ggRUtQ60CV X-Google-Smtp-Source: AGHT+IFJXzj4phvdryzflx9rWeLz6xiFCVsDs4lm4pwRQKzWjIwO0tYlP9l8vHd+2Te8Xudh68+m6w== X-Received: by 2002:a17:903:244e:b0:20c:bbac:2013 with SMTP id d9443c01a7336-20cbbac21bamr27507175ad.48.1728706034932; Fri, 11 Oct 2024 21:07:14 -0700 (PDT) Received: from KERNELXING-MB0.tencent.com ([43.132.141.25]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-20c8c21301dsm30939685ad.199.2024.10.11.21.07.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Oct 2024 21:07:14 -0700 (PDT) From: Jason Xing To: davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, dsahern@kernel.org, willemdebruijn.kernel@gmail.com, willemb@google.com, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com, jolsa@kernel.org Cc: bpf@vger.kernel.org, netdev@vger.kernel.org, Jason Xing Subject: [PATCH net-next v2 03/12] net-timestamp: reorganize in skb_tstamp_tx_output() Date: Sat, 12 Oct 2024 12:06:42 +0800 Message-Id: <20241012040651.95616-4-kerneljasonxing@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20241012040651.95616-1-kerneljasonxing@gmail.com> References: <20241012040651.95616-1-kerneljasonxing@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org From: Jason Xing It's a prep for bpf print function later. This patch only put the original generating logic into one function. No functional changes. Signed-off-by: Jason Xing --- My thought is keeping each patch small helps people to review. --- net/core/skbuff.c | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/net/core/skbuff.c b/net/core/skbuff.c index ab0a59f1e14d..f36eb9daa31a 100644 --- a/net/core/skbuff.c +++ b/net/core/skbuff.c @@ -5540,18 +5540,15 @@ void skb_complete_tx_timestamp(struct sk_buff *skb, } EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp); -void __skb_tstamp_tx(struct sk_buff *orig_skb, - const struct sk_buff *ack_skb, - struct skb_shared_hwtstamps *hwtstamps, - struct sock *sk, int tstype) +static void skb_tstamp_tx_output(struct sk_buff *orig_skb, + const struct sk_buff *ack_skb, + struct skb_shared_hwtstamps *hwtstamps, + struct sock *sk, int tstype) { struct sk_buff *skb; bool tsonly, opt_stats = false; u32 tsflags; - if (!sk) - return; - tsflags = READ_ONCE(sk->sk_tsflags[SOCKETOPT_TS_REQUESTOR]); if (!hwtstamps && !(tsflags & SOF_TIMESTAMPING_OPT_TX_SWHW) && skb_shinfo(orig_skb)->tx_flags & SKBTX_IN_PROGRESS) @@ -5595,6 +5592,17 @@ void __skb_tstamp_tx(struct sk_buff *orig_skb, __skb_complete_tx_timestamp(skb, sk, tstype, opt_stats); } + +void __skb_tstamp_tx(struct sk_buff *orig_skb, + const struct sk_buff *ack_skb, + struct skb_shared_hwtstamps *hwtstamps, + struct sock *sk, int tstype) +{ + if (!sk) + return; + + skb_tstamp_tx_output(orig_skb, ack_skb, hwtstamps, sk, tstype); +} EXPORT_SYMBOL_GPL(__skb_tstamp_tx); void skb_tstamp_tx(struct sk_buff *orig_skb, From patchwork Sat Oct 12 04:06:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Xing X-Patchwork-Id: 13833345 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-pl1-f180.google.com (mail-pl1-f180.google.com [209.85.214.180]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EE3C0A41; Sat, 12 Oct 2024 04:07:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728706042; cv=none; b=jK/C28gcGpveNb1ntg4iAMa+RH8/PidHcYKuHm7pCjwNLmmcSWPE744AN4JajrRRoUF6MEAKpwBUw2NOo8KlYsOKlltEm5QoJdKysK6BLRq3BVh8AD0t2XpvxjuDjb3jRWSPVt7ex5woL4m6uzbIfLlqVbC6BctXX5E81emWNEI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728706042; c=relaxed/simple; bh=yDVcNuKpmUuzLhzWpSjCSmxwkZNlvKM9ebIlcVMXVEo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=uM1nMWKsPRvMrZEwgnqe3xsn9i2mFfVPJZv11HPp3oLKvOskBXgpg/lRhhBTnkk+ilJQqDrTBPdiC8tqNtUZw93/h+XktpbcLb7rjrr3dL4YVWEgPoxiQ8xRO5cHaKXi9zobmUyIRAvp7da/KfkQb8vtgvaHU/7H0DEyo/I7e6k= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Vc/Mj5cN; arc=none smtp.client-ip=209.85.214.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Vc/Mj5cN" Received: by mail-pl1-f180.google.com with SMTP id d9443c01a7336-20caccadbeeso11356125ad.2; Fri, 11 Oct 2024 21:07:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728706040; x=1729310840; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=iExl0TWCr0WCpl/DT2bqOun0j97t9gABBnV9Yni75fc=; b=Vc/Mj5cNIKipGTb7th/fsPjl676LSbTJ9/eg4elETLwqjrhMf8AFY/v409dljs6cM+ A5HIklLR+A1uPFbtheBzOCU7AdzswpWMaK8/zfY/7c13q4Qr+wsP81EgDcRY11rtVj5B WFrk7USNJ6iRz9iu9+3FY7NGFZvwItL1oSHaltNAryE5mCIwQuZ931mVsN9LCU/xR6EN Hq/dQbBDCb1DOPaEu/y43ih7WaM0DnKTqabLzb+/suSQB2wcjzQeOOlZVBbMOPsyLCJQ OqoPjDEq2fr6+15SAMtRDaxlx1OrWN4IbjrxPwba+zM1fLJRrQdDCvV9IRZ43NWXTZnU N/IQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728706040; x=1729310840; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=iExl0TWCr0WCpl/DT2bqOun0j97t9gABBnV9Yni75fc=; b=tOKZNJFK8UjQ8KB9V9YLsEOG+6f2DpyF3zNSOZtKXKIDJFShdxxNwg4PIQ4W9voACO /K/w5tuKEf8ZmF3ju9j0lhQlPDqghKpNq5ySpU66UHglQ5RDY7krjtO3iGZBSA1ZHQeJ m4xkHPQdrklCOdxT+aMF8MXykL9qgQDS3f5unE3BS3hmntEqU8vRSEam6sWqQ8dRpXio E87ZOE4eodEHEy3IWWPtkg5Q/SbiOVkt5CrMeM82vmsp/zMgsRwrUpn6s1D7Aphh0vdk 97arEyqRXW8X3wQisDh/ChKvinfCun9bEwAWEDM7L+a4TQkdwG7q3baqWOHbhquURMfz BHgQ== X-Forwarded-Encrypted: i=1; AJvYcCVFTnDIhjMBZvkGHmnqEmigmBswX1WDBCtK0pftEGrWV5qNZvdtaDbPLBuf2ggibm0fIehSFB8=@vger.kernel.org X-Gm-Message-State: AOJu0YyqutcQy9tbn6EfhokvkxNJmW2XHYBinTdIh9uksF3NkQF4lrBy tHRVA2OsrzzQArTcQjj5XRcPT/UXmu4XXBLkHw115b4P089ybwWu X-Google-Smtp-Source: AGHT+IEutiaFkNyS8KftXpa5gOzRtWwtA5FTZUX0iuTue4MVYg8DtY8TBxxWHh9yKlx+lZLp7JOwtA== X-Received: by 2002:a17:903:2284:b0:20c:b483:cce2 with SMTP id d9443c01a7336-20cb483d145mr37711985ad.60.1728706040157; Fri, 11 Oct 2024 21:07:20 -0700 (PDT) Received: from KERNELXING-MB0.tencent.com ([43.132.141.25]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-20c8c21301dsm30939685ad.199.2024.10.11.21.07.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Oct 2024 21:07:19 -0700 (PDT) From: Jason Xing To: davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, dsahern@kernel.org, willemdebruijn.kernel@gmail.com, willemb@google.com, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com, jolsa@kernel.org Cc: bpf@vger.kernel.org, netdev@vger.kernel.org, Jason Xing Subject: [PATCH net-next v2 04/12] net-timestamp: add static key to control the whole bpf extension Date: Sat, 12 Oct 2024 12:06:43 +0800 Message-Id: <20241012040651.95616-5-kerneljasonxing@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20241012040651.95616-1-kerneljasonxing@gmail.com> References: <20241012040651.95616-1-kerneljasonxing@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org From: Jason Xing Willem suggested that we use a static key to control. The advantage is that we will not affect the existing applications at all if we don't load BPF program. In this patch, except the static key, I also add one logic that is used to test if the socket has enabled its tsflags in order to support bpf logic to allow both cases to happen at the same time. Or else, the skb carring related timestamp flag doesn't know which way of printing is desirable. One thing important is this patch allows print from both applications and bpf program at the same time. Now we have three kinds of print: 1) only BPF program prints 2) only application program prints 3) both can print without side effect Signed-off-by: Jason Xing --- include/net/sock.h | 1 + net/core/filter.c | 3 +++ net/core/skbuff.c | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+) diff --git a/include/net/sock.h b/include/net/sock.h index 66ecd78f1dfe..b7c51b95c92d 100644 --- a/include/net/sock.h +++ b/include/net/sock.h @@ -2889,6 +2889,7 @@ static inline bool sk_dev_equal_l3scope(struct sock *sk, int dif) void sock_def_readable(struct sock *sk); int sock_bindtoindex(struct sock *sk, int ifindex, bool lock_sk); +DECLARE_STATIC_KEY_FALSE(bpf_tstamp_control); void sock_set_timestamp(struct sock *sk, int optname, bool valbool); int sock_get_timestamping(struct so_timestamping *timestamping, sockptr_t optval, unsigned int optlen); diff --git a/net/core/filter.c b/net/core/filter.c index 996426095bd9..08135f538c99 100644 --- a/net/core/filter.c +++ b/net/core/filter.c @@ -5204,6 +5204,8 @@ static const struct bpf_func_proto bpf_get_socket_uid_proto = { .arg1_type = ARG_PTR_TO_CTX, }; +DEFINE_STATIC_KEY_FALSE(bpf_tstamp_control); + static int bpf_sock_set_timestamping(struct sock *sk, struct so_timestamping *timestamping) { @@ -5217,6 +5219,7 @@ static int bpf_sock_set_timestamping(struct sock *sk, return -EINVAL; WRITE_ONCE(sk->sk_tsflags[BPFPROG_TS_REQUESTOR], flags); + static_branch_enable(&bpf_tstamp_control); return 0; } diff --git a/net/core/skbuff.c b/net/core/skbuff.c index f36eb9daa31a..d0f912f1ff7b 100644 --- a/net/core/skbuff.c +++ b/net/core/skbuff.c @@ -5540,6 +5540,29 @@ void skb_complete_tx_timestamp(struct sk_buff *skb, } EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp); +static bool sk_tstamp_tx_flags(struct sock *sk, u32 tsflags, int tstype) +{ + u32 testflag; + + switch (tstype) { + case SCM_TSTAMP_SCHED: + testflag = SOF_TIMESTAMPING_TX_SCHED; + break; + case SCM_TSTAMP_SND: + testflag = SOF_TIMESTAMPING_TX_SOFTWARE; + break; + case SCM_TSTAMP_ACK: + testflag = SOF_TIMESTAMPING_TX_ACK; + break; + default: + return false; + } + if (tsflags & testflag) + return true; + + return false; +} + static void skb_tstamp_tx_output(struct sk_buff *orig_skb, const struct sk_buff *ack_skb, struct skb_shared_hwtstamps *hwtstamps, @@ -5558,6 +5581,9 @@ static void skb_tstamp_tx_output(struct sk_buff *orig_skb, if (!skb_may_tx_timestamp(sk, tsonly)) return; + if (!sk_tstamp_tx_flags(sk, tsflags, tstype)) + return; + if (tsonly) { #ifdef CONFIG_INET if ((tsflags & SOF_TIMESTAMPING_OPT_STATS) && @@ -5593,6 +5619,15 @@ static void skb_tstamp_tx_output(struct sk_buff *orig_skb, __skb_complete_tx_timestamp(skb, sk, tstype, opt_stats); } +static void bpf_skb_tstamp_tx_output(struct sock *sk, int tstype) +{ + u32 tsflags; + + tsflags = READ_ONCE(sk->sk_tsflags[BPFPROG_TS_REQUESTOR]); + if (!sk_tstamp_tx_flags(sk, tsflags, tstype)) + return; +} + void __skb_tstamp_tx(struct sk_buff *orig_skb, const struct sk_buff *ack_skb, struct skb_shared_hwtstamps *hwtstamps, @@ -5601,6 +5636,9 @@ void __skb_tstamp_tx(struct sk_buff *orig_skb, if (!sk) return; + if (static_branch_unlikely(&bpf_tstamp_control)) + bpf_skb_tstamp_tx_output(sk, tstype); + skb_tstamp_tx_output(orig_skb, ack_skb, hwtstamps, sk, tstype); } EXPORT_SYMBOL_GPL(__skb_tstamp_tx); From patchwork Sat Oct 12 04:06:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Xing X-Patchwork-Id: 13833346 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 99E10126BFA; Sat, 12 Oct 2024 04:07:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728706047; cv=none; b=t/XpiZTgn2vrFgJSBCBqcyIkdKgL7pgSCOx49afc9YOywrQL+9zNaAaMGmZ+z8YgqWXGiSYJhU1IgBGSWwsTZbJ+vpU0GJVKcaoIFsXEB++zVTcx80XZHtJ0nzLYepRLwwHf1pMY/lUricWthyExueSGPaXkuaZhPillqbOK3Yk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728706047; c=relaxed/simple; bh=rDljLUYtyvEolOi313e9tVjcWSdz+WrvZ+W7hx1oegY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=GMENE3BUzL65mrIVxkC1s7RPABnq++uFp3q2fsN0bxsgHDsEG5xJlAtQwV1wqmyNojD+TAjWImh71u6tvKUmKRjNoEVQHYtmzoZxoPWeS7fUimSH/eglSujuABp0HgeJOBdnfcYfdhEQLBm55OhO+Zk4CgGtP4e5vuP67D5HBM0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=cUGp9CyZ; arc=none smtp.client-ip=209.85.214.174 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="cUGp9CyZ" Received: by mail-pl1-f174.google.com with SMTP id d9443c01a7336-20c7edf2872so21136395ad.1; Fri, 11 Oct 2024 21:07:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728706045; x=1729310845; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=5hVRaBXC252Dx8TcXt1k7byW2s36lWcU4IhMjNxESCs=; b=cUGp9CyZcSTjjXQDvDdbQ0+JrAk+lzgkgnAwr6XbqztPeDBSatnywq3a90il3U9baG aNrBR+GZA0sSEx1Pp/Yc1/6abCrAvvU/irxz5ND+A/0bkdBmW3eQwOiNGKB+9XLqsYwO QSghYLF7ZkLP6mZaAEK+OoSxSBhuuw7oysgaDasXd2NVL7huHhdL87/XW8uKGtF1hips C+caAbKMEhl0Aao2M9TByVVVBWVYGOkxkCJdj81c53knu8wI2Pd2DzoKmbr2DdpTvqX2 pK7tVDDcJmoLS1tXFou8v6ScyzewuIPPSSxj11yc965KpgsLHhiTHFzWyeTAN1GDiKLb ScRg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728706045; x=1729310845; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=5hVRaBXC252Dx8TcXt1k7byW2s36lWcU4IhMjNxESCs=; b=oFuZr2nYpAqaOg+AOAJF9Oi+WeAVv4GeianJDzi+mErYJZ1qyo9Za6j1aN5HXmmJ0+ M/j41xayb9p2FyFjwgofbh19IgLjg1vcpAX+yX6I49IiT1WiEowLMBU+pXdkyEWX+VuQ Hx7C8t8jhL8eYzeZFGwtVI14fbrVJ44+sm7gHEonwF6qniEQf9iCeAHsvnjyJrhgESIq w94RuRPbfLJljtUzUnCUgWCKzxazgJCI1eKWDM4gWw431SLvHGwHLR5wBOY2nDt3Xih5 C9QfiLJ5GqKms724uTLri2peKVVo6O2+DZrT/6i38TuZdOU4LpP6VqHiMX/oZmnBwRfG hKdA== X-Forwarded-Encrypted: i=1; AJvYcCVmKyD8l8eU1WLt+C2TZ3DGr7TKSbaOKeOEygVEQ8XllmY1SEA/zY75CkQx6fotCKYfi3X8IxI=@vger.kernel.org X-Gm-Message-State: AOJu0YwTMBpz/KeW0PB0H3homsZVgFVHTEFfU/ElpcUIic1qtUkg5eSg PAwh75j6LWmdvl3BHjHfDsnD4K9Z54KDmn0kCL/PdDkArDKvva7f X-Google-Smtp-Source: AGHT+IHg8XfHQswhhxxTNWgks+HiljZNvmNN7B6/sXwYm2pE/3OS3tpIzR38RQz7ap3DxWp3AQ77og== X-Received: by 2002:a17:902:d48a:b0:20b:5aeb:9b8 with SMTP id d9443c01a7336-20ca03d67eamr73258105ad.24.1728706044857; Fri, 11 Oct 2024 21:07:24 -0700 (PDT) Received: from KERNELXING-MB0.tencent.com ([43.132.141.25]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-20c8c21301dsm30939685ad.199.2024.10.11.21.07.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Oct 2024 21:07:24 -0700 (PDT) From: Jason Xing To: davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, dsahern@kernel.org, willemdebruijn.kernel@gmail.com, willemb@google.com, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com, jolsa@kernel.org Cc: bpf@vger.kernel.org, netdev@vger.kernel.org, Jason Xing Subject: [PATCH net-next v2 05/12] net-timestamp: add bpf infrastructure to allow exposing timestamp later Date: Sat, 12 Oct 2024 12:06:44 +0800 Message-Id: <20241012040651.95616-6-kerneljasonxing@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20241012040651.95616-1-kerneljasonxing@gmail.com> References: <20241012040651.95616-1-kerneljasonxing@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org From: Jason Xing Implement basic codes so that we later can easily add each tx points. Introducing BPF_SOCK_OPS_ALL_CB_FLAGS used as a test statement can help use control whether to output or not. Signed-off-by: Jason Xing --- include/uapi/linux/bpf.h | 5 ++++- net/core/skbuff.c | 8 ++++++++ tools/include/uapi/linux/bpf.h | 5 ++++- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index c6cd7c7aeeee..157e139ed6fc 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -6900,8 +6900,11 @@ enum { * options first before the BPF program does. */ BPF_SOCK_OPS_WRITE_HDR_OPT_CB_FLAG = (1<<6), + /* Call bpf when the kernel is generating tx timestamps. + */ + BPF_SOCK_OPS_TX_TIMESTAMPING_OPT_CB_FLAG = (1<<7), /* Mask of all currently supported cb flags */ - BPF_SOCK_OPS_ALL_CB_FLAGS = 0x7F, + BPF_SOCK_OPS_ALL_CB_FLAGS = 0xFF, }; /* List of known BPF sock_ops operators. diff --git a/net/core/skbuff.c b/net/core/skbuff.c index d0f912f1ff7b..3a4110d0f983 100644 --- a/net/core/skbuff.c +++ b/net/core/skbuff.c @@ -5621,11 +5621,19 @@ static void skb_tstamp_tx_output(struct sk_buff *orig_skb, static void bpf_skb_tstamp_tx_output(struct sock *sk, int tstype) { + struct tcp_sock *tp; u32 tsflags; + if (!sk_is_tcp(sk)) + return; + tsflags = READ_ONCE(sk->sk_tsflags[BPFPROG_TS_REQUESTOR]); if (!sk_tstamp_tx_flags(sk, tsflags, tstype)) return; + + tp = tcp_sk(sk); + if (BPF_SOCK_OPS_TEST_FLAG(tp, BPF_SOCK_OPS_TX_TIMESTAMPING_OPT_CB_FLAG)) + return; } void __skb_tstamp_tx(struct sk_buff *orig_skb, diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index 1fb3cb2636e6..93853d9d4922 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -6899,8 +6899,11 @@ enum { * options first before the BPF program does. */ BPF_SOCK_OPS_WRITE_HDR_OPT_CB_FLAG = (1<<6), + /* Call bpf when the kernel is generating tx timestamps. + */ + BPF_SOCK_OPS_TX_TIMESTAMPING_OPT_CB_FLAG = (1<<7), /* Mask of all currently supported cb flags */ - BPF_SOCK_OPS_ALL_CB_FLAGS = 0x7F, + BPF_SOCK_OPS_ALL_CB_FLAGS = 0xFF, }; /* List of known BPF sock_ops operators. From patchwork Sat Oct 12 04:06:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Xing X-Patchwork-Id: 13833347 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BABF0126C05; Sat, 12 Oct 2024 04:07:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728706052; cv=none; b=IIVeaqDjh8mt59/AlIF7JRnf0B5rpI3q63S2mQUrGjI/O/9b+VQZGWEDZ7bqU3q0Df9qZ3X3vJSamQmaYlDzvToSf30dRa+YKqxyU/eUm5UtajCOX963fjWHsthBiI23uNX9E+dJZ3n/Sgn/1Z5X4aT82Pp+JkihHqf3HXZtLj8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728706052; c=relaxed/simple; bh=mRjovR8ntaoJYkJ37dbujZTNw4gAKlf5NfpDDO5l9Eo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Fvg24u5yH+021O2icvnsd6Gso9wVkxM2aB6Vrb9GdjLEQXQ67ShIANBOIbVFvQwG1TKpJ3pv/7p/GOWIdn2cuRa5YOmclgD1hja4W3JQXFZledxmyiXbrev7zC2FLaTnDqEmyBIBYY3kfFAoShBgFhIUbPY0AuMvOWbC4oBfDLE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=njwc6BG+; arc=none smtp.client-ip=209.85.214.174 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="njwc6BG+" Received: by mail-pl1-f174.google.com with SMTP id d9443c01a7336-20b5affde14so15896455ad.3; Fri, 11 Oct 2024 21:07:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728706050; x=1729310850; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=DFTdxQiqpD6DA4ro2hHwuejKJqrt64jz2yhwwbkz8zI=; b=njwc6BG+2yMx0bTqw8UnU3UtO6YXgXKL18XC5KTsfNQusK+C4FcmNoEhmc/7mf9JKZ urRK1C5BY0VcQdpMDGO6lFacG1WbTamnGlX05mTI+tZIS2aSvNUm989RdxbfkQx3GjlF fKnK2kU25u+TnUP1xp6QQniiO9Vjv/qW1jjJS2opwedvOoODFPZxl5CKrIgbLMqkMvmC xoRlDOoHuUMzBHBHWeTdpmlR2D2q1HsOsT4lyZnyYXjVpFGM5U3AuxGhoLPlOu+8ghJO oiVmXjVycZe5QXucHMXEpTXNksfIipIpBo90FICBGRWFguAp83KaYDF6N2s/cHCcr+nE 3h1g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728706050; x=1729310850; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=DFTdxQiqpD6DA4ro2hHwuejKJqrt64jz2yhwwbkz8zI=; b=aA5yZ6X1Libr0Q8NKtIzSj/CNtL2fWWlcwB6/qrb8mjyzfEFuUTaGRdAUuqHLLTLFX ELHyps9S6Iylst9nXLPZJHQ+FxS5bYfuiZCEx3gem1SMFzNW2w+lattw0DwWJk96Jqr6 WAlwT7+BYw0zaP+dq19tgfisWMN9O27p4hXpfYAnaUuKQG/KKrEWmFax1rPh4zYlj7nL wve7ie/ybcOIf0hp09EqTRwnWI0F7RevQS+E0xfpvQB3oV4sCs621Dq0nNdXgeezVe5Y zmgNMC6S+ibe4JGmzs24b4hx6Imz0Ujaj07T2Nvw2c7anmEfyyavwPe1RNvg03bM02i2 Pjvw== X-Forwarded-Encrypted: i=1; AJvYcCVTDlO7NedsId/FR8cp3pk0Cnn/EE/ofoKtuRrRyx2VmGyaHdaJ3ysSq647lPL6Qmx50lpGFek=@vger.kernel.org X-Gm-Message-State: AOJu0YxS6+rayl9ESPJRlggsaadQzUrynI/3ZCOcV4XXOecZM1Hzk5Wr IiSN91LmeR1VayTqmHZtfuClL/uQXssePMVomaeY8V76xmmTX+wU X-Google-Smtp-Source: AGHT+IFuGTd5fokqCVQPTgb9Xho914vTtq5dKo8iOrPse1q86SXIcu5RQpjb7XllQyWVYfiVpdVlxw== X-Received: by 2002:a17:902:e5ca:b0:20c:6f6f:afe5 with SMTP id d9443c01a7336-20ca16cad0dmr71115575ad.50.1728706049811; Fri, 11 Oct 2024 21:07:29 -0700 (PDT) Received: from KERNELXING-MB0.tencent.com ([43.132.141.25]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-20c8c21301dsm30939685ad.199.2024.10.11.21.07.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Oct 2024 21:07:29 -0700 (PDT) From: Jason Xing To: davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, dsahern@kernel.org, willemdebruijn.kernel@gmail.com, willemb@google.com, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com, jolsa@kernel.org Cc: bpf@vger.kernel.org, netdev@vger.kernel.org, Jason Xing Subject: [PATCH net-next v2 06/12] net-timestamp: introduce TS_SCHED_OPT_CB to generate dev xmit timestamp Date: Sat, 12 Oct 2024 12:06:45 +0800 Message-Id: <20241012040651.95616-7-kerneljasonxing@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20241012040651.95616-1-kerneljasonxing@gmail.com> References: <20241012040651.95616-1-kerneljasonxing@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org From: Jason Xing Introduce BPF_SOCK_OPS_TS_SCHED_OPT_CB flag so that we can decide to print timestamps when the skb just passes the dev layer. Signed-off-by: Jason Xing --- include/uapi/linux/bpf.h | 5 +++++ net/core/skbuff.c | 17 +++++++++++++++-- tools/include/uapi/linux/bpf.h | 5 +++++ 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 157e139ed6fc..3cf3c9c896c7 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -7019,6 +7019,11 @@ enum { * by the kernel or the * earlier bpf-progs. */ + BPF_SOCK_OPS_TS_SCHED_OPT_CB, /* Called when skb is passing through + * dev layer when SO_TIMESTAMPING + * feature is on. It indicates the + * recorded timestamp. + */ }; /* List of TCP states. There is a build check in net/ipv4/tcp.c to detect diff --git a/net/core/skbuff.c b/net/core/skbuff.c index 3a4110d0f983..16e7bdc1eacb 100644 --- a/net/core/skbuff.c +++ b/net/core/skbuff.c @@ -5632,8 +5632,21 @@ static void bpf_skb_tstamp_tx_output(struct sock *sk, int tstype) return; tp = tcp_sk(sk); - if (BPF_SOCK_OPS_TEST_FLAG(tp, BPF_SOCK_OPS_TX_TIMESTAMPING_OPT_CB_FLAG)) - return; + if (BPF_SOCK_OPS_TEST_FLAG(tp, BPF_SOCK_OPS_TX_TIMESTAMPING_OPT_CB_FLAG)) { + struct timespec64 tstamp; + u32 cb_flag; + + switch (tstype) { + case SCM_TSTAMP_SCHED: + cb_flag = BPF_SOCK_OPS_TS_SCHED_OPT_CB; + break; + default: + return; + } + + tstamp = ktime_to_timespec64(ktime_get_real()); + tcp_call_bpf_2arg(sk, cb_flag, tstamp.tv_sec, tstamp.tv_nsec); + } } void __skb_tstamp_tx(struct sk_buff *orig_skb, diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index 93853d9d4922..d60675e1a5a0 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -7018,6 +7018,11 @@ enum { * by the kernel or the * earlier bpf-progs. */ + BPF_SOCK_OPS_TS_SCHED_OPT_CB, /* Called when skb is passing through + * dev layer when SO_TIMESTAMPING + * feature is on. It indicates the + * recorded timestamp. + */ }; /* List of TCP states. There is a build check in net/ipv4/tcp.c to detect From patchwork Sat Oct 12 04:06:46 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Xing X-Patchwork-Id: 13833348 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-pl1-f181.google.com (mail-pl1-f181.google.com [209.85.214.181]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 96B69126C05; Sat, 12 Oct 2024 04:07:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728706057; cv=none; b=c4MZh9b8RfbkkZDq8DKZpLgU0NWmnXjscp4uAQbNEJl5pqmoGuysJ+RpX/eJWZ1BeFQrgk58H4mghrH0tF9nAFLgtWlvIUYNkLg+KRDLJv0JQIPnSvR85n8B3v813Fw3dOaAYoFzO0rOUlK9gA2PXLlljw4Zhw3GXHgsTwMApMU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728706057; c=relaxed/simple; bh=XvZDLG++0JSemHMyBlIi72WhzAdijUjLEc8Kz8FdA5M=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=k8bFT/2YsMAUuXwj8BZRP59u3PH7fCSTmBIqzLuB1wSJoOD/pLvWemxV5ly5ogXo+yxRSOKXe/XUakORYWRMXrVc2f1uyMKNf0/UKaF+6EtxNULgnctfSHK5ynoxHoDlt91XJcvPnbsdgnHymOZcbyM61wRqQJHWZzEI6qnld10= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=eQhHWWjf; arc=none smtp.client-ip=209.85.214.181 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="eQhHWWjf" Received: by mail-pl1-f181.google.com with SMTP id d9443c01a7336-20bc506347dso21532405ad.0; Fri, 11 Oct 2024 21:07:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728706055; x=1729310855; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=j3HJwJztm2eU03TzuaP6k1xW94JEkVBvNu9WuqJK/Rk=; b=eQhHWWjfouD4Zno3iJJ5LcfMCSL5tw158WDPFjg+IFK5sk5yCJ39kMrGlZC/pVsYYU 1pxAm5wxj5DqbHkArwZjaWPxTFI1l+AvJ9bnZyh3wzG7ntqppa6Zb0cR/I2DL2B+8XQm 1+tlKCNXMoJFwClShImiChXxUEfCK6VN9d8XdW/yeqNyg9ZWaSpRvboezB8i3wh5n6ro 113m4bcBbwajzQ7+N3N+DZugM9x/QagWW5QDEXv2tniYo7q/DKDy2JRYxGqdYTTb9urM lTaMMZlAis8AUN9TX6ECLMKwQLBELh78zO9/BUvZQ4xfx9BcjbLwgiYISJr3YMQvgxvL iA5w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728706055; x=1729310855; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=j3HJwJztm2eU03TzuaP6k1xW94JEkVBvNu9WuqJK/Rk=; b=WTtZkCNa0RaNYJQUjsjh7e3c+jej9iQmbUH9g006Ulv1QNYbj6vITWE67/ui6/I9fQ uyITwkrcD+WNWYh8NGUc5WS0YXut6zHHs0bB8bEOlDRY9IViVoOO+N86HouJ68vB2ih6 xD30a+Y07+vc7ahvabQf/4hhz5FURDDLE3ZcEYmM699SyXv7q2MebfucqJ1WA3lXDnUi DetzCst+aYDrL+xr8DYXI2UqL8D2q5QzUGpgH7PePm0711LF7BEb13hPh4kRDwU29yeO dcoh/jiRbAhKlkTd99CB2W3SLP6xducXZgaEhTELE571jP9IhCWVY6jR7bEdeV3x4ZxL TxKg== X-Forwarded-Encrypted: i=1; AJvYcCXyVZt6eWTB65zJXkQqF1G4SO/hrFjgwZk6vhasUZjBULL3ejm/Ahm/O9Eeonwo9QFr3RXZRVI=@vger.kernel.org X-Gm-Message-State: AOJu0YyW2tqMMM4UUpb3ay9JRMY208OHytKODv1HsRGQ9yuF9ZYKcDEm aMlHqUffUDAWSDOntKlO5z0TkJTP6/capTEMa8v0pP0f2Rq+xppM X-Google-Smtp-Source: AGHT+IHnd6+UieFd1ANMnkSoFi4BAvkPc5Phb7SB91p5A1CM/dmzEh6c8S9jmCKWMllv9dunVW+7VQ== X-Received: by 2002:a17:903:1c4:b0:20b:79cb:492f with SMTP id d9443c01a7336-20ca16b4aa0mr83283705ad.43.1728706054791; Fri, 11 Oct 2024 21:07:34 -0700 (PDT) Received: from KERNELXING-MB0.tencent.com ([43.132.141.25]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-20c8c21301dsm30939685ad.199.2024.10.11.21.07.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Oct 2024 21:07:34 -0700 (PDT) From: Jason Xing To: davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, dsahern@kernel.org, willemdebruijn.kernel@gmail.com, willemb@google.com, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com, jolsa@kernel.org Cc: bpf@vger.kernel.org, netdev@vger.kernel.org, Jason Xing Subject: [PATCH net-next v2 07/12] net-timestamp: introduce TS_SW_OPT_CB to generate driver timestamp Date: Sat, 12 Oct 2024 12:06:46 +0800 Message-Id: <20241012040651.95616-8-kerneljasonxing@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20241012040651.95616-1-kerneljasonxing@gmail.com> References: <20241012040651.95616-1-kerneljasonxing@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org From: Jason Xing When the skb is about to send from driver to nic, we can print timestamp by setting BPF_SOCK_OPS_TS_SW_OPT_CB in bpf program. Signed-off-by: Jason Xing --- include/uapi/linux/bpf.h | 5 +++++ net/core/skbuff.c | 13 ++++++++++--- tools/include/uapi/linux/bpf.h | 5 +++++ 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 3cf3c9c896c7..0d00539f247a 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -7024,6 +7024,11 @@ enum { * feature is on. It indicates the * recorded timestamp. */ + BPF_SOCK_OPS_TS_SW_OPT_CB, /* Called when skb is about to send + * to the nic when SO_TIMESTAMPING + * feature is on. It indicates the + * recorded timestamp. + */ }; /* List of TCP states. There is a build check in net/ipv4/tcp.c to detect diff --git a/net/core/skbuff.c b/net/core/skbuff.c index 16e7bdc1eacb..832d53de9874 100644 --- a/net/core/skbuff.c +++ b/net/core/skbuff.c @@ -5619,7 +5619,8 @@ static void skb_tstamp_tx_output(struct sk_buff *orig_skb, __skb_complete_tx_timestamp(skb, sk, tstype, opt_stats); } -static void bpf_skb_tstamp_tx_output(struct sock *sk, int tstype) +static void bpf_skb_tstamp_tx_output(struct sock *sk, int tstype, + struct skb_shared_hwtstamps *hwtstamps) { struct tcp_sock *tp; u32 tsflags; @@ -5640,11 +5641,17 @@ static void bpf_skb_tstamp_tx_output(struct sock *sk, int tstype) case SCM_TSTAMP_SCHED: cb_flag = BPF_SOCK_OPS_TS_SCHED_OPT_CB; break; + case SCM_TSTAMP_SND: + cb_flag = BPF_SOCK_OPS_TS_SW_OPT_CB; + break; default: return; } - tstamp = ktime_to_timespec64(ktime_get_real()); + if (hwtstamps) + tstamp = ktime_to_timespec64(hwtstamps->hwtstamp); + else + tstamp = ktime_to_timespec64(ktime_get_real()); tcp_call_bpf_2arg(sk, cb_flag, tstamp.tv_sec, tstamp.tv_nsec); } } @@ -5658,7 +5665,7 @@ void __skb_tstamp_tx(struct sk_buff *orig_skb, return; if (static_branch_unlikely(&bpf_tstamp_control)) - bpf_skb_tstamp_tx_output(sk, tstype); + bpf_skb_tstamp_tx_output(sk, tstype, hwtstamps); skb_tstamp_tx_output(orig_skb, ack_skb, hwtstamps, sk, tstype); } diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index d60675e1a5a0..020ec14ffae6 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -7023,6 +7023,11 @@ enum { * feature is on. It indicates the * recorded timestamp. */ + BPF_SOCK_OPS_TS_SW_OPT_CB, /* Called when skb is about to send + * to the nic when SO_TIMESTAMPING + * feature is on. It indicates the + * recorded timestamp. + */ }; /* List of TCP states. There is a build check in net/ipv4/tcp.c to detect From patchwork Sat Oct 12 04:06:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Xing X-Patchwork-Id: 13833349 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-pl1-f170.google.com (mail-pl1-f170.google.com [209.85.214.170]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2EBB4128369; Sat, 12 Oct 2024 04:07:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728706061; cv=none; b=nO2mNNHCGUhFvpmf43IqqIikJRxRQAZh6YDWGw6sztic106Wb2tdyReIvd3Eq4Dm3PqxGkj1U25kqtj+dp5S4e/jUg/CLbxgv+WYckG/ojl9qNBmGdekJAVC9NsNrRQsmy59+3202vhBjwjn/zydOAXCGFk9koGKu4X5Ok2GKVk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728706061; c=relaxed/simple; bh=S3HNPOpYMrfdrPsaUvWNqYtAGnxE1FPX3Yb8xP7Rtmk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=mV5YTFVwltLlLkOxLf/AA2YscaIioCrNNNKv55DlnS5+6Eubxsr6+cmVmyA3I8ZHUCDTz8cJXgdrRNJmzhA0oqzNnlR8WPyoQNwKlvmoP0rTWujGVTyEZK08YhmyPdicTMHh4ChZFT6mBMX1K6INo82Tt+Si6yO2syoI/7sd6Tc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=ByRWTZMv; arc=none smtp.client-ip=209.85.214.170 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="ByRWTZMv" Received: by mail-pl1-f170.google.com with SMTP id d9443c01a7336-20cb47387ceso6837255ad.1; Fri, 11 Oct 2024 21:07:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728706059; x=1729310859; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=4pagld+GkzvWpPPsbvZdzqsapkyB63T4pAkivFgLO5c=; b=ByRWTZMv6FjV3OyzGO/bmEpyg5EwqsHPNod1+qhqlz4HYTNtDrjR70bA02YAQWKOtJ 9N8Io59e4jcSnYKIF/Z1TblAoP5UVaUYqxGoF2mRsiB5qWOmeEV8a7cIetVHtwM1/5Og FhilfB1QtemcWlwio9UjJ4f1p6yTdhnfEC34KSbSzV4p8Op/lzRY6LIrqFsScB1XUPNM pfryP8IjL95U3fR0FM0fdNpeliIkdbp8dwWuo2N9FqzlbVzhfYLOLocUVOd9WlNz84dg hF8u499HKYXw13qQWhZlpR95j0t2zDfWLAnTR0bpeQ1H7bUTaD2xUWhgFnpMbIs3IRE4 J+zg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728706059; x=1729310859; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=4pagld+GkzvWpPPsbvZdzqsapkyB63T4pAkivFgLO5c=; b=HDIPEDPjQLxmyZWsMAjqLqvRjLdrr5KLz9qjfsB18xwYw3PBwXystr2KuHKXQZ88o8 YnLNt4XRbWwosjinrAV1xqwACLlhduQjzBGbSoLj64ynhxHaYvsVmPJMiggY+oMpRFvI dqOgTNaeNZMw7elxQfYD0d6cVmV0BCTcWtjN/3Qq/elX6b5B/DAKMCz2NITgEYvxv/rf 3r9qYBFRiAQK9WLgy/Le58jQsiV9foFxA9cBb3P2knBkUA5sl7QxS7SVoT5dNNCVHgWp yw19TcqdLeBZ1YWYiWjpRGiuZAlhu3wxzim3+yP4ARTCkev5fjWwVk9QYb1C1mJVKJfS 3BUA== X-Forwarded-Encrypted: i=1; AJvYcCXbntmgNbaBN1bxNgCqHq5szhyWFs1KoPOhnE8jVXY0S2+igkVnWQG5f2T5om6jlSN0Tb3m2sA=@vger.kernel.org X-Gm-Message-State: AOJu0YzfFqq+24nOHUyXdgPDdLWw3nLofsR6gOglKgnvT0dItQKg3ViL olCjpV676NjIEbBgaVmWkbzF8uLaUsg5u2EIasGZ/rXldF/FFGyy X-Google-Smtp-Source: AGHT+IE6G9YkwcWwiCDbFNSMEpF/g/AagIclmdqNFngCNkNu/juT+7dyMr3Zu4lCS276pacGBIxzXw== X-Received: by 2002:a17:902:ecc1:b0:20c:bffe:e1e5 with SMTP id d9443c01a7336-20cbffee323mr18675565ad.19.1728706059527; Fri, 11 Oct 2024 21:07:39 -0700 (PDT) Received: from KERNELXING-MB0.tencent.com ([43.132.141.25]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-20c8c21301dsm30939685ad.199.2024.10.11.21.07.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Oct 2024 21:07:39 -0700 (PDT) From: Jason Xing To: davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, dsahern@kernel.org, willemdebruijn.kernel@gmail.com, willemb@google.com, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com, jolsa@kernel.org Cc: bpf@vger.kernel.org, netdev@vger.kernel.org, Jason Xing Subject: [PATCH net-next v2 08/12] net-timestamp: introduce TS_ACK_OPT_CB to generate tcp acked timestamp Date: Sat, 12 Oct 2024 12:06:47 +0800 Message-Id: <20241012040651.95616-9-kerneljasonxing@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20241012040651.95616-1-kerneljasonxing@gmail.com> References: <20241012040651.95616-1-kerneljasonxing@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org From: Jason Xing When the last sent skb in each sendmsg() is acknowledged in TCP layer, we can print timestamp by setting BPF_SOCK_OPS_TS_ACK_OPT_CB in bpf program. Signed-off-by: Jason Xing --- include/uapi/linux/bpf.h | 5 +++++ net/core/skbuff.c | 3 +++ tools/include/uapi/linux/bpf.h | 5 +++++ 3 files changed, 13 insertions(+) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 0d00539f247a..1b478ec18ac2 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -7029,6 +7029,11 @@ enum { * feature is on. It indicates the * recorded timestamp. */ + BPF_SOCK_OPS_TS_ACK_OPT_CB, /* Called when all the skbs are + * acknowledged when SO_TIMESTAMPING + * feature is on. It indicates the + * recorded timestamp. + */ }; /* List of TCP states. There is a build check in net/ipv4/tcp.c to detect diff --git a/net/core/skbuff.c b/net/core/skbuff.c index 832d53de9874..e18305b03a01 100644 --- a/net/core/skbuff.c +++ b/net/core/skbuff.c @@ -5644,6 +5644,9 @@ static void bpf_skb_tstamp_tx_output(struct sock *sk, int tstype, case SCM_TSTAMP_SND: cb_flag = BPF_SOCK_OPS_TS_SW_OPT_CB; break; + case SCM_TSTAMP_ACK: + cb_flag = BPF_SOCK_OPS_TS_ACK_OPT_CB; + break; default: return; } diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index 020ec14ffae6..fc9b94de19f2 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -7028,6 +7028,11 @@ enum { * feature is on. It indicates the * recorded timestamp. */ + BPF_SOCK_OPS_TS_ACK_OPT_CB, /* Called when all the skbs are + * acknowledged when SO_TIMESTAMPING + * feature is on. It indicates the + * recorded timestamp. + */ }; /* List of TCP states. There is a build check in net/ipv4/tcp.c to detect From patchwork Sat Oct 12 04:06:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Xing X-Patchwork-Id: 13833350 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-pl1-f180.google.com (mail-pl1-f180.google.com [209.85.214.180]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3FC0512BF32; Sat, 12 Oct 2024 04:07:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728706066; cv=none; b=iwOh93CgqTBzU9fm1LgVG8Sj73YSTJ5cp1NsxWfj8ioHcut7AtDFgfh4HJUIDJKexjF49qXwyeo0g8QEvrK9Eocvpk8YiueFu4QnhiNyMkJKO/h6eKKb3MVGoXw89C56X5S58yfxUA2qAQGUrZR4ULF4b26/mk4NSjTEBq+7UPI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728706066; c=relaxed/simple; bh=kl0TitCeRlKaF4j98gdO50FA5YE6f5jA4tDxlmzCvKk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=uD9R1Rd1V/qhwnS1uYPrD8Tuf9Ejqa6k5hQIA/fGsDv4K8t7QG8Wh5qbsMhdsTBpPAMyCDRyEpvBoY+EMFdGL+8FJ4GBkXsWMP15E/T40PwrnDzrH+GwK8iUo7uUtpPo/FEmZydibwkxhrlgf/mSoGmidoJC3kCJxVEs7H3zX7c= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=eKNFEs8T; arc=none smtp.client-ip=209.85.214.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="eKNFEs8T" Received: by mail-pl1-f180.google.com with SMTP id d9443c01a7336-20bc506347dso21532665ad.0; Fri, 11 Oct 2024 21:07:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728706064; x=1729310864; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=44HcBJ9zqFTsxi7lWcBcBpv3nz8LXm+r/QFBaxzQLOg=; b=eKNFEs8TAb7s0pHGYVolyOG9vCGog+jjJhWNG1Mtf9ta9w3Ksu5/+ZSmKPHPXH+T+5 BuaLIPQgJWSp/v1ts4mumKCN4DO1i2E4JMMPDrQIevhpk54ycrY8duLi1tJI+Q+ZRONH 4hSnJ/0jwAHzOfCapGgCklnXKPWQ48X4eOex2HpgGwXOKf/tSX7viOc6I97L641b7tB1 p0beEiXKMmiibPaVnl6CI1J0wsj1jX7VyKhA5YVqtwNHfXrOfZkXhnNoV/gFdOqfWstq efdXVmK3/DmZSpnsXO4K3MkE7IWnHEHZWBQMyPAeFX315UB4fsx/NvSlkwC6BxjKfCN/ NgrQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728706064; x=1729310864; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=44HcBJ9zqFTsxi7lWcBcBpv3nz8LXm+r/QFBaxzQLOg=; b=rLVPqtnt1JtuzFzjAwy8IBLJh+Sw2mcS0bYhinruVqiuoLWLftANU+u7QjhmGo9QdX ueNnNcfMHkwqAxarFmniGUU6fHZF3aBF0GKyqASP8EtuEXREbAlOHFZUFEWxuAH2T5VI 8dAXBx+BqfC68i/DX3wIGYlsWBQK+CgcCCV1IcjDZEW6yEkRIBIT50WJH0CWj9SsjR1C vNNaXuTeNkFHN5ZU1xo5hP4weEkKBgDmXXdgDeiJdlrC96ZGbZFv1t4AOuBJWS4zqGha 7p4vvIlcwihd255fjrtGw/4OJXoowVTqaMJ8LEyEiRVMubJZIr9QpipxWj5CYOyLNpqr +QDA== X-Forwarded-Encrypted: i=1; AJvYcCUgFG/Bw0KUaANJ6HKuvOdCvwK4jQgVCkWBUCW4418krZB5E/3gFfvs9HFlewtWeYb9+7KdI3g=@vger.kernel.org X-Gm-Message-State: AOJu0Yz7svwa9tXORHMbzLbKT0V663RLYQDJOpvbAPaxLixIo0oiuogi u83rLF2pLsMvYHDyB9KqVC4pqJnCB6xxJA4NY+kwBZl4gVjNrWqN X-Google-Smtp-Source: AGHT+IG1rBVFLVHyXF9Ks1TKkJIlsW22bQG+83W/5r/WH8eH5CMaUxRYN+s2LJd0GcHnGlWkoDtOlA== X-Received: by 2002:a17:902:d501:b0:20b:c1e4:2d5d with SMTP id d9443c01a7336-20ca1677e53mr71579595ad.34.1728706064432; Fri, 11 Oct 2024 21:07:44 -0700 (PDT) Received: from KERNELXING-MB0.tencent.com ([43.132.141.25]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-20c8c21301dsm30939685ad.199.2024.10.11.21.07.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Oct 2024 21:07:44 -0700 (PDT) From: Jason Xing To: davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, dsahern@kernel.org, willemdebruijn.kernel@gmail.com, willemb@google.com, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com, jolsa@kernel.org Cc: bpf@vger.kernel.org, netdev@vger.kernel.org, Jason Xing Subject: [PATCH net-next v2 09/12] net-timestamp: add tx OPT_ID_TCP support for bpf case Date: Sat, 12 Oct 2024 12:06:48 +0800 Message-Id: <20241012040651.95616-10-kerneljasonxing@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20241012040651.95616-1-kerneljasonxing@gmail.com> References: <20241012040651.95616-1-kerneljasonxing@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org From: Jason Xing We can set OPT_ID|OPT_ID_TCP before we initialize the last skb from each sendmsg. We only set the socket once like how we use setsockopt() with OPT_ID|OPT_ID_TCP flags. Note: we will check if non-bpf _and_ bpf sk_tsflags have OPT_ID flag. If either of them has been set before, we will not initialize the key any more, or else it will affect the existing printing from applications or BPF program behaviour. Signed-off-by: Jason Xing --- include/net/sock.h | 1 + net/core/filter.c | 5 +++++ net/core/skbuff.c | 14 ++++++++++---- net/core/sock.c | 29 +++++++++++++++++++++-------- 4 files changed, 37 insertions(+), 12 deletions(-) diff --git a/include/net/sock.h b/include/net/sock.h index b7c51b95c92d..2b4ac289c8fa 100644 --- a/include/net/sock.h +++ b/include/net/sock.h @@ -2893,6 +2893,7 @@ DECLARE_STATIC_KEY_FALSE(bpf_tstamp_control); void sock_set_timestamp(struct sock *sk, int optname, bool valbool); int sock_get_timestamping(struct so_timestamping *timestamping, sockptr_t optval, unsigned int optlen); +int sock_set_tskey(struct sock *sk, int val, int type); int sock_set_timestamping(struct sock *sk, int optname, struct so_timestamping timestamping); diff --git a/net/core/filter.c b/net/core/filter.c index 08135f538c99..3b4afaa273d9 100644 --- a/net/core/filter.c +++ b/net/core/filter.c @@ -5210,6 +5210,7 @@ static int bpf_sock_set_timestamping(struct sock *sk, struct so_timestamping *timestamping) { u32 flags = timestamping->flags; + int ret; if (flags & ~SOF_TIMESTAMPING_MASK) return -EINVAL; @@ -5218,6 +5219,10 @@ static int bpf_sock_set_timestamping(struct sock *sk, SOF_TIMESTAMPING_TX_ACK))) return -EINVAL; + ret = sock_set_tskey(sk, flags, BPFPROG_TS_REQUESTOR); + if (ret) + return ret; + WRITE_ONCE(sk->sk_tsflags[BPFPROG_TS_REQUESTOR], flags); static_branch_enable(&bpf_tstamp_control); diff --git a/net/core/skbuff.c b/net/core/skbuff.c index e18305b03a01..1ef379a87f88 100644 --- a/net/core/skbuff.c +++ b/net/core/skbuff.c @@ -5619,7 +5619,7 @@ static void skb_tstamp_tx_output(struct sk_buff *orig_skb, __skb_complete_tx_timestamp(skb, sk, tstype, opt_stats); } -static void bpf_skb_tstamp_tx_output(struct sock *sk, int tstype, +static void bpf_skb_tstamp_tx_output(struct sock *sk, struct sk_buff *skb, int tstype, struct skb_shared_hwtstamps *hwtstamps) { struct tcp_sock *tp; @@ -5635,7 +5635,7 @@ static void bpf_skb_tstamp_tx_output(struct sock *sk, int tstype, tp = tcp_sk(sk); if (BPF_SOCK_OPS_TEST_FLAG(tp, BPF_SOCK_OPS_TX_TIMESTAMPING_OPT_CB_FLAG)) { struct timespec64 tstamp; - u32 cb_flag; + u32 cb_flag, key = 0; switch (tstype) { case SCM_TSTAMP_SCHED: @@ -5651,11 +5651,17 @@ static void bpf_skb_tstamp_tx_output(struct sock *sk, int tstype, return; } + if (sk_is_tcp(sk)) { + key = skb_shinfo(skb)->tskey; + key -= atomic_read(&sk->sk_tskey); + } + if (hwtstamps) tstamp = ktime_to_timespec64(hwtstamps->hwtstamp); else tstamp = ktime_to_timespec64(ktime_get_real()); - tcp_call_bpf_2arg(sk, cb_flag, tstamp.tv_sec, tstamp.tv_nsec); + + tcp_call_bpf_3arg(sk, cb_flag, key, tstamp.tv_sec, tstamp.tv_nsec); } } @@ -5668,7 +5674,7 @@ void __skb_tstamp_tx(struct sk_buff *orig_skb, return; if (static_branch_unlikely(&bpf_tstamp_control)) - bpf_skb_tstamp_tx_output(sk, tstype, hwtstamps); + bpf_skb_tstamp_tx_output(sk, orig_skb, tstype, hwtstamps); skb_tstamp_tx_output(orig_skb, ack_skb, hwtstamps, sk, tstype); } diff --git a/net/core/sock.c b/net/core/sock.c index a6e0d51a5f72..c15edbd382d5 100644 --- a/net/core/sock.c +++ b/net/core/sock.c @@ -915,21 +915,18 @@ int sock_get_timestamping(struct so_timestamping *timestamping, return 0; } -int sock_set_timestamping(struct sock *sk, int optname, - struct so_timestamping timestamping) +int sock_set_tskey(struct sock *sk, int val, int type) { - int val = timestamping.flags; - int ret; - - if (val & ~SOF_TIMESTAMPING_MASK) - return -EINVAL; + u32 tsflags; if (val & SOF_TIMESTAMPING_OPT_ID_TCP && !(val & SOF_TIMESTAMPING_OPT_ID)) return -EINVAL; + tsflags |= (sk->sk_tsflags[SOCKETOPT_TS_REQUESTOR] | + sk->sk_tsflags[BPFPROG_TS_REQUESTOR]); if (val & SOF_TIMESTAMPING_OPT_ID && - !(sk->sk_tsflags[SOCKETOPT_TS_REQUESTOR] & SOF_TIMESTAMPING_OPT_ID)) { + !(tsflags & SOF_TIMESTAMPING_OPT_ID)) { if (sk_is_tcp(sk)) { if ((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN)) @@ -943,6 +940,22 @@ int sock_set_timestamping(struct sock *sk, int optname, } } + return 0; +} + +int sock_set_timestamping(struct sock *sk, int optname, + struct so_timestamping timestamping) +{ + int val = timestamping.flags; + int ret; + + if (val & ~SOF_TIMESTAMPING_MASK) + return -EINVAL; + + ret = sock_set_tskey(sk, val, SOCKETOPT_TS_REQUESTOR); + if (ret) + return ret; + if (val & SOF_TIMESTAMPING_OPT_STATS && !(val & SOF_TIMESTAMPING_OPT_TSONLY)) return -EINVAL; From patchwork Sat Oct 12 04:06:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Xing X-Patchwork-Id: 13833351 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-pl1-f178.google.com (mail-pl1-f178.google.com [209.85.214.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D960B12C499; Sat, 12 Oct 2024 04:07:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728706071; cv=none; b=jZBhq1daAbahqFMqfolqMLqQIrsDZFVFxwSDXkOlVF+GKL8wwgSQqvnGpIawmgYNWKw8/VzzcbTRCObh7BYJnztVRz2CnOEyMC7BtjFHjwnlQLUNdZ1um9wll3UAfyjrwfVEwlFN9E+hQkFvCvm1o9EyHcaB2UOuyY7FHZo4Jqk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728706071; c=relaxed/simple; bh=AZLMFTRAR9NImMfyU1jqDkCBQJ6ODo1s57ciPrmyiAM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=rzcguVa5PdjvJWjjwZnFmLeNZ4mT3O16TyIN7oLLPGz63pGmest5J+q+KABpRNrt57bZNz2jn4iyWqI1rgll9ncJob8UGkIub0/2/ORoWe8YxIJ7ibZLUuo4OB3qGqRJj3E54aEzoXtOVtK6R/XKBwcVM+7AqtQu5TYhwM/+TzA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=a3mr5wJB; arc=none smtp.client-ip=209.85.214.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="a3mr5wJB" Received: by mail-pl1-f178.google.com with SMTP id d9443c01a7336-20c7ee8fe6bso20078135ad.2; Fri, 11 Oct 2024 21:07:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728706069; x=1729310869; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=HJyNH9xYi5bBFnTYrkHLA+DxeLz8FBF8B6RGVuO4Q5g=; b=a3mr5wJBPhfrzmmBPghKSO3OswE3EBv+T71IMMIBKKEO/ur6mFrDbUs80tvveQ65HT WVA4/6bfZvCGMzwaL5AT0ItBUdZM4MxMQ+0AoS24Ke0ZWqloNedjVSKToc2PRduMHVnU /CaevCcYKIJ4PtCPbrXaOyvhjuLn+YPpmDfSEsVT39rKgtPX+54rwMsuiGb9cJrKQ3D6 UoVImulKxipA1TEFkYV9cjfjfIJ986gI/IeZLQD7CCzT6SzPWn179iGDWL1M3lHGq7Ac lDijQwb5UHgOFa/EjoQ8QA421p/0j4lwdl9kqH1d1HOV8wQTe+R1cuP+ngrI5mA3pPVh bCLg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728706069; x=1729310869; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=HJyNH9xYi5bBFnTYrkHLA+DxeLz8FBF8B6RGVuO4Q5g=; b=RS0Ijkc6oRzIZkwEB18MN7YUfoWXj1ycZGziN449XfhD1hU9siuRUDVzCgR73+8E/J WyPTvvpNrVKNlS4W8dgzhM7mrtxHHb5QQ0pOz4YNpw2xCX7IkEuvapy6QLJqEoOC6k5E DqlkPCsvyngSedD07tcJp39TvpgcOxH7AIg5EkwwshDuYrxtp135BKKN/DPqWTyg0jU2 MRBwQZ/S3iI9NcHH5jOPzm78tRbotBgNr9IxzLo02/HOij89HhsmcKiXA3aqr5F9jHEh JA1bAwI8fpMONMUQKYmBQk92kTTvRgzKLoyTg1riJtAcgyNXdOCMKpPOQnFO6xCGxZ0G Ng3w== X-Forwarded-Encrypted: i=1; AJvYcCXA1QhcFPdF53pAUUtfZxG3hGz9GeIEQN5JbTA4IiQi1jofcjgaB5qeqGs1E83OB2IxI8tNKR0=@vger.kernel.org X-Gm-Message-State: AOJu0Yyy2YKTDC17eJ8hlgFGZR/1Huwe1K+aOOF6P/Rpxj1F79ZBSUGW zTAbUjf1U9GfHQsyXGqFOr/Y+pXoHuvF2ioiO4MZlIdO61miPGZ5 X-Google-Smtp-Source: AGHT+IE6vVdS+2Zrk2JyZ02RROZFZDrma6NMtYS2SXTh1gb6ZWRV8nVhPE1eX0AaU+aCjIqyQwOKiQ== X-Received: by 2002:a17:903:187:b0:20c:c880:c3a8 with SMTP id d9443c01a7336-20cc880c984mr2553725ad.50.1728706069207; Fri, 11 Oct 2024 21:07:49 -0700 (PDT) Received: from KERNELXING-MB0.tencent.com ([43.132.141.25]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-20c8c21301dsm30939685ad.199.2024.10.11.21.07.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Oct 2024 21:07:48 -0700 (PDT) From: Jason Xing To: davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, dsahern@kernel.org, willemdebruijn.kernel@gmail.com, willemb@google.com, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com, jolsa@kernel.org Cc: bpf@vger.kernel.org, netdev@vger.kernel.org, Jason Xing Subject: [PATCH net-next v2 10/12] net-timestamp: make bpf for tx timestamp work Date: Sat, 12 Oct 2024 12:06:49 +0800 Message-Id: <20241012040651.95616-11-kerneljasonxing@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20241012040651.95616-1-kerneljasonxing@gmail.com> References: <20241012040651.95616-1-kerneljasonxing@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org From: Jason Xing It's time to let bpf extension feature work. I extracted the part of logic from tcp_tx_timestamp() for bpf extension use, like TX timestamp flags. Signed-off-by: Jason Xing --- net/ipv4/tcp.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c index 6c8968eb4427..d37e231b2737 100644 --- a/net/ipv4/tcp.c +++ b/net/ipv4/tcp.c @@ -477,11 +477,31 @@ void tcp_init_sock(struct sock *sk) } EXPORT_SYMBOL(tcp_init_sock); +static void bpf_tx_timestamp(struct sock *sk, struct sk_buff *skb) +{ + u32 tsflags = READ_ONCE(sk->sk_tsflags[BPFPROG_TS_REQUESTOR]); + + if (tsflags && skb) { + struct skb_shared_info *shinfo = skb_shinfo(skb); + struct tcp_skb_cb *tcb = TCP_SKB_CB(skb); + + __sock_tx_timestamp(tsflags, &shinfo->tx_flags); + + if (tsflags & SOF_TIMESTAMPING_TX_ACK) + tcb->txstamp_ack = 1; + if (tsflags & SOF_TIMESTAMPING_TX_RECORD_MASK) + shinfo->tskey = TCP_SKB_CB(skb)->seq + skb->len - 1; + } +} + static void tcp_tx_timestamp(struct sock *sk, struct sockcm_cookie *sockc) { struct sk_buff *skb = tcp_write_queue_tail(sk); u32 tsflags = sockc->tsflags; + if (static_branch_unlikely(&bpf_tstamp_control)) + bpf_tx_timestamp(sk, skb); + if (tsflags && skb) { struct skb_shared_info *shinfo = skb_shinfo(skb); struct tcp_skb_cb *tcb = TCP_SKB_CB(skb); From patchwork Sat Oct 12 04:06:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Xing X-Patchwork-Id: 13833352 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-pl1-f182.google.com (mail-pl1-f182.google.com [209.85.214.182]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2397512C526; Sat, 12 Oct 2024 04:07:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728706076; cv=none; b=Qie5LB+SMI7KKGgBIQE2IE8yRv1GLupTBvYRPASF7j2sRr5mxrVd5a33BlGc7VM4WlZ3ekIRlDPW+MFO1hwjJ/DEgG18hOqYiUQ86pYel/LpkO2XSCufHvVxEgAk1x0Wq88fqTf3yzd0lpyEakXclJniPuFp5oD4ISYkPWnyUoY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728706076; c=relaxed/simple; bh=Ka2TUReOnfu7Vc5byOS3a3IpkLzeL0ivzs2vSdAKqQQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=r+zNpYtCsr5kZzisyNTFRjlae7seUB4SR+vH4+h2JWTHiJlBf+LGZZNaVtuDMp4xAFJ0L3rj7Mg5phpo0lgISQds8XDYP7ZX5htrwd2iAjd7kpp4v1oX+Ks6BQw+kZfaIiDWb2liTIL8LryfnCtv+XdVHkYNrGxZQNTolVGLh+g= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=kXIr2KH1; arc=none smtp.client-ip=209.85.214.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="kXIr2KH1" Received: by mail-pl1-f182.google.com with SMTP id d9443c01a7336-20ca1b6a80aso11021685ad.2; Fri, 11 Oct 2024 21:07:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728706074; x=1729310874; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=FzBpETB03ZUe/110lcAyhd5MMiyuu9Scyituu4R86q8=; b=kXIr2KH1rTZbC0vNgL8MYbTfF3PpXsjUpBQoaF0pk055rfn2EBqnwk8NIAI7wxEYxN xNOz8QAJQ1c978cf1GoSJdHEhumXNXnMBJHiXzU4nsBISQT2daE/mqtzbXLHoMN9BH+Q g9fcwjblbgWlIdNJwgLqqKkX5xsaIi3z4EEbBvcYmNFZqjFqtn7Q9SmUDEHQ8/xtPMQg R6hFR+lIWOlyYroeuov90U3L5txBnxnKH6620f2FRmqS3qXt1H4+G5/Xi5w4sm1P4Zlx GMMUJTJe4WtsWqUSCKBQ9oVfbEx4fBkv55gBhJsW0TJxL3I7wQrB6/rOfxVwANuBGb0P 66Ow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728706074; x=1729310874; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=FzBpETB03ZUe/110lcAyhd5MMiyuu9Scyituu4R86q8=; b=PSk1YD/u/4pEXt7ZDJfRTpxYxeHuxJo7viuPR7iU+wOZ8zc9yVKsAprYCjGDV+pc6i IaOmpzUcBaHnwxh+lZ+RjHDgKLxE2PAEnaF92DHZ28nWkqs8lK6o9VtPkaXMvD0hCIRG m2MAWprVDF7D/JWZB6hOUQbUCZLT2fy8r4nLULrSscurZenUTVWBm1FGR5I3ljc6ho41 Mia+p1VEjjP9u1mmN8RiP0Niz5FdD5b74AgsjiOHBAxFqgvPGdLY5nCjT4wMhlpBGgIW 28jZS2VLgh/7E0DqVt4RJcziBM0IGxPa8GKHVB9FdPHIgQd787hmSqL1Vg1t8cDUdpnH bv9Q== X-Forwarded-Encrypted: i=1; AJvYcCXev3ysUSsksLr08iVFgvZ1SSTac2CuJK9bdEvi5PnmwlgIkJhsXgYzJWSMQxgjhDvTFXahNlI=@vger.kernel.org X-Gm-Message-State: AOJu0YytzDrox1eRd3NYeVt8fsPdyeMiu1SSPdGA+cwte0/lElJ148aC 0QLGqTtUyBAxqFu1VDRQuAXnURFgxG5htAm9iyJAuqz4pj5q62wo X-Google-Smtp-Source: AGHT+IH18q3JtvyXGNgLAW2lweTyBJSfRvx/c6MUPbcvtqEaLXV6r7RwkT2v/QbZlXGmiblZHe8yiw== X-Received: by 2002:a17:903:41c2:b0:20b:920e:8fd3 with SMTP id d9443c01a7336-20cbb1c407emr26860265ad.35.1728706074424; Fri, 11 Oct 2024 21:07:54 -0700 (PDT) Received: from KERNELXING-MB0.tencent.com ([43.132.141.25]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-20c8c21301dsm30939685ad.199.2024.10.11.21.07.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Oct 2024 21:07:54 -0700 (PDT) From: Jason Xing To: davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, dsahern@kernel.org, willemdebruijn.kernel@gmail.com, willemb@google.com, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com, jolsa@kernel.org Cc: bpf@vger.kernel.org, netdev@vger.kernel.org, Jason Xing Subject: [PATCH net-next v2 11/12] net-timestamp: add bpf framework for rx timestamps Date: Sat, 12 Oct 2024 12:06:50 +0800 Message-Id: <20241012040651.95616-12-kerneljasonxing@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20241012040651.95616-1-kerneljasonxing@gmail.com> References: <20241012040651.95616-1-kerneljasonxing@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org From: Jason Xing Prepare for later changes in this series. Here I use u32 for bpf_sock_ops_cb_flags for better extension and introduce a new rx bpf flag to control separately. Main change is let userside set through bpf_setsockopt() for SO_TIMESTAMPING feature. Signed-off-by: Jason Xing --- include/linux/tcp.h | 2 +- include/net/tcp.h | 2 +- include/uapi/linux/bpf.h | 5 ++++- net/core/filter.c | 6 +++++- net/ipv4/tcp.c | 13 ++++++++++++- tools/include/uapi/linux/bpf.h | 5 ++++- 6 files changed, 27 insertions(+), 6 deletions(-) diff --git a/include/linux/tcp.h b/include/linux/tcp.h index 6a5e08b937b3..e21fd3035962 100644 --- a/include/linux/tcp.h +++ b/include/linux/tcp.h @@ -446,7 +446,7 @@ struct tcp_sock { /* Sock_ops bpf program related variables */ #ifdef CONFIG_BPF - u8 bpf_sock_ops_cb_flags; /* Control calling BPF programs + u32 bpf_sock_ops_cb_flags; /* Control calling BPF programs * values defined in uapi/linux/tcp.h */ u8 bpf_chg_cc_inprogress:1; /* In the middle of diff --git a/include/net/tcp.h b/include/net/tcp.h index 739a9fb83d0c..728db7107074 100644 --- a/include/net/tcp.h +++ b/include/net/tcp.h @@ -423,7 +423,7 @@ int tcp_set_rcvlowat(struct sock *sk, int val); int tcp_set_window_clamp(struct sock *sk, int val); void tcp_update_recv_tstamps(struct sk_buff *skb, struct scm_timestamping_internal *tss); -void tcp_recv_timestamp(struct msghdr *msg, const struct sock *sk, +void tcp_recv_timestamp(struct msghdr *msg, struct sock *sk, struct scm_timestamping_internal *tss); void tcp_data_ready(struct sock *sk); #ifdef CONFIG_MMU diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 1b478ec18ac2..d2754f155cf7 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -6903,8 +6903,11 @@ enum { /* Call bpf when the kernel is generating tx timestamps. */ BPF_SOCK_OPS_TX_TIMESTAMPING_OPT_CB_FLAG = (1<<7), + /* Call bpf when the kernel is generating rx timestamps. + */ + BPF_SOCK_OPS_RX_TIMESTAMPING_OPT_CB_FLAG = (1<<8), /* Mask of all currently supported cb flags */ - BPF_SOCK_OPS_ALL_CB_FLAGS = 0xFF, + BPF_SOCK_OPS_ALL_CB_FLAGS = 0x1FF, }; /* List of known BPF sock_ops operators. diff --git a/net/core/filter.c b/net/core/filter.c index 3b4afaa273d9..36b357b76f4a 100644 --- a/net/core/filter.c +++ b/net/core/filter.c @@ -5216,14 +5216,18 @@ static int bpf_sock_set_timestamping(struct sock *sk, return -EINVAL; if (!(flags & (SOF_TIMESTAMPING_TX_SCHED | SOF_TIMESTAMPING_TX_SOFTWARE | - SOF_TIMESTAMPING_TX_ACK))) + SOF_TIMESTAMPING_TX_ACK | SOF_TIMESTAMPING_RX_SOFTWARE))) return -EINVAL; ret = sock_set_tskey(sk, flags, BPFPROG_TS_REQUESTOR); if (ret) return ret; + if (flags & SOF_TIMESTAMPING_RX_SOFTWARE) + sock_enable_timestamp(sk, SOCK_TIMESTAMPING_RX_SOFTWARE); + WRITE_ONCE(sk->sk_tsflags[BPFPROG_TS_REQUESTOR], flags); + static_branch_enable(&bpf_tstamp_control); return 0; diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c index d37e231b2737..0891b41bc745 100644 --- a/net/ipv4/tcp.c +++ b/net/ipv4/tcp.c @@ -2260,14 +2260,25 @@ static int tcp_zerocopy_receive(struct sock *sk, } #endif +static void tcp_bpf_recv_timestamp(struct sock *sk, struct scm_timestamping_internal *tss) +{ + struct tcp_sock *tp = tcp_sk(sk); + + if (BPF_SOCK_OPS_TEST_FLAG(tp, BPF_SOCK_OPS_RX_TIMESTAMPING_OPT_CB_FLAG)) + return; +} + /* Similar to __sock_recv_timestamp, but does not require an skb */ -void tcp_recv_timestamp(struct msghdr *msg, const struct sock *sk, +void tcp_recv_timestamp(struct msghdr *msg, struct sock *sk, struct scm_timestamping_internal *tss) { int new_tstamp = sock_flag(sk, SOCK_TSTAMP_NEW); u32 tsflags = READ_ONCE(sk->sk_tsflags[SOCKETOPT_TS_REQUESTOR]); bool has_timestamping = false; + if (static_branch_unlikely(&bpf_tstamp_control)) + tcp_bpf_recv_timestamp(sk, tss); + if (tss->ts[0].tv_sec || tss->ts[0].tv_nsec) { if (sock_flag(sk, SOCK_RCVTSTAMP)) { if (sock_flag(sk, SOCK_RCVTSTAMPNS)) { diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index fc9b94de19f2..331e3e6f1ed5 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -6902,8 +6902,11 @@ enum { /* Call bpf when the kernel is generating tx timestamps. */ BPF_SOCK_OPS_TX_TIMESTAMPING_OPT_CB_FLAG = (1<<7), + /* Call bpf when the kernel is generating rx timestamps. + */ + BPF_SOCK_OPS_RX_TIMESTAMPING_OPT_CB_FLAG = (1<<8), /* Mask of all currently supported cb flags */ - BPF_SOCK_OPS_ALL_CB_FLAGS = 0xFF, + BPF_SOCK_OPS_ALL_CB_FLAGS = 0x1FF, }; /* List of known BPF sock_ops operators. From patchwork Sat Oct 12 04:06:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Xing X-Patchwork-Id: 13833353 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-pl1-f172.google.com (mail-pl1-f172.google.com [209.85.214.172]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1945314286; Sat, 12 Oct 2024 04:07:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728706081; cv=none; b=M8I2gSdhSXHp7xMnHJj639/zC8xJk1qyDOHhBRzC4t6Pzaf3Dwknj0SXucAu8fMcjwItE1SLJpn3XNtu7uRWmrM8Rttp1cVKltOZ+MkRVX4nY1FYI5FvwZ4hv0/OVwEMvWbZRDwRu1TnC5iggbtYLepNUPVwwWi7n48OsuZv1SI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728706081; c=relaxed/simple; bh=qIqMMsuuW7/dMCR/tUJLumUyy1VrwV9j6vcdlyn31bQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=POcfpd2e7bjtpqsjZCt/2XHUSeicyzUHfH7VS6g0O3veWd52nnD6Erjr9ULLwYWB+tTv4+ZXzA9Lu25Nqh/caq4OMWsoy/lvRFca9u4+5ZY8lKaV2OLBF0R0FFTAOUXT5r7TJEPKv+4AGWHHE2AyqRWByqFLSdcqXh9b3DNHuRo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=F/Ve4XRf; arc=none smtp.client-ip=209.85.214.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="F/Ve4XRf" Received: by mail-pl1-f172.google.com with SMTP id d9443c01a7336-20c7edf2872so21137945ad.1; Fri, 11 Oct 2024 21:07:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728706079; x=1729310879; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=dXSRG9m3FKtgekDc3MRYZh3dVuzXvgaCMmLFhQ0EFoU=; b=F/Ve4XRf/w2U1Pkk/xj3peo+H2dv0MAN+PCosrkvADEImPUj3kwdAdMsgq1dY9g9X3 vOvHEYaw0ue9u0H5W0R64HalgiN27yEAgefrtp+pV6r2yJTxprzaYQgyKJiYTMcwtt1Z +8Yj7hHbVUv7zySe+Gv19LPnpmDPt/ketrVHhRJTFeb9bYcGjzYjkzFYfNT0efFn6N+9 La+K50akxeX2FBe98K7aV0xmUTIAoGjF/cRfHgt8/igsfrmMo/weSTgAuTgbhREvt8kM Bgccc4p9f/FQj73qD5knaikcTxdYqRTzjKTeX2/Z4xN/sytzJYwOEvihV1xWFDkKF6MO sj+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728706079; x=1729310879; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=dXSRG9m3FKtgekDc3MRYZh3dVuzXvgaCMmLFhQ0EFoU=; b=Uez4UXCCx1QcURzM4qEyCu7Q5vmMrHgOfWLIooC0CnQIox/HsYjW8NuoZlBZNGabcI hwhtz+4v2QR7M6F25S+Pn13nE4q/dQpgk95JlNlzr4JdlwyHKuiYCFTKL0CugU8tFEja fCokevwtG9FYJMy27Zp/Pt0tXAnkfIYniw7/UehxQgO3kcH0q50hxWXBuefawYCvq7M/ Ge1pg+eoUfNG9DQizLhdE5Yh4w0DCM6dKZTcKyzXaXij1BkmSLL8YXorSJaKhZH+xTNO /VjLgwQYno4oGQ1wHB7AEnNsaAQQiZc65mviI5o/g/vnaoigl7vIAq92HYiA4Api3awy DukQ== X-Forwarded-Encrypted: i=1; AJvYcCWADXFcaztOSCBALKR+MQ5Pi6j4kwFrSuth9py0O9mTd9fsKdnOisLRquqlGKTZqLDrNKCyvhc=@vger.kernel.org X-Gm-Message-State: AOJu0YwHlDqcchEZdK7ysaCmrpjEGDZhwucIv1Ux2xvHrZJS4sHet8vr AJNm6g8/JA2rECTJtoKKBj5OIIiJi3K9KH60aEwttacyFpF6Sksn X-Google-Smtp-Source: AGHT+IH3pGZCRY3FoWyBeAj0q9jDDhE8OVwQdVVKndIpPw57a83KwNROIZl0TjUZFCqKELFxVBx/hw== X-Received: by 2002:a17:902:f60c:b0:20c:5a7b:dcb3 with SMTP id d9443c01a7336-20c8052b62bmr126649005ad.30.1728706079297; Fri, 11 Oct 2024 21:07:59 -0700 (PDT) Received: from KERNELXING-MB0.tencent.com ([43.132.141.25]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-20c8c21301dsm30939685ad.199.2024.10.11.21.07.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Oct 2024 21:07:59 -0700 (PDT) From: Jason Xing To: davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, dsahern@kernel.org, willemdebruijn.kernel@gmail.com, willemb@google.com, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com, jolsa@kernel.org Cc: bpf@vger.kernel.org, netdev@vger.kernel.org, Jason Xing Subject: [PATCH net-next v2 12/12] net-timestamp: add bpf support for rx software/hardware timestamp Date: Sat, 12 Oct 2024 12:06:51 +0800 Message-Id: <20241012040651.95616-13-kerneljasonxing@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20241012040651.95616-1-kerneljasonxing@gmail.com> References: <20241012040651.95616-1-kerneljasonxing@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org From: Jason Xing Now it's time to let the bpf for rx timestamp take effect. Signed-off-by: Jason Xing --- include/net/tcp.h | 14 ++++++++++++++ include/uapi/linux/bpf.h | 5 +++++ net/ipv4/tcp.c | 29 +++++++++++++++++++++++++++-- tools/include/uapi/linux/bpf.h | 5 +++++ 4 files changed, 51 insertions(+), 2 deletions(-) diff --git a/include/net/tcp.h b/include/net/tcp.h index 728db7107074..5a7893379ef7 100644 --- a/include/net/tcp.h +++ b/include/net/tcp.h @@ -2676,6 +2676,14 @@ static inline int tcp_call_bpf_3arg(struct sock *sk, int op, u32 arg1, u32 arg2, return tcp_call_bpf(sk, op, 3, args); } +static inline int tcp_call_bpf_4arg(struct sock *sk, int op, u32 arg1, u32 arg2, + u32 arg3, u32 arg4) +{ + u32 args[4] = {arg1, arg2, arg3, arg4}; + + return tcp_call_bpf(sk, op, 4, args); +} + #else static inline int tcp_call_bpf(struct sock *sk, int op, u32 nargs, u32 *args) { @@ -2693,6 +2701,12 @@ static inline int tcp_call_bpf_3arg(struct sock *sk, int op, u32 arg1, u32 arg2, return -EPERM; } +static inline int tcp_call_bpf_4arg(struct sock *sk, int op, u32 arg1, u32 arg2, + u32 arg3, u32 arg4) +{ + return -EPERM; +} + #endif static inline u32 tcp_timeout_init(struct sock *sk) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index d2754f155cf7..3527c20c8396 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -7037,6 +7037,11 @@ enum { * feature is on. It indicates the * recorded timestamp. */ + BPF_SOCK_OPS_TS_RX_OPT_CB, /* Called when tcp layer tries to + * receive skbs with timestamps when + * SO_TIMESTAMPING feature is on + * It indicates the recorded timestamp. + */ }; /* List of TCP states. There is a build check in net/ipv4/tcp.c to detect diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c index 0891b41bc745..14bc7283f574 100644 --- a/net/ipv4/tcp.c +++ b/net/ipv4/tcp.c @@ -2262,10 +2262,35 @@ static int tcp_zerocopy_receive(struct sock *sk, static void tcp_bpf_recv_timestamp(struct sock *sk, struct scm_timestamping_internal *tss) { + u32 tsflags = READ_ONCE(sk->sk_tsflags[BPFPROG_TS_REQUESTOR]); struct tcp_sock *tp = tcp_sk(sk); - if (BPF_SOCK_OPS_TEST_FLAG(tp, BPF_SOCK_OPS_RX_TIMESTAMPING_OPT_CB_FLAG)) - return; + if (BPF_SOCK_OPS_TEST_FLAG(tp, BPF_SOCK_OPS_RX_TIMESTAMPING_OPT_CB_FLAG)) { + u32 hw_sec, hw_nsec, sw_sec, sw_nsec; + + if (!(tsflags & (SOF_TIMESTAMPING_RX_SOFTWARE | + SOF_TIMESTAMPING_RX_HARDWARE))) + return; + + if (tsflags & SOF_TIMESTAMPING_RX_SOFTWARE) { + sw_sec = tss->ts[0].tv_sec; + sw_nsec = tss->ts[0].tv_nsec; + } else { + sw_sec = 0; + sw_nsec = 0; + } + + if (tsflags & SOF_TIMESTAMPING_RX_HARDWARE) { + hw_sec = tss->ts[2].tv_sec; + hw_nsec = tss->ts[2].tv_nsec; + } else { + hw_sec = 0; + hw_nsec = 0; + } + + tcp_call_bpf_4arg(sk, BPF_SOCK_OPS_TS_RX_OPT_CB, + sw_sec, sw_nsec, hw_sec, hw_nsec); + } } /* Similar to __sock_recv_timestamp, but does not require an skb */ diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index 331e3e6f1ed5..fad942abc36a 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -7036,6 +7036,11 @@ enum { * feature is on. It indicates the * recorded timestamp. */ + BPF_SOCK_OPS_TS_RX_OPT_CB, /* Called when tcp layer tries to + * receive skbs with timestamps when + * SO_TIMESTAMPING feature is on + * It indicates the recorded timestamp. + */ }; /* List of TCP states. There is a build check in net/ipv4/tcp.c to detect