From patchwork Mon Jul 3 17:50:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?J=C3=B6rn-Thorben_Hinz?= X-Patchwork-Id: 13300319 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1AA0FC30654 for ; Mon, 3 Jul 2023 17:53:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230106AbjGCRxN (ORCPT ); Mon, 3 Jul 2023 13:53:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56236 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229873AbjGCRxM (ORCPT ); Mon, 3 Jul 2023 13:53:12 -0400 Received: from mailrelay.tu-berlin.de (mailrelay.tu-berlin.de [130.149.7.70]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8A910E49; Mon, 3 Jul 2023 10:53:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tu-berlin.de; l=1385; s=dkim-tub; t=1688406789; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=gbuZxhAorkaMg20j4SIBabc/JGzysoTgj49OGqoJEp0=; b=eBMnRwQtN6kq+hxFoYjJAg5O3D95VMqLCLBZqJVUhoUltoglXN7iWOgh qPCFqLCOsPEQUYQVbJACmcReSRwNRL4edSwjUBbmOZ6OSrM/spVMPqN7B BWKYG85UVFXKTIZX351gWvnCuJo9fDrTHDSJoRh2tQH4RzIJy1q0kd+HR c=; X-IronPort-AV: E=Sophos;i="6.01,178,1684792800"; d="scan'208";a="1387329" Received: from postcard.tu-berlin.de (HELO mail.tu-berlin.de) ([141.23.12.142]) by mailrelay.tu-berlin.de with ESMTP; 03 Jul 2023 19:52:05 +0200 From: =?utf-8?q?J=C3=B6rn-Thorben_Hinz?= To: , , , CC: =?utf-8?q?J=C3=B6rn-Thorben_Hinz?= , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Shuah Khan , Willem de Bruijn , Deepa Dinamani Subject: [PATCH 1/2] net: Implement missing getsockopt(SO_TIMESTAMPING_NEW) Date: Mon, 3 Jul 2023 19:50:45 +0200 Message-ID: <20230703175048.151683-2-jthinz@mailbox.tu-berlin.de> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230703175048.151683-1-jthinz@mailbox.tu-berlin.de> References: <20230703175048.151683-1-jthinz@mailbox.tu-berlin.de> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org Commit 9718475e6908 ("socket: Add SO_TIMESTAMPING_NEW") added the new socket option SO_TIMESTAMPING_NEW. Setting the option is handled in sk_setsockopt(), querying it was not handled in sk_getsockopt(), though. For consistency, implement the missing getsockopt(SO_TIMESTAMPING_NEW). Similar as with SO_TIMESTAMP_{OLD,NEW}, the active timestamping flags are only returned when querying the same SO_TIMESTAMPING_{OLD,NEW} option they were set for. Empty flags are returned with SO_TIMESTAMPING_{NEW,OLD} otherwise. Fixes: 9718475e6908 ("socket: Add SO_TIMESTAMPING_NEW") Signed-off-by: Jörn-Thorben Hinz --- net/core/sock.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/net/core/sock.c b/net/core/sock.c index 9370fd50aa2c..cfb48244ed12 100644 --- a/net/core/sock.c +++ b/net/core/sock.c @@ -1710,9 +1710,14 @@ int sk_getsockopt(struct sock *sk, int level, int optname, break; case SO_TIMESTAMPING_OLD: + case SO_TIMESTAMPING_NEW: lv = sizeof(v.timestamping); - v.timestamping.flags = sk->sk_tsflags; - v.timestamping.bind_phc = sk->sk_bind_phc; + if (optname == (sock_flag(sk, SOCK_TSTAMP_NEW) ? + SO_TIMESTAMPING_NEW : + SO_TIMESTAMPING_OLD)) { + v.timestamping.flags = sk->sk_tsflags; + v.timestamping.bind_phc = sk->sk_bind_phc; + } break; case SO_RCVTIMEO_OLD: From patchwork Mon Jul 3 17:50:46 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?J=C3=B6rn-Thorben_Hinz?= X-Patchwork-Id: 13300320 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A9BFDC001B3 for ; Mon, 3 Jul 2023 17:53:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230359AbjGCRxY (ORCPT ); Mon, 3 Jul 2023 13:53:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56452 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230273AbjGCRxW (ORCPT ); Mon, 3 Jul 2023 13:53:22 -0400 Received: from mailrelay.tu-berlin.de (mailrelay.tu-berlin.de [130.149.7.70]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2A279C2; Mon, 3 Jul 2023 10:53:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tu-berlin.de; l=4091; s=dkim-tub; t=1688406797; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=sVFj5XSEpvbTR84sGLSbUV0+aK9N+w+4xBjQ6fw0+Us=; b=Y4sY+8LHxjCriKGa+njKADQCwaP9QMILCz1d447SBA18w8YGm0VrVr8v 8ZrexQXgNt/ZnTTSgHr3tyslGNXqX4efiv7TfWViv8fxwQ3lhcv04ZxgC FPdIKPIuvvJUJIZhzBeuGu/UI1G5R75llIxAe+fpLPFYCcK13bjM16L+q 0=; X-IronPort-AV: E=Sophos;i="6.01,178,1684792800"; d="scan'208";a="1320359" Received: from bulkmail.tu-berlin.de (HELO mail.tu-berlin.de) ([141.23.12.143]) by mailrelay.tu-berlin.de with ESMTP; 03 Jul 2023 19:52:08 +0200 From: =?utf-8?q?J=C3=B6rn-Thorben_Hinz?= To: , , , CC: =?utf-8?q?J=C3=B6rn-Thorben_Hinz?= , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Shuah Khan , Deepa Dinamani , Willem de Bruijn Subject: [PATCH 2/2] bpf: Allow setting SO_TIMESTAMPING* with bpf_setsockopt() Date: Mon, 3 Jul 2023 19:50:46 +0200 Message-ID: <20230703175048.151683-3-jthinz@mailbox.tu-berlin.de> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230703175048.151683-1-jthinz@mailbox.tu-berlin.de> References: <20230703175048.151683-1-jthinz@mailbox.tu-berlin.de> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org A BPF application, e.g., a TCP congestion control, might benefit from or even require precise (=hardware) packet timestamps. These timestamps are already available in __sk_buff.hwtstamp and bpf_sock_ops.skb_hwtstamp, but could not be requested: A BPF program was not allowed to set SO_TIMESTAMPING* on a socket. Enable BPF programs to actively request the generation of timestamps from a stream socket. The also required ioctl(SIOCSHWTSTAMP) on the network device must still be done separately, in user space. Signed-off-by: Jörn-Thorben Hinz --- include/uapi/linux/bpf.h | 3 ++- net/core/filter.c | 2 ++ tools/include/uapi/linux/bpf.h | 3 ++- tools/testing/selftests/bpf/progs/bpf_tracing_net.h | 2 ++ tools/testing/selftests/bpf/progs/setget_sockopt.c | 4 ++++ 5 files changed, 12 insertions(+), 2 deletions(-) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 60a9d59beeab..3e64b8137931 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -2640,7 +2640,8 @@ union bpf_attr { * **SO_RCVBUF**, **SO_SNDBUF**, **SO_MAX_PACING_RATE**, * **SO_PRIORITY**, **SO_RCVLOWAT**, **SO_MARK**, * **SO_BINDTODEVICE**, **SO_KEEPALIVE**, **SO_REUSEADDR**, - * **SO_REUSEPORT**, **SO_BINDTOIFINDEX**, **SO_TXREHASH**. + * **SO_REUSEPORT**, **SO_BINDTOIFINDEX**, **SO_TXREHASH**, + * **SO_TIMESTAMPING_NEW**, **SO_TIMESTAMPING_OLD**. * * **IPPROTO_TCP**, which supports the following *optname*\ s: * **TCP_CONGESTION**, **TCP_BPF_IW**, * **TCP_BPF_SNDCWND_CLAMP**, **TCP_SAVE_SYN**, diff --git a/net/core/filter.c b/net/core/filter.c index 06ba0e56e369..af0f3a6762de 100644 --- a/net/core/filter.c +++ b/net/core/filter.c @@ -5108,6 +5108,8 @@ static int sol_socket_sockopt(struct sock *sk, int optname, case SO_MAX_PACING_RATE: case SO_BINDTOIFINDEX: case SO_TXREHASH: + case SO_TIMESTAMPING_NEW: + case SO_TIMESTAMPING_OLD: if (*optlen != sizeof(int)) return -EINVAL; break; diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index 60a9d59beeab..3e64b8137931 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -2640,7 +2640,8 @@ union bpf_attr { * **SO_RCVBUF**, **SO_SNDBUF**, **SO_MAX_PACING_RATE**, * **SO_PRIORITY**, **SO_RCVLOWAT**, **SO_MARK**, * **SO_BINDTODEVICE**, **SO_KEEPALIVE**, **SO_REUSEADDR**, - * **SO_REUSEPORT**, **SO_BINDTOIFINDEX**, **SO_TXREHASH**. + * **SO_REUSEPORT**, **SO_BINDTOIFINDEX**, **SO_TXREHASH**, + * **SO_TIMESTAMPING_NEW**, **SO_TIMESTAMPING_OLD**. * * **IPPROTO_TCP**, which supports the following *optname*\ s: * **TCP_CONGESTION**, **TCP_BPF_IW**, * **TCP_BPF_SNDCWND_CLAMP**, **TCP_SAVE_SYN**, diff --git a/tools/testing/selftests/bpf/progs/bpf_tracing_net.h b/tools/testing/selftests/bpf/progs/bpf_tracing_net.h index cfed4df490f3..8d526db8ceeb 100644 --- a/tools/testing/selftests/bpf/progs/bpf_tracing_net.h +++ b/tools/testing/selftests/bpf/progs/bpf_tracing_net.h @@ -15,8 +15,10 @@ #define SO_RCVLOWAT 18 #define SO_BINDTODEVICE 25 #define SO_MARK 36 +#define SO_TIMESTAMPING_OLD 37 #define SO_MAX_PACING_RATE 47 #define SO_BINDTOIFINDEX 62 +#define SO_TIMESTAMPING_NEW 65 #define SO_TXREHASH 74 #define __SO_ACCEPTCON (1 << 16) diff --git a/tools/testing/selftests/bpf/progs/setget_sockopt.c b/tools/testing/selftests/bpf/progs/setget_sockopt.c index 7a438600ae98..54205d10793c 100644 --- a/tools/testing/selftests/bpf/progs/setget_sockopt.c +++ b/tools/testing/selftests/bpf/progs/setget_sockopt.c @@ -48,6 +48,10 @@ static const struct sockopt_test sol_socket_tests[] = { { .opt = SO_MARK, .new = 0xeb9f, .expected = 0xeb9f, }, { .opt = SO_MAX_PACING_RATE, .new = 0xeb9f, .expected = 0xeb9f, }, { .opt = SO_TXREHASH, .flip = 1, }, + { .opt = SO_TIMESTAMPING_NEW, .new = SOF_TIMESTAMPING_RX_HARDWARE, + .expected = SOF_TIMESTAMPING_RX_HARDWARE, }, + { .opt = SO_TIMESTAMPING_OLD, .new = SOF_TIMESTAMPING_RX_HARDWARE, + .expected = SOF_TIMESTAMPING_RX_HARDWARE, }, { .opt = 0, }, };