From patchwork Sun Jan 12 11:37:34 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Xing X-Patchwork-Id: 13936313 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-pl1-f176.google.com (mail-pl1-f176.google.com [209.85.214.176]) (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 CE20E1B0414; Sun, 12 Jan 2025 11:38:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.176 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736681886; cv=none; b=dkDw8DfFt4BQ3usHdjBYFP5rS4L8m968RiY/dUbOIX6qE6sddnAcWHkGQOv82lUBvKTFbXPrOGYLSStupCObwc++ZsrirUfxpAdxXW9yA5NZLg3VuExHvn6FSm6lYaLXmgtZ1CWshqkO9ynoVkYc9uRJQ0fiye3Q2RfQGG06I18= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736681886; c=relaxed/simple; bh=RRpZglcOlW8NN6fN7Q6AK8rJjj/4vJD+11jGR8UY25U=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=WSK23m7EzjynVMJLoVOd1zUpnF6eOGWh29TsGwYDwLDqBiwxdyNpioxTlJ39wTmfTd/ZPldthqfzwb7xGwNmdoxd4uMmTxR9vOHt/EyB3wgVgjwuHDmNq1X2adpJUP+QomfshRBV0fQfVAoW7WYjuJMBwbOtukrjY4uWhNRrB/E= 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=I3aNfVeQ; arc=none smtp.client-ip=209.85.214.176 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="I3aNfVeQ" Received: by mail-pl1-f176.google.com with SMTP id d9443c01a7336-2163bd70069so60247125ad.0; Sun, 12 Jan 2025 03:38:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736681883; x=1737286683; 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=XMU+F6/bCdP+GeYqX6gdZ9HGbY39WxXn7I6MkDtModU=; b=I3aNfVeQwlKgjXrYH0EkOvifp1aHHq/lUcIpjyBbHWSYdKhuBf37L95UuPXSPBogBm xWgL2ew/QQg5RPP0MgBIs3a9lgnnj9uwPax8jIM1sppWNhAGIxlDm5bWHKEaQVTbL7Kr sgk/4/yM5DSO2dcYJW9JlhWEMLGiNejdPQlzcxHl0v/Pjy+Ie7hvfOofPdsBQSbkfD9z RV7dB4GLhq84vpYxNOeyy1RW0jJneSEk0db5d3p0+6xuby/Trz4QN0yUKkz8eLBiy+fv 6v+pUlBK4s8YfB989O6nt1I+NvkCJrK33iA1/888gBJK6Og4CbOkh4W2tss4pYXquVfo j08g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736681883; x=1737286683; 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=XMU+F6/bCdP+GeYqX6gdZ9HGbY39WxXn7I6MkDtModU=; b=tHzssgIIhWyXan36D9gAG46zc1SmvuJiz8iCZR3t+dzdKXAd0/HJHCRJtN8ChNq5dp xgNETg3JxY8E20IrvC96/XOziJ6DCKnf8MxEuuJo8LpQ1Gd5/VkNLK2AXme+a8llvsPK sz4PzX7Ysm72/vFJyRCMhDhy85uYUCbLZ2DYhj6p4BmXaD/hfYtAj2dqM0mzjqASXxZR lBL5yeB5u/u8nv4IIjJbqq4b3mIrsTt3OC5wqgL1+6rBuQb1OJMwIFBp9COwYukVpSfz RoqG0E2jA1cjcdiaeb6Q91iQSHLu73MAPBpR7USTcDRmz5isNd+PStwBJrRfiS52BgqM Ayiw== X-Forwarded-Encrypted: i=1; AJvYcCX72SsPWEk7adPPTBphZFxxisM/ZmfZ9CSqAXZNv7iZ1uPyMaMSCkctn2MX+mp4wwl7HCUa9Og=@vger.kernel.org X-Gm-Message-State: AOJu0Yy/coFDTVHS8HQEQoEjQfrkwiRlmci//Uh38z9lFdl072tAD1my D+8C+LpNxClUGCYqQn+wsF/zlwpwaw6hokN/wPzD3DcB2EWpFUYw X-Gm-Gg: ASbGncsHRQEE25If8OaDHRLOrrMFkdVuwk60oRePhXvEvjf5lX4rc03IVTciHAlEA0E E3XgD8Ust3JFIAOUBa4Hj+r9d95Z0/Wz50Pkx18jmyO0AG2ij9fCc9E2mrR5LIgYv2jFgdgjMzf M/84+f6s6cvIsOTi/fZcvC3juP5vbITD6ODWqTKam9EQIg69Sg9vk6FfKQ0fiovb7n7Lf3uMyOo fNgVFfm8GM0nTXAOyAAJzHjo9uedG2wNR8jvvp/HlZXEEuZh9bdmsGWXeLQFIc4kgcAHnwjGTdt 38cTzJuVJXDOMzMnT7U= X-Google-Smtp-Source: AGHT+IFnzAJjZgFipwRCRhTT6utuLtxBPG8K/7SVXAy6NfTeh1S9HsDsUneBzyn3VhSrJJNdzvo31w== X-Received: by 2002:a17:902:ec83:b0:215:e98c:c5d9 with SMTP id d9443c01a7336-21a83f57012mr195677035ad.18.1736681882977; Sun, 12 Jan 2025 03:38:02 -0800 (PST) Received: from KERNELXING-MC1.tencent.com ([111.201.29.174]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21a9f253a98sm37353765ad.224.2025.01.12.03.37.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 12 Jan 2025 03:38:02 -0800 (PST) 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, horms@kernel.org Cc: bpf@vger.kernel.org, netdev@vger.kernel.org, Jason Xing Subject: [PATCH net-next v5 01/15] net-timestamp: add support for bpf_setsockopt() Date: Sun, 12 Jan 2025 19:37:34 +0800 Message-Id: <20250112113748.73504-2-kerneljasonxing@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20250112113748.73504-1-kerneljasonxing@gmail.com> References: <20250112113748.73504-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 Users can write the following code to enable the bpf extension: bpf_setsockopt(skops, SOL_SOCKET, SK_BPF_CB_FLAGS, &flags, sizeof(flags)); Signed-off-by: Jason Xing --- include/net/sock.h | 7 +++++++ include/uapi/linux/bpf.h | 8 ++++++++ net/core/filter.c | 25 +++++++++++++++++++++++++ tools/include/uapi/linux/bpf.h | 1 + 4 files changed, 41 insertions(+) diff --git a/include/net/sock.h b/include/net/sock.h index ccf86c8a7a8a..f5447b4b78fd 100644 --- a/include/net/sock.h +++ b/include/net/sock.h @@ -303,6 +303,7 @@ struct sk_filter; * @sk_stamp: time stamp of last packet received * @sk_stamp_seq: lock for accessing sk_stamp on 32 bit architectures only * @sk_tsflags: SO_TIMESTAMPING flags + * @sk_bpf_cb_flags: used for bpf_setsockopt * @sk_use_task_frag: allow sk_page_frag() to use current->task_frag. * Sockets that can be used under memory reclaim should * set this to false. @@ -445,6 +446,12 @@ struct sock { u32 sk_reserved_mem; int sk_forward_alloc; u32 sk_tsflags; +#ifdef CONFIG_BPF_SYSCALL +#define SK_BPF_CB_FLAG_TEST(SK, FLAG) ((SK)->sk_bpf_cb_flags & (FLAG)) + u32 sk_bpf_cb_flags; +#else +#define SK_BPF_CB_FLAG_TEST(SK, FLAG) 0 +#endif __cacheline_group_end(sock_write_rxtx); __cacheline_group_begin(sock_write_tx); diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 4162afc6b5d0..e629e09b0b31 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -6903,6 +6903,13 @@ enum { BPF_SOCK_OPS_ALL_CB_FLAGS = 0x7F, }; +/* Definitions for bpf_sk_cb_flags */ +enum { + SK_BPF_CB_TX_TIMESTAMPING = 1<<0, + SK_BPF_CB_MASK = (SK_BPF_CB_TX_TIMESTAMPING - 1) | + SK_BPF_CB_TX_TIMESTAMPING +}; + /* List of known BPF sock_ops operators. * New entries can only be added at the end */ @@ -7081,6 +7088,7 @@ enum { TCP_BPF_SYN_IP = 1006, /* Copy the IP[46] and TCP header */ TCP_BPF_SYN_MAC = 1007, /* Copy the MAC, IP[46], and TCP header */ TCP_BPF_SOCK_OPS_CB_FLAGS = 1008, /* Get or Set TCP sock ops flags */ + SK_BPF_CB_FLAGS = 1009, /* Used to set socket bpf flags */ }; enum { diff --git a/net/core/filter.c b/net/core/filter.c index b957cf57299e..c6dd2d2e44c8 100644 --- a/net/core/filter.c +++ b/net/core/filter.c @@ -5222,6 +5222,23 @@ static const struct bpf_func_proto bpf_get_socket_uid_proto = { .arg1_type = ARG_PTR_TO_CTX, }; +static int sk_bpf_set_cb_flags(struct sock *sk, char *optval, bool getopt) +{ + u32 sk_bpf_cb_flags; + + if (getopt) + return -EINVAL; + + sk_bpf_cb_flags = *(u32 *)optval; + + if (sk_bpf_cb_flags & ~SK_BPF_CB_MASK) + return -EINVAL; + + sk->sk_bpf_cb_flags = sk_bpf_cb_flags; + + return 0; +} + static int sol_socket_sockopt(struct sock *sk, int optname, char *optval, int *optlen, bool getopt) @@ -5238,6 +5255,7 @@ static int sol_socket_sockopt(struct sock *sk, int optname, case SO_MAX_PACING_RATE: case SO_BINDTOIFINDEX: case SO_TXREHASH: + case SK_BPF_CB_FLAGS: if (*optlen != sizeof(int)) return -EINVAL; break; @@ -5247,6 +5265,13 @@ static int sol_socket_sockopt(struct sock *sk, int optname, return -EINVAL; } + if (optname == SK_BPF_CB_FLAGS) +#ifdef CONFIG_BPF_SYSCALL + return sk_bpf_set_cb_flags(sk, optval, getopt); +#else + return -EINVAL; +#endif + if (getopt) { if (optname == SO_BINDTODEVICE) return -EINVAL; diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index 4162afc6b5d0..6b0a5b787b12 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -7081,6 +7081,7 @@ enum { TCP_BPF_SYN_IP = 1006, /* Copy the IP[46] and TCP header */ TCP_BPF_SYN_MAC = 1007, /* Copy the MAC, IP[46], and TCP header */ TCP_BPF_SOCK_OPS_CB_FLAGS = 1008, /* Get or Set TCP sock ops flags */ + SK_BPF_CB_FLAGS = 1009, /* Used to set socket bpf flags */ }; enum { From patchwork Sun Jan 12 11:37:35 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Xing X-Patchwork-Id: 13936314 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 DFE901ABED7; Sun, 12 Jan 2025 11:38:08 +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=1736681890; cv=none; b=qomCeKFVIhfEOLIMmzDsG9Xh0o1MwVgliTlwzd+y1iCblq0Q9IdhnjyjjlSAKN8c6sQT9yqf4Qd2nG3mr1eGITJG1EkXD49AKjvXW9wRzb4bycoKkuQb3SGLEUvnuOpC49BxDgMWPG4E4uj1sE+ViEw6P+YN1+PWpfde7qcbKTI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736681890; c=relaxed/simple; bh=2E1HF5NoSftlEAMfDZsNHqTJ/VktwpLAYvS4z1o1KuI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=P+jJxxA3VGTkai/EVsbjFEDNTwyoJsN9Y8NEBEMBk0YeuKZT5qOybytjJnAYs4Zr4GN4dXCLvmQ6QvtydgeDqLZUZUma2YD0hY8YCQEEQ1MZHAgYq0jnR0uXkOjWHRO5Wre24HTPXFKx0aTxrGsEee3wwo5+8JxMrVCYjRaRgIA= 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=lihdHBJ0; 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="lihdHBJ0" Received: by mail-pl1-f170.google.com with SMTP id d9443c01a7336-2161eb95317so57556055ad.1; Sun, 12 Jan 2025 03:38:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736681888; x=1737286688; 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=qjV/MKlqBN/wrwWo9g3ip9U4q0fvIKgRwsUvfhkt65w=; b=lihdHBJ0SekYR4pRUkMjS9tkiBb27zfEUs4MgwIb6GH5orMMbtVK3aoq0EeGnwrN5z mMFvsBxlA43aGwvAuBGNwiuZRoYmtgWnk2SxO8LQxU8JztL3bi6Swe5ZjywNtunIPfoS W7H3VWBuxKwXw7mtVH0OdLT5Y7Om+OAu8iuzYmG7VfcSa8dBkyNhBerI/6rKKVeAmTv+ naim1rtSvFY55qsis+NbDlwEXwOS8cAgY9sIvHhmuMXFjK7+RhZCRK3oLaixfCs6e5Bd Yp+xK4neu3xwTlOeRRqAYWUVpDwAetqZ+XGrNpQH8d6+YOkM+J1DHDixjT9kjXZjgbzI iikw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736681888; x=1737286688; 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=qjV/MKlqBN/wrwWo9g3ip9U4q0fvIKgRwsUvfhkt65w=; b=PphnIrexEApx1eoOt9qdrWGJiWLhpTBKrocIeIv8Cg8F4c3aqYqiaP5r/+KqByLycy cCkRk+WGBl+BfmCrvC/H6Vcn/eSxxmRcWEkEA8hKdJZvbuXIQTp1HQuvFy/aI7TuqpwE ii0vnZvfQwN/G3vcejjJciOgBq7MffhsGc4XjlzZXwdsS+v+Kazs9bsHxRZPaqsmn2sn VnDkYczh88dKeWHBsEm3tgpjyJqasdlNARrU77i7d9V6QT64kwqRL0j9Dgp9q1bg8Ujm v+S2Wd+fONFn5xTF0R9Sfzd6nXeqcAOmG6AAq/GsiHHheLbQN8WPJq4xBibXsz1yM/Kc F3TA== X-Forwarded-Encrypted: i=1; AJvYcCWapjYaed2kvVzUXQGexrK+NKTZRknCznUqA0AzETRwBy3LdKEURPDyyW8veHbXjnbtGDQjKjE=@vger.kernel.org X-Gm-Message-State: AOJu0Yw7oKuB6fC1Fqrp8Qw4aF3V5IDqQKjNFKRqExfxD0u27zhoNSzo fziXhQdWFzQM0uLuhh61FqLT/5gJiH3A3eBLg6eUQp7pakU18UrjHLbLs2J8 X-Gm-Gg: ASbGncu8vCsVMgP8hDXKxP4i5LqP0XgN8e2ytkgpS9qU6V4Vum+2ayrDZbjhoc3p/7Q EsfyEWfVzOoLxG5a3SHvX4zl5adr/AjMcaxH2D1NArEWPmEShrVbR5Kxxn+qTdY53CHqlDWruIu D205Q4PmupWwKNWtTX54rKt4IbLQoIyjKxUoQ/XdUPvP6Q+RdFi/shsYPJp3xBa1kwb/cZzoFPa Ish9z27q1Ze3OLOegtX4njbeqr8U99PSGDLqzTeoH5549TA9xALQcFPjuz5wAe4XkVWGyVcBmfa 6IOsO47kCs/94ki+8Wc= X-Google-Smtp-Source: AGHT+IEkaO35tFCtA7EY3tdzwp4B4RMJfH6AKYAwan1RTf1DX1SiDrGukRkmbCkd3f40hNsVb3SHdA== X-Received: by 2002:a17:903:178f:b0:216:84e9:d334 with SMTP id d9443c01a7336-21a83f767e2mr229727025ad.33.1736681888191; Sun, 12 Jan 2025 03:38:08 -0800 (PST) Received: from KERNELXING-MC1.tencent.com ([111.201.29.174]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21a9f253a98sm37353765ad.224.2025.01.12.03.38.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 12 Jan 2025 03:38:07 -0800 (PST) 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, horms@kernel.org Cc: bpf@vger.kernel.org, netdev@vger.kernel.org, Jason Xing Subject: [PATCH net-next v5 02/15] net-timestamp: prepare for bpf prog use Date: Sun, 12 Jan 2025 19:37:35 +0800 Message-Id: <20250112113748.73504-3-kerneljasonxing@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20250112113748.73504-1-kerneljasonxing@gmail.com> References: <20250112113748.73504-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 Later, I would introduce three points to report some information to user space based on this. Signed-off-by: Jason Xing --- include/net/sock.h | 7 +++++++ net/core/sock.c | 14 ++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/include/net/sock.h b/include/net/sock.h index f5447b4b78fd..dd874e8337c0 100644 --- a/include/net/sock.h +++ b/include/net/sock.h @@ -2930,6 +2930,13 @@ int sock_set_timestamping(struct sock *sk, int optname, struct so_timestamping timestamping); void sock_enable_timestamps(struct sock *sk); +#if defined(CONFIG_CGROUP_BPF) && defined(CONFIG_BPF_SYSCALL) +void bpf_skops_tx_timestamping(struct sock *sk, struct sk_buff *skb, int op); +#else +static inline void bpf_skops_tx_timestamping(struct sock *sk, struct sk_buff *skb, int op) +{ +} +#endif void sock_no_linger(struct sock *sk); void sock_set_keepalive(struct sock *sk); void sock_set_priority(struct sock *sk, u32 priority); diff --git a/net/core/sock.c b/net/core/sock.c index eae2ae70a2e0..e06bcafb1b2d 100644 --- a/net/core/sock.c +++ b/net/core/sock.c @@ -948,6 +948,20 @@ int sock_set_timestamping(struct sock *sk, int optname, return 0; } +#if defined(CONFIG_CGROUP_BPF) && defined(CONFIG_BPF_SYSCALL) +void bpf_skops_tx_timestamping(struct sock *sk, struct sk_buff *skb, int op) +{ + struct bpf_sock_ops_kern sock_ops; + + memset(&sock_ops, 0, offsetof(struct bpf_sock_ops_kern, temp)); + sock_ops.op = op; + if (sk_is_tcp(sk) && sk_fullsock(sk)) + sock_ops.is_fullsock = 1; + sock_ops.sk = sk; + __cgroup_bpf_run_filter_sock_ops(sk, &sock_ops, CGROUP_SOCK_OPS); +} +#endif + void sock_set_keepalive(struct sock *sk) { lock_sock(sk); From patchwork Sun Jan 12 11:37:36 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Xing X-Patchwork-Id: 13936315 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-pl1-f175.google.com (mail-pl1-f175.google.com [209.85.214.175]) (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 7A3D61ABED7; Sun, 12 Jan 2025 11:38:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736681895; cv=none; b=WbyU2jQ8qN2oluJLb4nneE9e464Oi/CiKea7LQyV0VOPWeMPOJQRf34LjEE2xI63gsnVVZ1Lka2ykY/AK/dl3IZQvw0GwvLOcvh2vT5DAhPsAlLL7X2C8f5J9j9Ad2iv/sXzYSnzR8+RgORYz2IomLNwbSZxDEIfGCXx/sDMUYE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736681895; c=relaxed/simple; bh=Hu6sj4YlNIIq1o19KtqVt8sq2Hf+2qUtX/5DmW9NxHE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=WMKtpMfgJwGXuOCqZAcI3kLv2xbALar6Xdc7AU6v+53BamR1kTkO/3gccFq7B6gqd3GXW5aCNB1UJPVIZn7GBiLTNh3x8EYjRxXXVdRtt3aaanA1Hk5N/23nX3OCLeJrBowXqTaneB80O33H1Blzu2jYQtmd607FKhGwHsnlMBc= 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=GA+7W1f5; arc=none smtp.client-ip=209.85.214.175 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="GA+7W1f5" Received: by mail-pl1-f175.google.com with SMTP id d9443c01a7336-21649a7bcdcso55827335ad.1; Sun, 12 Jan 2025 03:38:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736681894; x=1737286694; 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=rLjMFqMa0MLBHoGjYV+ZgrPN+yneWJOf3SVcKyDiUGI=; b=GA+7W1f5KMgK7+a+KEiZO0gf2Rc+A3SCKy8MwCWWDz2AsVTf0l1MUtgUGFvzaFet9f OYOggZDF3OJRVwvKs7tVXnRzwKBowt5gcqvUv5uUs/sA7vfAK4HobVtpOfiQpIAiWX2n VQl6XtA/OkERZ9xd7GJkisPX1w9JwTtaxNbN+9VlvqQ0fjaZxw0Qnc/5n/GLp/AewA8I hqHfc7D5VOtcE11AU/o2u26hdif8BzeH2b+hnPIeuhhNbK4DRqX+nQ8g8LddMHpqhJDv 9qkrpI2g/lacFoVI22XbQAaYu9wyTbkRKltxaS/4HafV3+UqRKOFLDy9zlWoraIz2Vib seCQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736681894; x=1737286694; 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=rLjMFqMa0MLBHoGjYV+ZgrPN+yneWJOf3SVcKyDiUGI=; b=eEg3zW7xdQGHdBnK5TugET/GzycCzaeka/icDb4PXPjD8yFPrlliItUDRL8Hnrt+mM /ltOmFXo/ieu9LN/sNyG5GfLjp10xJvPtewD8eGZSXKIAGwBY4R34ex0j26SQeBiV0BZ NvDipgwZYXRQHX033xB5OH1yreW4Q+umKeQ/faNtwhvbTl8i6oTW/FqS5nLtmWcTSNiL S/Tx8TEjEgvZk8e8nsrnz6QzruEphApC2oqDckaSUoLWEn4KeeDO62072+ekSgfFtHRw lkP8DF+bgLL4c75ye1DxyZfbVZynZCIBSpct8glDKO6Cg0gc/Jmjn0bKXGwnu4khgNrs atEw== X-Forwarded-Encrypted: i=1; AJvYcCXDLKdIHw1Tmnc/LOBRCc3SsV323SvEUb+HJ9swpL9hga7uBuFdYk7mY3Qh7c55bu8TElqpyqQ=@vger.kernel.org X-Gm-Message-State: AOJu0Yz9oNvhaf7t0ABsXLs/mwMa7BwFSVw8pyuW0y3+Ad52+tn34gSh Fm/C4PRxd4XhyVImecp5oW8UQ5GmEp+bKFUyY2ZGjVSZyi62lY3K X-Gm-Gg: ASbGncv/XhKSQbJ3RvIpRB3M3hl1has6eYB6LZMTNJ9pEvUv13YJtpM5Qf+5mlsbuFy HAKOe+vbbS7+iduV/go97067l/ChEE6sSzF3v+qCSOYKnSmXk6OyLLZSva4c8ks7lqwgrt3jOpm 24pFbDtyr3Q4B4TDaMY49ETx6jZ+MSbtpjTic6VWHwg0v6eeZIpdFHxpJLEmsv0o45hDYLW72+L n+F2g1xSLLozLClPSSI+LIS+LXM6GLq6xhEMSI6ohGq9wVK1b3GMNrOTPfxCQT+nyeCTE/2UCa2 nVxxfPVlWMrG8K3InWU= X-Google-Smtp-Source: AGHT+IH1WWA5xu6CBh1XrDpontq1UPp/scaC/ProBgb6LrQX0jQQHvjP/0MchngwrbX/aTJQ9hU4dw== X-Received: by 2002:a17:902:d492:b0:216:2bd7:1c2e with SMTP id d9443c01a7336-21a83f54a67mr208613255ad.18.1736681893748; Sun, 12 Jan 2025 03:38:13 -0800 (PST) Received: from KERNELXING-MC1.tencent.com ([111.201.29.174]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21a9f253a98sm37353765ad.224.2025.01.12.03.38.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 12 Jan 2025 03:38:13 -0800 (PST) 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, horms@kernel.org Cc: bpf@vger.kernel.org, netdev@vger.kernel.org, Jason Xing Subject: [PATCH net-next v5 03/15] bpf: introduce timestamp_used to allow UDP socket fetched in bpf prog Date: Sun, 12 Jan 2025 19:37:36 +0800 Message-Id: <20250112113748.73504-4-kerneljasonxing@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20250112113748.73504-1-kerneljasonxing@gmail.com> References: <20250112113748.73504-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 timestamp_used consists of two parts, one is is_fullsock, the other one is for UDP socket which will be support in the next round. Signed-off-by: Jason Xing --- include/linux/filter.h | 1 + net/core/filter.c | 4 ++-- net/core/sock.c | 1 + net/ipv4/tcp_input.c | 2 ++ net/ipv4/tcp_output.c | 2 ++ 5 files changed, 8 insertions(+), 2 deletions(-) diff --git a/include/linux/filter.h b/include/linux/filter.h index a3ea46281595..daca3fe48b8f 100644 --- a/include/linux/filter.h +++ b/include/linux/filter.h @@ -1508,6 +1508,7 @@ struct bpf_sock_ops_kern { void *skb_data_end; u8 op; u8 is_fullsock; + u8 timestamp_used; u8 remaining_opt_len; u64 temp; /* temp and everything after is not * initialized to 0 before calling diff --git a/net/core/filter.c b/net/core/filter.c index c6dd2d2e44c8..1ac996ec5e0f 100644 --- a/net/core/filter.c +++ b/net/core/filter.c @@ -10424,10 +10424,10 @@ static u32 sock_ops_convert_ctx_access(enum bpf_access_type type, } \ *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \ struct bpf_sock_ops_kern, \ - is_fullsock), \ + timestamp_used), \ fullsock_reg, si->src_reg, \ offsetof(struct bpf_sock_ops_kern, \ - is_fullsock)); \ + timestamp_used)); \ *insn++ = BPF_JMP_IMM(BPF_JEQ, fullsock_reg, 0, jmp); \ if (si->dst_reg == si->src_reg) \ *insn++ = BPF_LDX_MEM(BPF_DW, reg, si->src_reg, \ diff --git a/net/core/sock.c b/net/core/sock.c index e06bcafb1b2d..dbb9326ae9d1 100644 --- a/net/core/sock.c +++ b/net/core/sock.c @@ -958,6 +958,7 @@ void bpf_skops_tx_timestamping(struct sock *sk, struct sk_buff *skb, int op) if (sk_is_tcp(sk) && sk_fullsock(sk)) sock_ops.is_fullsock = 1; sock_ops.sk = sk; + sock_ops.timestamp_used = 1; __cgroup_bpf_run_filter_sock_ops(sk, &sock_ops, CGROUP_SOCK_OPS); } #endif diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c index 4811727b8a02..cad41ad34bd5 100644 --- a/net/ipv4/tcp_input.c +++ b/net/ipv4/tcp_input.c @@ -169,6 +169,7 @@ static void bpf_skops_parse_hdr(struct sock *sk, struct sk_buff *skb) memset(&sock_ops, 0, offsetof(struct bpf_sock_ops_kern, temp)); sock_ops.op = BPF_SOCK_OPS_PARSE_HDR_OPT_CB; sock_ops.is_fullsock = 1; + sock_ops.timestamp_used = 1; sock_ops.sk = sk; bpf_skops_init_skb(&sock_ops, skb, tcp_hdrlen(skb)); @@ -185,6 +186,7 @@ static void bpf_skops_established(struct sock *sk, int bpf_op, memset(&sock_ops, 0, offsetof(struct bpf_sock_ops_kern, temp)); sock_ops.op = bpf_op; sock_ops.is_fullsock = 1; + sock_ops.timestamp_used = 1; sock_ops.sk = sk; /* sk with TCP_REPAIR_ON does not have skb in tcp_finish_connect */ if (skb) diff --git a/net/ipv4/tcp_output.c b/net/ipv4/tcp_output.c index 0e5b9a654254..7b4d1dfd57d4 100644 --- a/net/ipv4/tcp_output.c +++ b/net/ipv4/tcp_output.c @@ -522,6 +522,7 @@ static void bpf_skops_hdr_opt_len(struct sock *sk, struct sk_buff *skb, sock_owned_by_me(sk); sock_ops.is_fullsock = 1; + sock_ops.timestamp_used = 1; sock_ops.sk = sk; } @@ -567,6 +568,7 @@ static void bpf_skops_write_hdr_opt(struct sock *sk, struct sk_buff *skb, sock_owned_by_me(sk); sock_ops.is_fullsock = 1; + sock_ops.timestamp_used = 1; sock_ops.sk = sk; } From patchwork Sun Jan 12 11:37:37 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Xing X-Patchwork-Id: 13936316 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-pl1-f176.google.com (mail-pl1-f176.google.com [209.85.214.176]) (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 AF9EF1ABED7; Sun, 12 Jan 2025 11:38:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.176 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736681901; cv=none; b=V9v1xsDYSdkrZT4w4PjXEyqvIAiIzLg25fDl/izJKpSPMRpV9oO6Sr+hfxORGrMXjAWqYduMjJ/3Np0sIS9FOMIRXE+AJnr3x7EH59843l89CeIXxDIOhfIb57qCV0HDTCO7wX9CNy+ghpEzzNgVHpHUtp+Od/VoT0JdfIxm6Ns= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736681901; c=relaxed/simple; bh=2ojxKwgn3BFaGLBy+4x4MvB+ljH5A2omqcDIvarA/Y4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=RXGRmffuxUMElsUgWVYBNi40YZQFRQWLoejIP4RY5Fta/ukz5BtRB3xjCe1Y4tZNzunSaeJjBcFGrHcINEChXuuNHD1ZXQIrMfn0gAw4czalQzGj6U32ia/VMhIi0j32bcNu4HfxBXVopAb/EuwsKoejskYE2qGm2K8HscIDZ6k= 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=K3WxXL6u; arc=none smtp.client-ip=209.85.214.176 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="K3WxXL6u" Received: by mail-pl1-f176.google.com with SMTP id d9443c01a7336-216281bc30fso69443915ad.0; Sun, 12 Jan 2025 03:38:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736681899; x=1737286699; 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=YeN/vKrKCPyqWVUSUJNR8E9aGAUgXYbg1YNcWt8dLR0=; b=K3WxXL6ugDHzZEDUllBL8gQDmBslrabMLQUygjLkWnkE19rHK7WUKZ98qL//qoQcen l4K5xmX4vnW08O4sos07EdtZMeJoF7inxpDQQE7POI3fyICN7VjBKHmIA94/a7PnaUse H2eaZ41qoZf+a99dZqzMLDJb7psc1Wa2gNvExTVJXHs/0Hol5EEpJlg7uSGs0iIQfrCy qzPDGMOzf3DDCcY8qy7SZzNeZr7443pQluuVPlbhSL0960OtjjsLqmqOV2YoLIlZewLO 22hGLLXLGLYb0VnAJkh8iLWYBPqBSrzkYCo7ZlijxP2eq9VzCfK2Hkxf2A2Oh3DKSxwt aenw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736681899; x=1737286699; 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=YeN/vKrKCPyqWVUSUJNR8E9aGAUgXYbg1YNcWt8dLR0=; b=EmcVt55y6eTvNrZP7BDu85iaQ1vjc7mWWPXgRBOgmOQGMRQxNq3F74HUb5rIO/ATKw MdtYmiQzIxR49DUQSG//oATrjXKm3Axw8CMjzp5iQWr7tf2q7nCnjamIM9tPHIIAuSYi O/b/WrnJp4BUjC/QFRPDL0yqHVPO9eu7EG1DTPs1Qb/59AIH5Do8CzoGhvZ85i8UDFoH G3PdHRTnjI+veEIn+gSsZrW8szB0GpopJral89+sOVZdBBAFlSaBfZum+gJCKzwFHtEN R8bRzujpqZxSmT62w8Jinczdxpk58PFm9Z8mYi+A5ePZHRy5sAlfQjDt89I844+2hxEt ZJrQ== X-Forwarded-Encrypted: i=1; AJvYcCW5cMV9Ux2ZZ9SqHjFYdW9efP/didwuEoc4Hx3HorhCzx1C7IBzbSnopZrfpeExEu58ZcS4KSc=@vger.kernel.org X-Gm-Message-State: AOJu0Yy0jiQVN7GsiHvpulSctmUP3QIznMLzDzKiAPfKY3Q84Q/QPu6t L8vuuTgg99TXSo2zQT6tMzPIr1OvvSgxAz1gGxRwRYpTjgXg0Ccw X-Gm-Gg: ASbGnct5zsWD2oOqf3SlEeYLV3H0Tauoj9swlqe5Vbt99vAfz7H3mLROaOVOz+oFdSX kzhWPFHOAX3ToHHTGKjkgHlXK2qxtYZ0eXUcC/Os0bpXyEQoUG9dxliiLabcLJwBGO5S+9VXpYj cxNoQiNU2o6iS+2iKJvSvkLJMI2beMdDBq+o3kCS7fhs7Z7Pmj7HdJLWY2axFxFEHE/tN2vFt99 p+Gcbch3Ohi+IbAzxa+zhym+giN84uiF1oTFaVmZ3W2XDJr0C8IAo9rPfiRcZaS2AGxte88cLex YZQk9eJtZrucxz1MMCQ= X-Google-Smtp-Source: AGHT+IFtxL4EaY3JhkqtySG9y9+g1BhTEfqP1e/DYr6xmJJ+RTuPHN8tpH3BT4cej1gTfOHBhUJrjQ== X-Received: by 2002:a17:902:e74b:b0:215:5ea2:654b with SMTP id d9443c01a7336-21a83f3eebemr266858895ad.1.1736681899016; Sun, 12 Jan 2025 03:38:19 -0800 (PST) Received: from KERNELXING-MC1.tencent.com ([111.201.29.174]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21a9f253a98sm37353765ad.224.2025.01.12.03.38.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 12 Jan 2025 03:38:18 -0800 (PST) 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, horms@kernel.org Cc: bpf@vger.kernel.org, netdev@vger.kernel.org, Jason Xing Subject: [PATCH net-next v5 04/15] net-timestamp: support SK_BPF_CB_FLAGS only in bpf_sock_ops_setsockopt Date: Sun, 12 Jan 2025 19:37:37 +0800 Message-Id: <20250112113748.73504-5-kerneljasonxing@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20250112113748.73504-1-kerneljasonxing@gmail.com> References: <20250112113748.73504-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 We will allow both TCP and UDP sockets to use this helper to enable this feature. So let SK_BPF_CB_FLAGS pass the check: 1. skip is_fullsock check 2. skip owned by me check Signed-off-by: Jason Xing --- net/core/filter.c | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/net/core/filter.c b/net/core/filter.c index 1ac996ec5e0f..0e915268db5f 100644 --- a/net/core/filter.c +++ b/net/core/filter.c @@ -5507,12 +5507,9 @@ static int sol_ipv6_sockopt(struct sock *sk, int optname, KERNEL_SOCKPTR(optval), *optlen); } -static int __bpf_setsockopt(struct sock *sk, int level, int optname, - char *optval, int optlen) +static int ___bpf_setsockopt(struct sock *sk, int level, int optname, + char *optval, int optlen) { - if (!sk_fullsock(sk)) - return -EINVAL; - if (level == SOL_SOCKET) return sol_socket_sockopt(sk, optname, optval, &optlen, false); else if (IS_ENABLED(CONFIG_INET) && level == SOL_IP) @@ -5525,6 +5522,15 @@ static int __bpf_setsockopt(struct sock *sk, int level, int optname, return -EINVAL; } +static int __bpf_setsockopt(struct sock *sk, int level, int optname, + char *optval, int optlen) +{ + if (!sk_fullsock(sk)) + return -EINVAL; + + return ___bpf_setsockopt(sk, level, optname, optval, optlen); +} + static int _bpf_setsockopt(struct sock *sk, int level, int optname, char *optval, int optlen) { @@ -5675,7 +5681,16 @@ static const struct bpf_func_proto bpf_sock_addr_getsockopt_proto = { BPF_CALL_5(bpf_sock_ops_setsockopt, struct bpf_sock_ops_kern *, bpf_sock, int, level, int, optname, char *, optval, int, optlen) { - return _bpf_setsockopt(bpf_sock->sk, level, optname, optval, optlen); + struct sock *sk = bpf_sock->sk; + + if (optname != SK_BPF_CB_FLAGS) { + if (sk_fullsock(sk)) + sock_owned_by_me(sk); + else if (optname != SK_BPF_CB_FLAGS) + return -EINVAL; + } + + return ___bpf_setsockopt(sk, level, optname, optval, optlen); } static const struct bpf_func_proto bpf_sock_ops_setsockopt_proto = { From patchwork Sun Jan 12 11:37:38 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Xing X-Patchwork-Id: 13936317 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 37E581ACECA; Sun, 12 Jan 2025 11:38:24 +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=1736681906; cv=none; b=oSuhJ06VtdyAK4L0D+zN6BQNRh6I1158twcxoxprT21AYQ7rfBDNlw3WsjIFzwJ1Jt2gSMWxwpPRqgpTjc1Qzbbb6ZlSkNpQbf2S2SrOM9epD8xswYJ6xVGanT6x6kUl8MDEuC9lac2rnfcHTHkWmOSotzDuJlskLQJSQ1dBAwk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736681906; c=relaxed/simple; bh=X8PG6ZLzql4XepmkV1UxAQ4M8RTqUr0lLOYSmC4iNKM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=mEoN1Ku26sa/ytgQNRDeZUx64mT+cVSiAlQ3y1Wj0GWPOvLVG26Ti0L7BrZID1DDe0EkrehiTLFuiOFii/3lLQJbgCbSKhuNOBGc+47kFix0YCxYLKPUb95pR/t0qMDh6w8pZcMDMuCdYUclQoNQ5V+0xgGbOjzm2oX2NQpjaFU= 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=MxHDHY0w; 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="MxHDHY0w" Received: by mail-pl1-f170.google.com with SMTP id d9443c01a7336-21636268e43so77522405ad.2; Sun, 12 Jan 2025 03:38:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736681904; x=1737286704; 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=fEm0zovFJqig1DzXXVbNZQTWvtqYNNSgeEtxTT36beo=; b=MxHDHY0wv6PRAc+AZCejPVdX3a0HMi1IpmAloKCZKVKYsaIiddwgAN9HGbqCAW2J+M 1YeIZJLzgGOXAl7RB+E6rv86MTlrEENqT8DaE6Q+OVZlvvWjlO0LmX790lGDHbsIimDY vQAxGcr5qyXT98aVVqPrVEQqf1PBoUfkbh/ijE/mZR/mUDwrHFra2XHlLvSW6nKMeX6G C9ivz48x8LPGLSvx7UKleaRSakCIBEHrPiHiehsFD/Z3EnJ5NXipweIntSEBtC/IEw9d 0RaWRuerhW8PshqYx5h3nhTiCk8jBWUgVJL9w11TT1v0aIA7Ioq97rRj1I3zJ4Zn7lVZ Ywgw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736681904; x=1737286704; 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=fEm0zovFJqig1DzXXVbNZQTWvtqYNNSgeEtxTT36beo=; b=Ob7PU64wx+iZD103Kk27KjWaEMCc6AklK0lmrstSP1sNEWAUfXIsMxmDFxUJV15PRO FDYYwEVcOrY9CAcxKC8EPqHrSDCCQWtTfzvFkwVIdvLNKR2bXf6LFujYBMjj3pnPUK6s 49X1rxXFnBAwJCmdIZD3800yfN+jRzvoch3IUkJcywIWSZ/+mw2BxwEXFYNj87mNg+s2 nKeiNXJhmE1jLi9paMz/Jq0LqvGnLDBBIAAdZjbvVdw7F/P4rE8QeA1B2H0R+kjhZckd bDKvhLhMd9w8LtKzUyd31cnIoYfvE0wIQZ3SyYm+h8NkhX5RLodkZuf+vlDkN16ehmAK DL5Q== X-Forwarded-Encrypted: i=1; AJvYcCXzLxEIB75aMDJ5zxaDrOwRjzKt7VQm5wo857sRQk3ZoI5+BbUhuB56bRHoj/uK+phXkD8vkC0=@vger.kernel.org X-Gm-Message-State: AOJu0YwIp25kTpa/F/pL5rIfUrpBMAD8CWxFhQO7tgAMnWyPbRpj5m0N q6/jFUeOLjJo8MHWzO1MzBwqB8PymDfytwIMUkKu0Ld4V3qQ967v X-Gm-Gg: ASbGnctFeZdLfYt3BrRtQlsniplsuwePa8VdVr6ylVsCzkmS0erBKzWVfAphTykH4Lm SbXDZ0SzJ7tSwW0+HFa+FoJpWe8ox5pogAfIySeWRwQtIMZrfJR8IX+tVCgBmmLw4RSIkC5MLvN TEzHbwSWXKJZI2nbHlgevJ6YPvKmh4slBYXHHk3oKiXIAbKrvmFV5Ae3HFzO44Y/aK4gRe7xRs0 0x28RVA+sEBloR7/LfO7bEZGYw7Phh7hN+NFD8LkiVOC1tD8sGDhiBtMEbaHq/q81Ic9RsSVHF5 5uNX64WsWGv1iTe112g= X-Google-Smtp-Source: AGHT+IEnBnWA7PeX66ST6O6xNz5j7ZtFWo9xBOoUm2pKfVacVqFpQQY7TwB+8tpyat697IY5DlFCcA== X-Received: by 2002:a17:902:cec3:b0:211:ce91:63ea with SMTP id d9443c01a7336-21a83f56f9emr260140035ad.15.1736681904534; Sun, 12 Jan 2025 03:38:24 -0800 (PST) Received: from KERNELXING-MC1.tencent.com ([111.201.29.174]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21a9f253a98sm37353765ad.224.2025.01.12.03.38.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 12 Jan 2025 03:38:24 -0800 (PST) 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, horms@kernel.org Cc: bpf@vger.kernel.org, netdev@vger.kernel.org, Jason Xing Subject: [PATCH net-next v5 05/15] net-timestamp: add strict check in some BPF calls Date: Sun, 12 Jan 2025 19:37:38 +0800 Message-Id: <20250112113748.73504-6-kerneljasonxing@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20250112113748.73504-1-kerneljasonxing@gmail.com> References: <20250112113748.73504-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 In the next round, we will support the UDP proto for SO_TIMESTAMPING bpf extension, so we need to ensure there is no safety problem. Signed-off-by: Jason Xing --- net/core/filter.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/net/core/filter.c b/net/core/filter.c index 0e915268db5f..517f09aabc92 100644 --- a/net/core/filter.c +++ b/net/core/filter.c @@ -5571,7 +5571,7 @@ static int __bpf_getsockopt(struct sock *sk, int level, int optname, static int _bpf_getsockopt(struct sock *sk, int level, int optname, char *optval, int optlen) { - if (sk_fullsock(sk)) + if (sk_fullsock(sk) && optname != SK_BPF_CB_FLAGS) sock_owned_by_me(sk); return __bpf_getsockopt(sk, level, optname, optval, optlen); } @@ -5776,6 +5776,7 @@ BPF_CALL_5(bpf_sock_ops_getsockopt, struct bpf_sock_ops_kern *, bpf_sock, int, level, int, optname, char *, optval, int, optlen) { if (IS_ENABLED(CONFIG_INET) && level == SOL_TCP && + bpf_sock->sk->sk_protocol == IPPROTO_TCP && optname >= TCP_BPF_SYN && optname <= TCP_BPF_SYN_MAC) { int ret, copy_len = 0; const u8 *start; @@ -5817,7 +5818,8 @@ BPF_CALL_2(bpf_sock_ops_cb_flags_set, struct bpf_sock_ops_kern *, bpf_sock, struct sock *sk = bpf_sock->sk; int val = argval & BPF_SOCK_OPS_ALL_CB_FLAGS; - if (!IS_ENABLED(CONFIG_INET) || !sk_fullsock(sk)) + if (!IS_ENABLED(CONFIG_INET) || !sk_fullsock(sk) || + sk->sk_protocol != IPPROTO_TCP) return -EINVAL; tcp_sk(sk)->bpf_sock_ops_cb_flags = val; @@ -7626,6 +7628,9 @@ BPF_CALL_4(bpf_sock_ops_load_hdr_opt, struct bpf_sock_ops_kern *, bpf_sock, u8 search_kind, search_len, copy_len, magic_len; int ret; + if (bpf_sock->op != SK_BPF_CB_FLAGS) + return -EINVAL; + /* 2 byte is the minimal option len except TCPOPT_NOP and * TCPOPT_EOL which are useless for the bpf prog to learn * and this helper disallow loading them also. From patchwork Sun Jan 12 11:37:39 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Xing X-Patchwork-Id: 13936318 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 816D31B0F27; Sun, 12 Jan 2025 11:38:30 +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=1736681912; cv=none; b=HfTBgPAwnGt03r4vIkoUGkcOFNyulSBh+9roGX+Lm7BwPR6mix2CgJuqNX9a+Gt0ASjTIKTM5rZXBTo6Yqu5I2F0V0Bx1F+4mIhRaPxfraGYe1Tw+Q9RTItyq4CrqyFYPg5r4jWUYKueK27513jc3GAuw7AFTA/klZ4hyyyuT1Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736681912; c=relaxed/simple; bh=+Y8VkW1rPLeyKhkY0/ncArtR6rMW9fgkqpTbp9gHm10=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=bf3Rk+F9trroGaPmwxhSFLtH/Gh/PR8zhiLyj69UAScdT8LPV6wgAKL2fdmlDwWwXWCqfgfxAgz9Wnnrdd3r0XY24ec+tLpAtbi04SxmlobD+bVWgtdmXihPgrkTuJxzPVc6lmVLBwCuBv8Z6c6i52Ye1EYQJoSrzn7EwnLdTZs= 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=gWTa7oy+; 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="gWTa7oy+" Received: by mail-pl1-f181.google.com with SMTP id d9443c01a7336-21a1e6fd923so77078575ad.1; Sun, 12 Jan 2025 03:38:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736681910; x=1737286710; 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=r1RCiHcUSqZQwwgY6I8u+HQGqR3XU2ZymQTTWqq7ylg=; b=gWTa7oy+ouAMr8tCndzOlYL2piT4uiQGbw7fUhwUj5S7FHeA/59VyExXEX4s2eGQQD Vc1r40wiFluRferTn4K9lMfmaItbtCFzJNaSBaaGNfQ9nUyxbstwKW6fHzBcrsEa5/eY BHA2/RNHkTCfXUUVs8ZQTUZAprZbUrRtb1VLCTBXww7PGH6xxqumCEzoBb82wTGLvDOb UBYR0GcCPmk6itmmYAjv/ypQY/BMamOtkM9/moENGj407fPV3nQv7/Srn6XnG9uR2SAa z/7o9JOqj3FKqFi2ATUS/VULs3LU4GGAcr305N1DB69VCABGYAeq9BxClaOJ6Gl4UjnR ci5Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736681910; x=1737286710; 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=r1RCiHcUSqZQwwgY6I8u+HQGqR3XU2ZymQTTWqq7ylg=; b=OM6ib6oTP9/smRT+MDxX2f9KdZB8hIOQw1q6oavVQ96ZkJP0D4f/xpziZvM2/a7b8M RUV0Fj7RPPSlWPxVMoZIPXDlTC1eAVGAZF0A8PEAtLRco0OBpWqkOS5pyh7xUkbgdtB7 YPPwT3eE9Lq2Yf9ajRihp9Pxsjr1pTgWGxhAjypGSAyafBj/CdcbUSKy+lFzmgFMT6C4 C+qtM4K/8vZNuR+bxMTQGQq6TIYcZcZcMZiCtlHOoF2jI/v13kVw4lYs9T9qV2PCS0zY VzqmH+zoIzG8GVCoaSScgAtIJybfUHetl7tKYayqdcUv694icMCNSotyjb5/jBGbPrMf lgiw== X-Forwarded-Encrypted: i=1; AJvYcCVjc5OmI0uv4agt9PBB2HvBODpdWVm+2YqeAvbcpNN0NHQi+90JjAkgPYXV7YAbtP4efDNUhY0=@vger.kernel.org X-Gm-Message-State: AOJu0YytMOEg8yc3yPWP/5qCuiuFATiIFHdlUvwpLW4cV62RHB00UQrs 512lME40N7IcBEnO3s5SlAQUvVkkNneAHK97ko8eHYYj4MSNUBGX X-Gm-Gg: ASbGncuT9NqJJ2PPD7LfdFsdQXdSe+VBtnwexWwUuznQfArr2AZPmHcpi9bYNAUUpmw GEUmh9pz03BeLDZo79vJ2NOZwd0+Yg8TsKeFYodldVKFMXDp6CVWpgMTcSjX4qwLKnOUAHmCqMa aT/ay009AUMCvakeliAlihHMWZOrXG3ie/cS0rCveQ6KC11UsnirweXLkyKR+JmlQ6qDFkZKNeC x5R6BqDmDxlOC2mdxODJkLWHTUphLJqsxa4Dc6VzF9QYRE78fA34c23J2Vob20S4oxa4/xpouLR MusLOzzKEDe5nL/SxQQ= X-Google-Smtp-Source: AGHT+IEq502QFKXZYYhiDw77wQP4J6WBlxm0IG8QypOdhUeBJTTNWt6BSBdTt5u01wuxGx6zmylgYg== X-Received: by 2002:a17:902:ce8d:b0:216:3e86:1cb9 with SMTP id d9443c01a7336-21a83fdf1a1mr233697465ad.50.1736681909818; Sun, 12 Jan 2025 03:38:29 -0800 (PST) Received: from KERNELXING-MC1.tencent.com ([111.201.29.174]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21a9f253a98sm37353765ad.224.2025.01.12.03.38.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 12 Jan 2025 03:38:29 -0800 (PST) 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, horms@kernel.org Cc: bpf@vger.kernel.org, netdev@vger.kernel.org, Jason Xing Subject: [PATCH net-next v5 06/15] net-timestamp: prepare for isolating two modes of SO_TIMESTAMPING Date: Sun, 12 Jan 2025 19:37:39 +0800 Message-Id: <20250112113748.73504-7-kerneljasonxing@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20250112113748.73504-1-kerneljasonxing@gmail.com> References: <20250112113748.73504-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 No functional changes here. I add skb_enable_app_tstamp() to test if the orig_skb matches the usage of application SO_TIMESTAMPING and skb_sw_tstamp_tx() to distinguish the software and hardware timestamp when tsflag is SCM_TSTAMP_SND. After this patch, I will soon add checks about bpf SO_TIMESTAMPING. In this way, we can support two modes parallelly. Signed-off-by: Jason Xing --- include/linux/skbuff.h | 22 ++++++++++++++++------ net/core/dev.c | 2 +- net/core/skbuff.c | 42 ++++++++++++++++++++++++++++++++++++++++-- net/ipv4/tcp_input.c | 3 ++- 4 files changed, 59 insertions(+), 10 deletions(-) diff --git a/include/linux/skbuff.h b/include/linux/skbuff.h index bb2b751d274a..09461ee84d2f 100644 --- a/include/linux/skbuff.h +++ b/include/linux/skbuff.h @@ -4533,21 +4533,31 @@ void skb_complete_tx_timestamp(struct sk_buff *skb, 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); + struct sock *sk, bool sw, int tstype); /** - * skb_tstamp_tx - queue clone of skb with send time stamps + * skb_tstamp_tx - queue clone of skb with send time HARDWARE stamps * @orig_skb: the original outgoing packet * @hwtstamps: hardware time stamps, may be NULL if not available * * If the skb has a socket associated, then this function clones the * skb (thus sharing the actual data and optional structures), stores - * the optional hardware time stamping information (if non NULL) or - * generates a software time stamp (otherwise), then queues the clone - * to the error queue of the socket. Errors are silently ignored. + * the optional hardware time stamping information (if non NULL) then + * queues the clone to the error queue of the socket. Errors are + * silently ignored. */ void skb_tstamp_tx(struct sk_buff *orig_skb, struct skb_shared_hwtstamps *hwtstamps); +/** + * skb_tstamp_tx - queue clone of skb with send time SOFTWARE stamps + * @orig_skb: the original outgoing packet + * + * If the skb has a socket associated, then this function clones the + * skb (thus sharing the actual data and optional structures), + * generates a software time stamp (otherwise), then queues the clone + * to the error queue of the socket. Errors are silently ignored. + */ +void skb_sw_tstamp_tx(struct sk_buff *orig_skb); /** * skb_tx_timestamp() - Driver hook for transmit timestamping @@ -4565,7 +4575,7 @@ static inline void skb_tx_timestamp(struct sk_buff *skb) { skb_clone_tx_timestamp(skb); if (skb_shinfo(skb)->tx_flags & SKBTX_SW_TSTAMP) - skb_tstamp_tx(skb, NULL); + skb_sw_tstamp_tx(skb); } /** diff --git a/net/core/dev.c b/net/core/dev.c index 1a90ed8cc6cc..397fbcd5e4de 100644 --- a/net/core/dev.c +++ b/net/core/dev.c @@ -4398,7 +4398,7 @@ int __dev_queue_xmit(struct sk_buff *skb, struct net_device *sb_dev) skb_assert_len(skb); if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_SCHED_TSTAMP)) - __skb_tstamp_tx(skb, NULL, NULL, skb->sk, SCM_TSTAMP_SCHED); + __skb_tstamp_tx(skb, NULL, NULL, skb->sk, true, SCM_TSTAMP_SCHED); /* Disable soft irqs for various locks below. Also * stops preemption for RCU. diff --git a/net/core/skbuff.c b/net/core/skbuff.c index a441613a1e6c..b34c7ec3d5e9 100644 --- a/net/core/skbuff.c +++ b/net/core/skbuff.c @@ -5539,10 +5539,38 @@ void skb_complete_tx_timestamp(struct sk_buff *skb, } EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp); +static bool skb_enable_app_tstamp(struct sk_buff *skb, int tstype, bool sw) +{ + int flag; + + switch (tstype) { + case SCM_TSTAMP_SCHED: + flag = SKBTX_SCHED_TSTAMP; + break; + case SCM_TSTAMP_SND: + if (sw) + flag = SKBTX_SW_TSTAMP; + else + flag = SKBTX_HW_TSTAMP; + break; + case SCM_TSTAMP_ACK: + if (TCP_SKB_CB(skb)->txstamp_ack) + return true; + fallthrough; + default: + return false; + } + + if (skb_shinfo(skb)->tx_flags & flag) + return true; + + return false; +} + 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) + struct sock *sk, bool sw, int tstype) { struct sk_buff *skb; bool tsonly, opt_stats = false; @@ -5551,6 +5579,9 @@ void __skb_tstamp_tx(struct sk_buff *orig_skb, if (!sk) return; + if (!skb_enable_app_tstamp(orig_skb, tstype, sw)) + return; + tsflags = READ_ONCE(sk->sk_tsflags); if (!hwtstamps && !(tsflags & SOF_TIMESTAMPING_OPT_TX_SWHW) && skb_shinfo(orig_skb)->tx_flags & SKBTX_IN_PROGRESS) @@ -5596,10 +5627,17 @@ void __skb_tstamp_tx(struct sk_buff *orig_skb, } EXPORT_SYMBOL_GPL(__skb_tstamp_tx); +void skb_sw_tstamp_tx(struct sk_buff *orig_skb) +{ + return __skb_tstamp_tx(orig_skb, NULL, NULL, orig_skb->sk, true, + SCM_TSTAMP_SND); +} +EXPORT_SYMBOL_GPL(skb_sw_tstamp_tx); + void skb_tstamp_tx(struct sk_buff *orig_skb, struct skb_shared_hwtstamps *hwtstamps) { - return __skb_tstamp_tx(orig_skb, NULL, hwtstamps, orig_skb->sk, + return __skb_tstamp_tx(orig_skb, NULL, hwtstamps, orig_skb->sk, false, SCM_TSTAMP_SND); } EXPORT_SYMBOL_GPL(skb_tstamp_tx); diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c index cad41ad34bd5..c2cdd0acb504 100644 --- a/net/ipv4/tcp_input.c +++ b/net/ipv4/tcp_input.c @@ -3330,7 +3330,8 @@ static void tcp_ack_tstamp(struct sock *sk, struct sk_buff *skb, if (!before(shinfo->tskey, prior_snd_una) && before(shinfo->tskey, tcp_sk(sk)->snd_una)) { tcp_skb_tsorted_save(skb) { - __skb_tstamp_tx(skb, ack_skb, NULL, sk, SCM_TSTAMP_ACK); + __skb_tstamp_tx(skb, ack_skb, NULL, sk, true, + SCM_TSTAMP_ACK); } tcp_skb_tsorted_restore(skb); } } From patchwork Sun Jan 12 11:37:40 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Xing X-Patchwork-Id: 13936319 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 1335D1AD3F6; Sun, 12 Jan 2025 11:38: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=1736681917; cv=none; b=inxeNopoO4FK3tolyG96HR1d3n/QI7BOu6MlLMcggh+qO7U3DvC8zy2maZ9r1LstH40cpo8y2lkdlaUgc+FMmR4D4YlzHOuNAxW057mwPA33w1RWdkFX4DUFxU7YKG2mdSZYmQxNXpjziDXpb2jBqvDtlXEgfqKt00pw65ouK/A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736681917; c=relaxed/simple; bh=Hhvd0hmHqkWjBZwc8xc/MVJ4wpsWacTxStTLosIT/bU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=MddRqAH6P+5rG5WT7M984LJmSdizno1Ue0vnXxQr4ikO7bQ8peuzXZYxCzjXmgKQ6xD83wjQcgaM0lOYIjNYGYqv8zwqaPOeAmIUCRphWQZYPrMp6aIiFwjxHZgma6RH8J6Bm9bY6K7N1i9ZpF88dnkSLsEQIiMyC0NF+kcEvEI= 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=lM1GYkTM; 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="lM1GYkTM" Received: by mail-pl1-f181.google.com with SMTP id d9443c01a7336-21649a7bcdcso55829125ad.1; Sun, 12 Jan 2025 03:38:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736681915; x=1737286715; 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=T46OJJ70WGat//Ojd9KW5PyoOlR3VuuKBTjr81P+Sec=; b=lM1GYkTMtoDhZwNP+KrJZIfyhAuVfa8fxAOC6ItKPdjx+Muu+UAPsiSYczmhZFWB6v VY1bto14js/9hjQjDnbntVRe+8PCnXGowGm0WEjeS7sO6qGrXIODCa/6XXjfOTeKmqt+ +GFTbzh/XrVPv4xQMrMutMva0Ou4hn1mpv8JWgePWGmbiqWxFmZbcEIKBtAKWMA2AAgC qaKizX/bDfqSNHq81VT/wjP2SPGrZ/d+c9ByFIrxg9GTeMC1397Xedn9codRCD9b6V/i wNti+Ny1yI88uOu9LrpKgPTPvdn5DGWITeeDnshlSELFgsmmVG1x8FZnZgzepk7dPjVQ IOtw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736681915; x=1737286715; 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=T46OJJ70WGat//Ojd9KW5PyoOlR3VuuKBTjr81P+Sec=; b=dJB2bdJ9Bm23bJy87LKCa2ALxLzS7IXmirXmtLszTFPwWLf71ugsFNyl7762+dMSop Y8pa5/t+dTNnBVWjkAZ31tpb/kE5YQQIfTymOy0IRUsVrmodqYhYxUwBnQjloBmKIUfp 0CjtCQEVwe8bRlK9CRGrsGXZeVhrTqJm2hBl1KK4B7IbTkMKvf5Qk5FAAS4dLI34vfjL 6T8j+8pCv3jUyhReaSyDY9TalyGM2vK+04kMrj2YpZ6q9V1ezNjNCaZIsgTqM6hqt7HC dm17D5UnPOI+DoWxCMA9etnoM5TQVF7mQCT5Hy9lLLaEe7qnFMGzJYAVk70tXclIjSwj qNlw== X-Forwarded-Encrypted: i=1; AJvYcCVcbO2oF8Pm+t9Xb2z5GjkXCN2GnjXY7C5HNJtXMNyYuYIH9rMXpx0grHirPenOQjCAecyb67g=@vger.kernel.org X-Gm-Message-State: AOJu0YyRjPmp53kh0Mt6zbvnQevaPYu3PB0AsJOdNbTgHKEFBkePv0EO m4Q9U9P/g5wUvTVzr/DXDAr0CfxN2r+w5Ie0MgwtCaVOKOlW/pxH X-Gm-Gg: ASbGncuRSMxcbVd4hVn/maqB9H9XiXztkfS5uZUUUslxXu/VJqa/M+EQHyHb8k8XOW8 cEwX5f/XMqb7OTN7ElcU6YHJ89ERLjFNez+sGGh1+7lWJ9SDLjINvgVwzIbql+VWnHvSAUPn7I1 5xFK9F7ilY5Yw+FVywO4MHiw1/PrqnhHJSviWUrCbnDk5mX0F74yRxraODri7/hukw797p7L1U9 hNVFPB/ag+zkv0i6Cq5Ka4TextvvQ2YCPhYF7WW0lrOoJcqvKmnLCVrHWfwikPlD8PzteCXuG4V VFVBZBROhwr9RpRZfl0= X-Google-Smtp-Source: AGHT+IEDWnidsn58sdHKgFs0VQdQy6MoPfkCjTdT5c11pxClfbw0ZtoQdH6FIOHcu/geolmjnFLUAg== X-Received: by 2002:a17:903:1248:b0:216:282d:c67b with SMTP id d9443c01a7336-21a83fc042dmr249393025ad.35.1736681915314; Sun, 12 Jan 2025 03:38:35 -0800 (PST) Received: from KERNELXING-MC1.tencent.com ([111.201.29.174]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21a9f253a98sm37353765ad.224.2025.01.12.03.38.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 12 Jan 2025 03:38:34 -0800 (PST) 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, horms@kernel.org Cc: bpf@vger.kernel.org, netdev@vger.kernel.org, Jason Xing Subject: [PATCH net-next v5 07/15] net-timestamp: support SCM_TSTAMP_SCHED for bpf extension Date: Sun, 12 Jan 2025 19:37:40 +0800 Message-Id: <20250112113748.73504-8-kerneljasonxing@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20250112113748.73504-1-kerneljasonxing@gmail.com> References: <20250112113748.73504-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 Introducing SKBTX_BPF is used as an indicator telling us whether the skb should be traced by the bpf prog. Signed-off-by: Jason Xing --- include/linux/skbuff.h | 6 +++++- include/uapi/linux/bpf.h | 5 +++++ net/core/dev.c | 3 ++- net/core/skbuff.c | 20 ++++++++++++++++++++ tools/include/uapi/linux/bpf.h | 5 +++++ 5 files changed, 37 insertions(+), 2 deletions(-) diff --git a/include/linux/skbuff.h b/include/linux/skbuff.h index 09461ee84d2f..30901dfb4539 100644 --- a/include/linux/skbuff.h +++ b/include/linux/skbuff.h @@ -489,10 +489,14 @@ enum { /* generate software time stamp when entering packet scheduling */ SKBTX_SCHED_TSTAMP = 1 << 6, + + /* used for bpf extension when a bpf program is loaded */ + SKBTX_BPF = 1 << 7, }; #define SKBTX_ANY_SW_TSTAMP (SKBTX_SW_TSTAMP | \ - SKBTX_SCHED_TSTAMP) + SKBTX_SCHED_TSTAMP | \ + SKBTX_BPF) #define SKBTX_ANY_TSTAMP (SKBTX_HW_TSTAMP | \ SKBTX_HW_TSTAMP_USE_CYCLES | \ SKBTX_ANY_SW_TSTAMP) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index e629e09b0b31..72f93c6e45c1 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -7022,6 +7022,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/dev.c b/net/core/dev.c index 397fbcd5e4de..3e0cee0b3a0b 100644 --- a/net/core/dev.c +++ b/net/core/dev.c @@ -4397,7 +4397,8 @@ int __dev_queue_xmit(struct sk_buff *skb, struct net_device *sb_dev) skb_reset_mac_header(skb); skb_assert_len(skb); - if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_SCHED_TSTAMP)) + if (unlikely(skb_shinfo(skb)->tx_flags & + (SKBTX_SCHED_TSTAMP | SKBTX_BPF))) __skb_tstamp_tx(skb, NULL, NULL, skb->sk, true, SCM_TSTAMP_SCHED); /* Disable soft irqs for various locks below. Also diff --git a/net/core/skbuff.c b/net/core/skbuff.c index b34c7ec3d5e9..169c6d03d698 100644 --- a/net/core/skbuff.c +++ b/net/core/skbuff.c @@ -5567,6 +5567,24 @@ static bool skb_enable_app_tstamp(struct sk_buff *skb, int tstype, bool sw) return false; } +static void __skb_tstamp_tx_bpf(struct sk_buff *skb, struct sock *sk, int tstype) +{ + int op; + + if (!sk) + return; + + switch (tstype) { + case SCM_TSTAMP_SCHED: + op = BPF_SOCK_OPS_TS_SCHED_OPT_CB; + break; + default: + return; + } + + bpf_skops_tx_timestamping(sk, skb, op); +} + void __skb_tstamp_tx(struct sk_buff *orig_skb, const struct sk_buff *ack_skb, struct skb_shared_hwtstamps *hwtstamps, @@ -5578,6 +5596,8 @@ void __skb_tstamp_tx(struct sk_buff *orig_skb, if (!sk) return; + if (skb_shinfo(orig_skb)->tx_flags & SKBTX_BPF) + __skb_tstamp_tx_bpf(orig_skb, sk, tstype); if (!skb_enable_app_tstamp(orig_skb, tstype, sw)) return; diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index 6b0a5b787b12..891217ab6d2d 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -7015,6 +7015,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 Sun Jan 12 11:37:41 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Xing X-Patchwork-Id: 13936320 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 CBC361AF0C8; Sun, 12 Jan 2025 11:38:41 +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=1736681923; cv=none; b=Hz4uR1o1CRw/z69PTyFBaVHWtCB6ygQwOCH7gBi0HZ9MufP2ALRQtlCHXFZq3YKfqiP9yAfF+dTvSkkiWj3cmVnZManSjeIOJ+Mio2clMMNcsntpa/EZvLDlhw1fV8goDeJ4t3A3eT6cTqCr8ERaqAPTLfw30MXHhxfUkJ/z2Gg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736681923; c=relaxed/simple; bh=P/NHx5OEjipLf4SoTZq/dtCgh3EtgrlGbSyeEsZVCaw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=GWzgaaUdtxwSDijXw2SeLIwC6m5cos41yK54+600QUMnnlpwtulfdM9N7XyPHYMOWBTp+EU1cncNab1o2U9nX6d3+xAdw8u7dVqpp5MNcIXZN8av0X8q14NTNUiRqwz9XHfzmZ9M+EciATSTFXlTzRWTfS6XKjqLuRCeoxFllFs= 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=jkOamo4J; 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="jkOamo4J" Received: by mail-pl1-f172.google.com with SMTP id d9443c01a7336-21675fd60feso78010185ad.2; Sun, 12 Jan 2025 03:38:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736681921; x=1737286721; 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=lcP/mlvoDSBLf6CXQO6H4lxilPQTJXjgBrRAWxyv3VY=; b=jkOamo4J8ZUg03IOICbtncC5RdKhhSWFOj7qzEn2g2tSpw6zizF/Qdd/ryRMcAM5Ru 6MG8spaA4I31S0wisvQBWIfafOupIILgdee5R05w7ivrU+fpjOOvGtZ04Xh8kX55+ZW6 Kxr61N8jYBCMIAVVPHHETjh/BfDsG076xhFBCDxfxNPUL5tepcqB4jE+LObsuhvo2yPY GYh6HGqNhoAkCG79Md37hp2taD26TNQr61ElB9bMxks2iSsscYFnCRfpmbPANDcXX+Op HUcGOg4TFc1pGSi5XVTP4kVidJyXfV35YfbOHQ1/3Dsc6jYfGKUGNlCADVUIJHpJvsqu 6zXg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736681921; x=1737286721; 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=lcP/mlvoDSBLf6CXQO6H4lxilPQTJXjgBrRAWxyv3VY=; b=wk1k+WwFSNl+b6dqxD86qVkNrtrIwXCDqEepVI/Idylg0ZBGZyPFCZeZGo/P2Ewygx AMPHSNZKoVdeaDi4nkRM3+DP+bYGjUiw4Cs0vFByFB7Fyu8wEc6wdXNPIpjEmPcbDmyj DLomiOK7pQ9VmRLz8h+PYPnwNw8cB+nVNZl79gF4FfsNOrXExo6dp5FigC+oT4bpOJ60 Nawp6e7iIAeWLSr4tEErvzRi4ErqCsQLIMfqXU5RlCHcM6NbqjV9sbt2N/1V0QLraaYL uBB4H94bz3h3Lm1DbW6Uk6PHEuLdOk9hqxtTK8qszv4g3FiweHNiLyCF8Tq6oLLK8KwF 7vQQ== X-Forwarded-Encrypted: i=1; AJvYcCVguoXOBw+kENNlw6dItHLh+gtbpiuf1WAnW20uYnWI/iFPaLbfoB2+xIdRrjL1oSaOl3EnGJw=@vger.kernel.org X-Gm-Message-State: AOJu0YyX9dObBKPSaEcSw+Bl4Rv1bk5wa6iFgsa3SGpXcsGd7rvhf/vF 5bDpeFx9uAo5U9nqm8iL4EvpL7N4ZsS2teW5n8Yn8aRG5CX3Smhb X-Gm-Gg: ASbGncvE27P0ZbXu/VFkNHpMTiZSHUQnpTFAdwKOyB+4dFfn5+17lRce2QN8yKJXp5b kk8yGu+NChe7ZGe3UNY+TsUiaP5VM8hOQ4iyWncqf5ZZGPy0YqZkP1YePxOWwLuSb07o5a6fALs 5y6ocyn+HKE5Ze5Pm2kkD8xZSnqemjPxOcKjwLvy1j4iGLjyUGzZKYPp3Yhhf/p1nvTmDxyT259 BUIH1e2x3LBvwu8TfzlszgCiH1oPJyKdDlnLR8IM0KdhADBgR6YolZc88KeX4iCzlgARdF977nj lqF16tZcjTBp3es0V8Q= X-Google-Smtp-Source: AGHT+IH8fChqzS+3WK5BIKB0C0AWwwgBXBsdDijrGtOVPgT8brg0YBjwffhhAfJvNFTGThNPpVvUUQ== X-Received: by 2002:a17:903:120b:b0:216:3e87:c9fc with SMTP id d9443c01a7336-21a83f48c19mr246070305ad.5.1736681921016; Sun, 12 Jan 2025 03:38:41 -0800 (PST) Received: from KERNELXING-MC1.tencent.com ([111.201.29.174]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21a9f253a98sm37353765ad.224.2025.01.12.03.38.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 12 Jan 2025 03:38:40 -0800 (PST) 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, horms@kernel.org Cc: bpf@vger.kernel.org, netdev@vger.kernel.org, Jason Xing Subject: [PATCH net-next v5 08/15] net-timestamp: support sw SCM_TSTAMP_SND for bpf extension Date: Sun, 12 Jan 2025 19:37:41 +0800 Message-Id: <20250112113748.73504-9-kerneljasonxing@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20250112113748.73504-1-kerneljasonxing@gmail.com> References: <20250112113748.73504-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 Support SCM_TSTAMP_SND case. Then we will get the software timestamp when the driver is about to send the skb. Later, I will support the hardware timestamp. Signed-off-by: Jason Xing --- include/linux/skbuff.h | 2 +- include/uapi/linux/bpf.h | 5 +++++ net/core/skbuff.c | 3 +++ tools/include/uapi/linux/bpf.h | 5 +++++ 4 files changed, 14 insertions(+), 1 deletion(-) diff --git a/include/linux/skbuff.h b/include/linux/skbuff.h index 30901dfb4539..4f38c17c67a7 100644 --- a/include/linux/skbuff.h +++ b/include/linux/skbuff.h @@ -4578,7 +4578,7 @@ void skb_sw_tstamp_tx(struct sk_buff *orig_skb); static inline void skb_tx_timestamp(struct sk_buff *skb) { skb_clone_tx_timestamp(skb); - if (skb_shinfo(skb)->tx_flags & SKBTX_SW_TSTAMP) + if (skb_shinfo(skb)->tx_flags & (SKBTX_SW_TSTAMP | SKBTX_BPF)) skb_sw_tstamp_tx(skb); } diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 72f93c6e45c1..a6d761f07f67 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -7027,6 +7027,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 169c6d03d698..0fb31df4ed95 100644 --- a/net/core/skbuff.c +++ b/net/core/skbuff.c @@ -5578,6 +5578,9 @@ static void __skb_tstamp_tx_bpf(struct sk_buff *skb, struct sock *sk, int tstype case SCM_TSTAMP_SCHED: op = BPF_SOCK_OPS_TS_SCHED_OPT_CB; break; + case SCM_TSTAMP_SND: + op = BPF_SOCK_OPS_TS_SW_OPT_CB; + break; default: return; } diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index 891217ab6d2d..73fc0a95c9ca 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -7020,6 +7020,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 Sun Jan 12 11:37:42 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Xing X-Patchwork-Id: 13936321 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-pl1-f177.google.com (mail-pl1-f177.google.com [209.85.214.177]) (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 50C961AF0BA; Sun, 12 Jan 2025 11:38:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736681928; cv=none; b=G/UfzesiMVe10kNxS3Taa98Skzl++XPxwURapk0e1n7WasrFgjiDWZvjQeuJjVdnKUqpj74XWSBp6ObhKAVmGdVCyXpvVy6N/Ve0JFySz2CSYyT2O0EMjLw6iQcCeG4CaTfJaJAqL08/4P5rZeCN01BegHkT1M+8j/8jmaFWIMU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736681928; c=relaxed/simple; bh=dXZN9OYzbOqKIu9QHhS7XF5V8OQ7EsPNsgPzpvt6EcE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=T/X0CPsUNrarm58v1I8vwJbrvZUm3QS6IWj6sswb7z0Fwz3NA6d5HhgCUqSFXVfHZl0q8JA/7Zup/2uGtH4N+yZpg6z1r5DuFtejncL+yptDy1OgQQFWV2TPk3/q9nRMUkrtFvScHJlr/Cd7jZW+AFFCiUdLL8eW60ZsBK6HLQc= 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=gDF4AP8l; arc=none smtp.client-ip=209.85.214.177 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="gDF4AP8l" Received: by mail-pl1-f177.google.com with SMTP id d9443c01a7336-21661be2c2dso54955765ad.1; Sun, 12 Jan 2025 03:38:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736681926; x=1737286726; 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=iWfVbd6VYo+kf/usUvGD0JCme8UsGRycx3bttXHiKcU=; b=gDF4AP8l7AMcVZ6qqhx3mmQlpJudU97iZFZs5jlQOAe396OGKg1kCR+9Ox9DZe9UOU SScxCZCj3ckXgDlYsm21HdYIYyOVNL5KyoF3ljlgJMOWhNqOfpJB4IzqobeWjd60AnGi OSyaK0XxTVD80n+wwnO/1jDDB5dTIWwOaxp0ZyquguDj7CHMFW4wgubAZCoN6VFRX36d tgevY3TKLrY+UEZLqWrIfuZvOXZbV3ZF1WahdSMj9tzR0FlsOkyxgX6S93yIp8UuYVr+ sCDQrxOZ3cvGPfil8dr+w2Gw5dIKzy85CNU+ViMzBc7CWezHieCfRlq3KOn6+2UF+EsC wYwg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736681926; x=1737286726; 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=iWfVbd6VYo+kf/usUvGD0JCme8UsGRycx3bttXHiKcU=; b=myFbD4Oyy6uwEL05Fq67E7WcMVQZ5wRC8euYW8SO2fLo1lrOyB1qbZbhMspL5hQ/A/ ESF2q7gQ5/ZS9GTw8H3ZlcrPpKzjZfygHUqqFwCX/tONgVvxjCE7lcQ+q+QnlKrrN+IB gZjMEheTQ3qqHdjWmzYFgX/miT06e9v1XdGFgobC17YsR9EPqN+ZYT/Y2IucqIjhucKO 0/2YSRM14ZAfYKUjre9t3VFl+ZGEv1M4ehgbxXei62qalhN+bQZkU44YClfl02s/WYCR HT+AvZjPaAqwpJtKuF7ZvGSxf0xlnrPXXFjF1GtuA/bzzm874quEZQQW//v1w6C+pGEN Wstw== X-Forwarded-Encrypted: i=1; AJvYcCVui8OWPgYqs54IGqBEFWO6OCatygGrm4h9K1Z2zDqP8vlw0gNWVJlNR3kD9RXHBtRDrZrn6B4=@vger.kernel.org X-Gm-Message-State: AOJu0Yza9zYCSBxlFNYuLJJkqCxmshpBKCZSiAZScilCW7SgrgnbAAvU 5x6+hJZTGGHupGWoV1ZaS8R0BRfMYQlmY51wUtGqTara8yvxlHMVGcJSiEc7 X-Gm-Gg: ASbGnctO54O/BQurkbcAW5m12XJq6SS2B/tnf89bDQUXUz/Gg90CTmSsUtZX1BUenFa Ku9D2OvZ5/1f71RE4kArG3FsD/TDRgZflhmJeRs4UdVFYWHppjAfWntXjPnWQ9eicVj2ik+XqQQ 5GZZWcIEOQ6KztOg/cxOaLgUF2gDpbn3Gc4ex2zKCJ45cVwmtfks+GnZSYwYDukjqXTuj8cSklV kY6z9OhYG1uQMeWiDofDg70Kuu44qzGPv8Zixpw7YAONyARNe17hveAsL+6r9gFuQ7JT9wgWB2a zfuLQGnDmfdM5BnxLSo= X-Google-Smtp-Source: AGHT+IGTNAZiHncEpO5/cQkLzUZ1ckLPMQUcokupwzURAEKXgVgjUjvKIM+MOepLQS9zUccntHpiDg== X-Received: by 2002:a17:902:d488:b0:215:4394:40b5 with SMTP id d9443c01a7336-21a83fd2700mr283552695ad.43.1736681926540; Sun, 12 Jan 2025 03:38:46 -0800 (PST) Received: from KERNELXING-MC1.tencent.com ([111.201.29.174]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21a9f253a98sm37353765ad.224.2025.01.12.03.38.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 12 Jan 2025 03:38:46 -0800 (PST) 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, horms@kernel.org Cc: bpf@vger.kernel.org, netdev@vger.kernel.org, Jason Xing Subject: [PATCH net-next v5 09/15] net-timestamp: support SCM_TSTAMP_ACK for bpf extension Date: Sun, 12 Jan 2025 19:37:42 +0800 Message-Id: <20250112113748.73504-10-kerneljasonxing@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20250112113748.73504-1-kerneljasonxing@gmail.com> References: <20250112113748.73504-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 Handle the ACK timestamp case. Actually testing SKBTX_BPF flag can work, but we need to Introduce a new txstamp_ack_bpf to avoid cache line misses in tcp_ack_tstamp(). To be more specific, in most cases, normal flows would not access skb_shinfo as txstamp_ack is zero, so that this function won't appear in the hot spot lists. Introducing a new member txstamp_ack_bpf works similarly. Signed-off-by: Jason Xing --- include/net/tcp.h | 3 ++- include/uapi/linux/bpf.h | 5 +++++ net/core/skbuff.c | 3 +++ net/ipv4/tcp_input.c | 3 ++- net/ipv4/tcp_output.c | 5 +++++ tools/include/uapi/linux/bpf.h | 5 +++++ 6 files changed, 22 insertions(+), 2 deletions(-) diff --git a/include/net/tcp.h b/include/net/tcp.h index 5b2b04835688..f00a8e3f9b31 100644 --- a/include/net/tcp.h +++ b/include/net/tcp.h @@ -959,9 +959,10 @@ struct tcp_skb_cb { __u8 sacked; /* State flags for SACK. */ __u8 ip_dsfield; /* IPv4 tos or IPv6 dsfield */ __u8 txstamp_ack:1, /* Record TX timestamp for ack? */ + txstamp_ack_bpf:1, /* ack timestamp for bpf use */ eor:1, /* Is skb MSG_EOR marked? */ has_rxtstamp:1, /* SKB has a RX timestamp */ - unused:5; + unused:4; __u32 ack_seq; /* Sequence number ACK'd */ union { struct { diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index a6d761f07f67..a0aff1b4eb61 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -7032,6 +7032,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 0fb31df4ed95..17b9d8061f04 100644 --- a/net/core/skbuff.c +++ b/net/core/skbuff.c @@ -5581,6 +5581,9 @@ static void __skb_tstamp_tx_bpf(struct sk_buff *skb, struct sock *sk, int tstype case SCM_TSTAMP_SND: op = BPF_SOCK_OPS_TS_SW_OPT_CB; break; + case SCM_TSTAMP_ACK: + op = BPF_SOCK_OPS_TS_ACK_OPT_CB; + break; default: return; } diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c index c2cdd0acb504..0f2e6e73de9f 100644 --- a/net/ipv4/tcp_input.c +++ b/net/ipv4/tcp_input.c @@ -3323,7 +3323,8 @@ static void tcp_ack_tstamp(struct sock *sk, struct sk_buff *skb, const struct skb_shared_info *shinfo; /* Avoid cache line misses to get skb_shinfo() and shinfo->tx_flags */ - if (likely(!TCP_SKB_CB(skb)->txstamp_ack)) + if (likely(!TCP_SKB_CB(skb)->txstamp_ack && + !TCP_SKB_CB(skb)->txstamp_ack_bpf)) return; shinfo = skb_shinfo(skb); diff --git a/net/ipv4/tcp_output.c b/net/ipv4/tcp_output.c index 7b4d1dfd57d4..aa1da7c89383 100644 --- a/net/ipv4/tcp_output.c +++ b/net/ipv4/tcp_output.c @@ -1556,6 +1556,7 @@ static void tcp_adjust_pcount(struct sock *sk, const struct sk_buff *skb, int de static bool tcp_has_tx_tstamp(const struct sk_buff *skb) { return TCP_SKB_CB(skb)->txstamp_ack || + TCP_SKB_CB(skb)->txstamp_ack_bpf || (skb_shinfo(skb)->tx_flags & SKBTX_ANY_TSTAMP); } @@ -1572,7 +1573,9 @@ static void tcp_fragment_tstamp(struct sk_buff *skb, struct sk_buff *skb2) shinfo2->tx_flags |= tsflags; swap(shinfo->tskey, shinfo2->tskey); TCP_SKB_CB(skb2)->txstamp_ack = TCP_SKB_CB(skb)->txstamp_ack; + TCP_SKB_CB(skb2)->txstamp_ack_bpf = TCP_SKB_CB(skb)->txstamp_ack_bpf; TCP_SKB_CB(skb)->txstamp_ack = 0; + TCP_SKB_CB(skb)->txstamp_ack_bpf = 0; } } @@ -3213,6 +3216,8 @@ void tcp_skb_collapse_tstamp(struct sk_buff *skb, shinfo->tskey = next_shinfo->tskey; TCP_SKB_CB(skb)->txstamp_ack |= TCP_SKB_CB(next_skb)->txstamp_ack; + TCP_SKB_CB(skb)->txstamp_ack_bpf |= + TCP_SKB_CB(next_skb)->txstamp_ack_bpf; } } diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index 73fc0a95c9ca..0fe7d663a244 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -7025,6 +7025,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 Sun Jan 12 11:37:43 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Xing X-Patchwork-Id: 13936322 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 336DB1AF0BA; Sun, 12 Jan 2025 11:38:52 +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=1736681934; cv=none; b=ZAH2LnceMxjJ5J9GN6lUnccFldSBuEdfwEypDORoLomSTavF3H616+We+Uc/n49sUEoI7VZaEPjAR3zaxIMDSNu4Jc6zNCsXxoShMD5FVaiTJAGRM1DN+7BG82vjbAljU9k2/9iScEvY5OelZxlvDUvwf17T53TsnXxEih7o1cU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736681934; c=relaxed/simple; bh=5HWKNWNgHZkoYZJi0DE7QoeYQsWYf/i2DNgj7lr+MIw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Oyn2gposIyRM7X0ZUteBH1k2BiNoJB3dfXCtxkKgXzXBl7EfbqRrNDetiXpOJoiJtQgcmmnjCaj2+2KZi59HahaOwI8bXsuEaf7LJ9VyJWDhu9uW+rRK30I/crzv/yQ5dvzYQDsX8bZwAdWOOqpI4A2R0DoMexCvjsZ/kqieYnA= 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=FxS7MJVn; 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="FxS7MJVn" Received: by mail-pl1-f182.google.com with SMTP id d9443c01a7336-216281bc30fso69447095ad.0; Sun, 12 Jan 2025 03:38:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736681932; x=1737286732; 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=Lg3LyyWwzGuTKgZbU2qb3KZUFeqEKmzFTLkXqDFWKec=; b=FxS7MJVnyH+u3ZfU3pXm2YqIREEch1xIF6ZVelIu7eWfABom5+C14wRf1Gn2ueudk2 fU0hatMq1G504JhinHei1SbgBxsoa4l9MhMpWSF8zkPnU9fRKppoWcCl5+krtUFjPs+N VEobJNlf4HkqhlOmq7j+jeIpJrzkNuf0yuYhj8QfYm8kiA2LFEripnXVa1f64D1Wg6Kr 2IJ3keKnyzF87OeTWCU8SZp1ynazrL/5Z7YYBdmiDVDsrZs14FBuluuwb3hxz8XQCL7W 1FZdnG7lz4iho28kyfrs+ZPJOnOM6RDrY2nmtD6tOjNIoy9h4evbopzFg1nyjF7q0Cxz 5HGw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736681932; x=1737286732; 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=Lg3LyyWwzGuTKgZbU2qb3KZUFeqEKmzFTLkXqDFWKec=; b=OOOOgF5h4jEtcQTHq/RG/ICQtgG4slqvts4TEHAEWwsScCfcriCszPpP5WF9Q8/lw4 npPYP4SBUOACP+ItAaZrLBaNW6z/xc/dgRvRAbDYkLY6nfVesquPtzhg/2aMuEnpCqrI FttLlK8vhcU+2gZCsFa5XL31ymQoSOIvExoyzdX213pQR65Q9ZVeS8/C4JXGSbXt2q7r PrywsZnOfE70sYEhhhLZeXyiRg4G3qZNOh2qGML7NsSum4kG6dhIbM+z7wpm2AxMvoB0 e5ubuw4R6rOXtXe/1ISQBFl7lSj8NS9YhbQLm0j1H2isnDfnYknJ3MIV9h+/6Tl2bEqn IwZg== X-Forwarded-Encrypted: i=1; AJvYcCUqshjqcbmd0zAtwhZl47p7t8+fMeZPkFPiGpNCOiCqRbslVgLtmc+4Nc0BHyh41Onx7W6wTkI=@vger.kernel.org X-Gm-Message-State: AOJu0YznfLrjycZH3PLmYBV8Ccv1poihtAj+szeWpu5kk6yjwIb0csbS /7Om0ChseFudjyirf/SEGKG7GVirdfdbtI+iDF93dBA5lQA7zOKQ X-Gm-Gg: ASbGncuWoHc6sM0iv9leidOIWdDBZW64FJih2k73gCtKo5U+q68FAlvXE0dXB0BDoyu Yq4ip0P3f9zc5h1kEZYi070se9Hf+AWht8fPQAYqTb63R8rlQTxfkDtkmLmH8CutgYmg/Z+Q27N fcu1G3c2pk/DT/xXK803Fln+2M0JiIOfZGsuxIsNEyLz4NFaBfhyxAZiftAoSssnkqyMm2DsvN6 HNWmSNFp7rHK97Y9fEfRsmWAymI5l1u29i+VzSZUsO0XxTBOFj2wsr/y0tVTRmCR4iWOswg/ub0 ZiO15qnf+96doaRnGGg= X-Google-Smtp-Source: AGHT+IHBvOQsLwQ7wA3eT//puYcIHB+YgHgTSSxHE3DUTZaoAnYzpMxFh/JWktsZTojy+RBzRFsJCg== X-Received: by 2002:a17:903:230d:b0:216:2e6d:babd with SMTP id d9443c01a7336-21a83f502a0mr273920945ad.15.1736681932156; Sun, 12 Jan 2025 03:38:52 -0800 (PST) Received: from KERNELXING-MC1.tencent.com ([111.201.29.174]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21a9f253a98sm37353765ad.224.2025.01.12.03.38.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 12 Jan 2025 03:38:51 -0800 (PST) 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, horms@kernel.org Cc: bpf@vger.kernel.org, netdev@vger.kernel.org, Jason Xing Subject: [PATCH net-next v5 10/15] net-timestamp: support hw SCM_TSTAMP_SND for bpf extension Date: Sun, 12 Jan 2025 19:37:43 +0800 Message-Id: <20250112113748.73504-11-kerneljasonxing@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20250112113748.73504-1-kerneljasonxing@gmail.com> References: <20250112113748.73504-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 To avoid changing so many callers using SKBTX_HW_TSTAMP from drivers, use this simple modification like this patch does to support printing hardware timestamp. Signed-off-by: Jason Xing --- include/linux/skbuff.h | 4 +++- net/core/skbuff.c | 2 +- net/socket.c | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/include/linux/skbuff.h b/include/linux/skbuff.h index 4f38c17c67a7..d3ef8db94a94 100644 --- a/include/linux/skbuff.h +++ b/include/linux/skbuff.h @@ -470,7 +470,7 @@ struct skb_shared_hwtstamps { /* Definitions for tx_flags in struct skb_shared_info */ enum { /* generate hardware time stamp */ - SKBTX_HW_TSTAMP = 1 << 0, + __SKBTX_HW_TSTAMP = 1 << 0, /* generate software time stamp when queueing packet to NIC */ SKBTX_SW_TSTAMP = 1 << 1, @@ -494,6 +494,8 @@ enum { SKBTX_BPF = 1 << 7, }; +#define SKBTX_HW_TSTAMP (__SKBTX_HW_TSTAMP | SKBTX_BPF) + #define SKBTX_ANY_SW_TSTAMP (SKBTX_SW_TSTAMP | \ SKBTX_SCHED_TSTAMP | \ SKBTX_BPF) diff --git a/net/core/skbuff.c b/net/core/skbuff.c index 17b9d8061f04..4bc7a424eb8a 100644 --- a/net/core/skbuff.c +++ b/net/core/skbuff.c @@ -5551,7 +5551,7 @@ static bool skb_enable_app_tstamp(struct sk_buff *skb, int tstype, bool sw) if (sw) flag = SKBTX_SW_TSTAMP; else - flag = SKBTX_HW_TSTAMP; + flag = __SKBTX_HW_TSTAMP; break; case SCM_TSTAMP_ACK: if (TCP_SKB_CB(skb)->txstamp_ack) diff --git a/net/socket.c b/net/socket.c index 4afe31656a2b..57343341bfb6 100644 --- a/net/socket.c +++ b/net/socket.c @@ -676,7 +676,7 @@ void __sock_tx_timestamp(__u32 tsflags, __u8 *tx_flags) u8 flags = *tx_flags; if (tsflags & SOF_TIMESTAMPING_TX_HARDWARE) { - flags |= SKBTX_HW_TSTAMP; + flags |= __SKBTX_HW_TSTAMP; /* PTP hardware clocks can provide a free running cycle counter * as a time base for virtual clocks. Tell driver to use the From patchwork Sun Jan 12 11:37:44 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Xing X-Patchwork-Id: 13936323 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 B0B721B0F04; Sun, 12 Jan 2025 11:38:58 +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=1736681940; cv=none; b=BPfqD1ykqD7yGK0HLAvtM/Hx1hVOR8vCu2zr1II9JsRWJe5L6SCINxfp8LttZN7bdeBtSzFioidteos+i1ATEybVWbfVgILhCakJt+qn5dSTKFay+f8KxwF010h6q1jRFeAu2q4W68Qid2ZiEZJWoMic6bE/xqILSqg936Of3sw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736681940; c=relaxed/simple; bh=l0HwOE6TA6jWDZ53BRGT8mO7j3+7n0GEQvnNRfFQObo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=U4qosKv0FHKva2EGqtr7asN+7wWxwGXJiGSnxh//+sNa7p9sbgzMsrE5vdnljO1rZ4eh8hzOah6US9MVKWPTgXG8rGteeOfHvqFug7kVB3sXue5LN+GBrnK9tjYubzoI8XUiHB8ttTYkhrD17crVC+26jHzynb1mr1UoaFkmt0Q= 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=nXgSeHGP; 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="nXgSeHGP" Received: by mail-pl1-f173.google.com with SMTP id d9443c01a7336-21636268e43so77525945ad.2; Sun, 12 Jan 2025 03:38:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736681938; x=1737286738; 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=Z6Ff54bFoCS8x70EOGd3R+aVHVP1OimJz7qjsT1++RA=; b=nXgSeHGPLpcYancyWIr9+DXI1PHxPT+kEb/qUyA0+4d7r0T/1vNT1v3cqE6TEP07B/ tK1uE9CRlGBn/NRB3zXx732e9ZPjQWJx70zVjir36jgj5oe/UjORQZZxpFnP775wKxHt wCH1s+q5xfTh0drfiihpfhBDJNW0HvPyCRzhmMPRubaeUO2YT3zGAFwckNocJD8ocO0P jedj9vMy0eECsaegIoFs434XDENzV1dS954b2SS0emK4hl0NRUP3MFzQ/6+cnRkj6a+J bPGcyoSTDmPJyNoJLXwRsUqglfc2FRQ6siPTJNQr/H/ns5Xq8YPIuwANTBRydx69/EK+ sFgw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736681938; x=1737286738; 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=Z6Ff54bFoCS8x70EOGd3R+aVHVP1OimJz7qjsT1++RA=; b=QoJRkesPM1X6JjqybZYYs7TUbu4GbRQS/UeAFWRLcJChvP64V5SyKKkPZheSvNisCx Lmb3l76NIuhPMbnEEez3A/p3rbaFMFsLRxRc4+1I8KX5KDH2AiL2w/2IJNiZgNRNtIHc YK+PlXpvvm+JUWf17yKpIRBUGDj1gErqFcjAH89iV82Z/PQ+bPLAnYHUTyRjdiwZl3XW 1p8zn+a3+HOrhn9Hghyx8IcdtgSCIe9AyPL9umlhxAKudF8V77DUnE0M1sJJlwHN2bQz zriKMBSS3BT/K/1Zfy9hS2t3gnbHWC8zuj9Wcf0MDnb7+uBYEXB6VTjiuHFyvr4hiK5X iHeA== X-Forwarded-Encrypted: i=1; AJvYcCV3bK0ViyrYvx+TT8tGWScUzN5Ng+BAm6YXdebr/sgUSlokt9L/kwcI3y0/R670WIFwEg/nLBg=@vger.kernel.org X-Gm-Message-State: AOJu0YyaFqUhG4k9dc0Pn6PZ4NT8qjJdDIT9J2/37JWExWFkwuHS1Qgu u43TmojZf6+ATHKnsXCNWa34BVewifk5YKULQVd8qVLdzvKmxb9yZq+Fq/MI X-Gm-Gg: ASbGnctoiTWr37EOeMEK/FkFY9sJRdlyYo3VyonfoJnazjMfs5C4ZeUikxTyrWI46kx vNNLtVFvxDBFfmAIL+ne1oDLE2fVUnd/lrzBqSQlBiV761ItX0hxnI3OexmMHnBjhYGWQfG9A9q cC33Jg9Hijp5xS+TV7REecz5ujexMmJEnd+McdOWINhiKY1h8oTmhRrreG9s9ZBMEODYRnt1+Fw XHeFr7cFFIC15VCZa6vPCTOfVA27RbJ7+BvWb+VSaAcljYzyi9r4QBWxafJB1Hc2lpS+dlZrGDX VpDioYT8EkqRe839Uy0= X-Google-Smtp-Source: AGHT+IFHxfDRw5UTDYrd265TKY9WUTby7ZQmi8Z11rkFC9AktvRhKkh9JMGe0aimuD+Ktt+hg1SBaw== X-Received: by 2002:a17:902:dac6:b0:216:4a8a:2665 with SMTP id d9443c01a7336-21a84012a17mr257867615ad.50.1736681938120; Sun, 12 Jan 2025 03:38:58 -0800 (PST) Received: from KERNELXING-MC1.tencent.com ([111.201.29.174]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21a9f253a98sm37353765ad.224.2025.01.12.03.38.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 12 Jan 2025 03:38:57 -0800 (PST) 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, horms@kernel.org Cc: bpf@vger.kernel.org, netdev@vger.kernel.org, Jason Xing Subject: [PATCH net-next v5 11/15] net-timestamp: support export skb to the userspace Date: Sun, 12 Jan 2025 19:37:44 +0800 Message-Id: <20250112113748.73504-12-kerneljasonxing@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20250112113748.73504-1-kerneljasonxing@gmail.com> References: <20250112113748.73504-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 People can follow these three steps as below to fetch the shared info from the exported skb in the bpf prog: 1. skops_kern = bpf_cast_to_kern_ctx(skops); 2. skb = skops_kern->skb; 3. shinfo = bpf_core_cast(skb->head + skb->end, struct skb_shared_info); It's worth to highlight we will be able to fetch the hwstamp, tskey and more key information extracted from the skb. More details can be seen in the last selftest patch of the series. Signed-off-by: Jason Xing --- net/core/sock.c | 1 + 1 file changed, 1 insertion(+) diff --git a/net/core/sock.c b/net/core/sock.c index dbb9326ae9d1..2f54e60a50d4 100644 --- a/net/core/sock.c +++ b/net/core/sock.c @@ -958,6 +958,7 @@ void bpf_skops_tx_timestamping(struct sock *sk, struct sk_buff *skb, int op) if (sk_is_tcp(sk) && sk_fullsock(sk)) sock_ops.is_fullsock = 1; sock_ops.sk = sk; + bpf_skops_init_skb(&sock_ops, skb, 0); sock_ops.timestamp_used = 1; __cgroup_bpf_run_filter_sock_ops(sk, &sock_ops, CGROUP_SOCK_OPS); } From patchwork Sun Jan 12 11:37:45 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Xing X-Patchwork-Id: 13936324 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-pj1-f46.google.com (mail-pj1-f46.google.com [209.85.216.46]) (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 CAB9C1ACECA; Sun, 12 Jan 2025 11:39:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.46 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736681946; cv=none; b=SQQqLhjBcZt3B3iJ48D8gtneHJ1qAXxMNJ7oEvy6IhZKRAQqy7oKI6DtFyqNqDh0mAHMZswxjiQJWVIqrOauOq6BD/ivEVx1DCwk7aDMPN/7hj2rJvV+Ls8hpYk9W4q7MJsRABrgc6BZF7iLUihZ/7ejo0GKZktQ+lF6qHeSCQk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736681946; c=relaxed/simple; bh=qgLgc+vtWhxd3wDw4kFYbDMlFJVL1MhDz8zW3gWKabs=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=JpHwW/oM/OuWntvizq6uY+DRC21ujbDte1l8N8x+ThM8Mj4Fi/KHGYa33WmF/nLLpU4MHKH18JEg/ICxGWLbk+KcHo/y/7fNWzMcDBJVXZZ6zHuLi1A/DieoivVFtvZjv6wXtqmdQEmjtODulhbRLPAAdzaooXQbaGyntt+x1vA= 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=Lul8ypMW; arc=none smtp.client-ip=209.85.216.46 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="Lul8ypMW" Received: by mail-pj1-f46.google.com with SMTP id 98e67ed59e1d1-2ee86a1a92dso4720059a91.1; Sun, 12 Jan 2025 03:39:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736681944; x=1737286744; 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=KivH3GOTURvpBe4gz30uCxSbbTX4Z4qxExfXxl7mwmc=; b=Lul8ypMWkSkN2JnPYsS6lRqo3wYiXxNVA5OuSqA8GncPLzRmU+cBBS5xffOMbczE7j G+xlwimrtigCazP4xOS4u++W//yieg88Z06SRC6o10gighCPevcyDBVvq/mDF4gLHgd1 cDeqge1Lr+5nuv9OxT7L45gyRLuJ6q0f4i2nCAVCTgo+0AIbae7ytzIK4UvDuV+LlupZ TAYZYgdrfvvz01OdbZFkjX6UrFENQlt1bwf1hPH6zZgrUdGwum8LG6y83MCsvnjUD1Ty w9irX1t6yJzPm4ArY0EFwesgdy8gYRlo2haGs7ndhwzDZGGQU5rFGrn8HwCY7PXR3uGo o0qQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736681944; x=1737286744; 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=KivH3GOTURvpBe4gz30uCxSbbTX4Z4qxExfXxl7mwmc=; b=f2gWO/0uqH3xr7oYhONH/WbF5pEHGOg8d49FjZGftwAAOeTLX9W3v7+A5WXFVQT1VS qN4lldQRakBmi8kCgOmfyQNErwsBOLWux2NUgCfaYHTV77+zv5i4FDyqqb6Eu2IHrc/0 iAo0JeANpjUmPTC5meG1dMdPAt3cqPpFDR2SYEiOPrPdVFUjLlcwx544bt3UCZ74R/sd HKXcARWeDANxePHeP4TE7wlVu09PTrexiTg4t9aLmwrbTjBqqRAyVlWwsxXIoMsAQOAB TKPnrlfsZJld152TukUG67+13sXYlLlm/sc7gvEPJtzxJDmuBhYU1TiYVlXzT6r5PaSL iHPA== X-Forwarded-Encrypted: i=1; AJvYcCUZGW9hfRvnNO2McNubdPxqT0ty7C1w4ojWL08duG0cHvKOGb03WVp8GkyXWGP/cHplBWkZiaE=@vger.kernel.org X-Gm-Message-State: AOJu0YwwgDudDctECF3e2jd1vj6E3lP+8RJTHFiu9T4XpQcJTojvAcRU oJSX4gd4Y88lhZje59Mcw8cfnxZU6l/7v88DG6HF2bi6Gd6ldGRB X-Gm-Gg: ASbGncsPysglBialFD0mf9wECjLy0AmPZh8HONEi8BWh9oogkeRLKBFZ/gHovIENmhY UAPfr1O2W4OUBOp7NF5MHh8WhO+YNCi7Hxyx+H0rfqUFJizW1zUSdtqjC2tNMLkXZiBc03+uUpA 3q60IPUWRmwfLEfGmOzi9P/ze14qU/kDOGIuOx9WGvUjBcg5KuPgRWLZvUeDyYrySKPmkAZv/qI I40it75cycaE++fV4c8KSZb6ln6OfBSZQBvSTRllvdECXSyROsqTv2vWOtGo7ZjagBdqZjTRoxW Bk2PP5pqrSOGYw2mHsA= X-Google-Smtp-Source: AGHT+IFlBE2of8yhL+90szVdy1s0kLksAdfi3OnOceLTRUnttsVmeqA/MREdA1M/UiW6VaUsMcRs7Q== X-Received: by 2002:a17:90a:dfcb:b0:2ee:863e:9ff6 with SMTP id 98e67ed59e1d1-2f548eae2ecmr27094390a91.16.1736681944030; Sun, 12 Jan 2025 03:39:04 -0800 (PST) Received: from KERNELXING-MC1.tencent.com ([111.201.29.174]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21a9f253a98sm37353765ad.224.2025.01.12.03.38.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 12 Jan 2025 03:39:03 -0800 (PST) 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, horms@kernel.org Cc: bpf@vger.kernel.org, netdev@vger.kernel.org, Jason Xing Subject: [PATCH net-next v5 12/15] net-timestamp: make TCP tx timestamp bpf extension work Date: Sun, 12 Jan 2025 19:37:45 +0800 Message-Id: <20250112113748.73504-13-kerneljasonxing@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20250112113748.73504-1-kerneljasonxing@gmail.com> References: <20250112113748.73504-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 Make partial of the feature work finally. After this, user can fully use the bpf prog to trace the tx path for TCP type. Signed-off-by: Jason Xing --- net/ipv4/tcp.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c index 0d704bda6c41..0a41006b10d1 100644 --- a/net/ipv4/tcp.c +++ b/net/ipv4/tcp.c @@ -492,6 +492,15 @@ static void tcp_tx_timestamp(struct sock *sk, struct sockcm_cookie *sockc) if (tsflags & SOF_TIMESTAMPING_TX_RECORD_MASK) shinfo->tskey = TCP_SKB_CB(skb)->seq + skb->len - 1; } + + if (SK_BPF_CB_FLAG_TEST(sk, SK_BPF_CB_TX_TIMESTAMPING) && skb) { + struct skb_shared_info *shinfo = skb_shinfo(skb); + struct tcp_skb_cb *tcb = TCP_SKB_CB(skb); + + tcb->txstamp_ack_bpf = 1; + shinfo->tx_flags |= SKBTX_BPF; + shinfo->tskey = TCP_SKB_CB(skb)->seq + skb->len - 1; + } } static bool tcp_stream_is_readable(struct sock *sk, int target) From patchwork Sun Jan 12 11:37:46 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Xing X-Patchwork-Id: 13936325 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 556F71ACECA; Sun, 12 Jan 2025 11:39:10 +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=1736681951; cv=none; b=oktZhz9uC/+MZJD+p6mXyRPUJiH8VAd4YJkfrt3KrFf4VmHUOlihMjLKox/h5BSaBmsNO/tGX9SG1gSAUJuhzXf7GG9ghIbN+fEcdT7XT0i86yszGdSPgQte1iHjhu6zAG/4ntKHZRaFotOrL5Hq7dYQaRLrjlqjC7k4EUJHy9E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736681951; c=relaxed/simple; bh=LwCw6WpEg/QEKA3nZCYhr2XUsxdpeWC+8nm7yYVCRxs=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=tICEFomGRJf8KKd+LCrqzrqKr9hPlr/6hAj1MMoBCMAT/bSUjizGVLHc3RoiKbHuXo/yImyrMuRv7G+FeOCDq8X8zoxP3EX/7WC3OrwL1Uj4v9PlsJ75xFIX0J8+kwBGsUnzoys2arx9fYuYVy/Xkjct0RY3mSFgoaV9YPlkegg= 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=Q67H8ejr; 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="Q67H8ejr" Received: by mail-pl1-f181.google.com with SMTP id d9443c01a7336-216634dd574so37777705ad.2; Sun, 12 Jan 2025 03:39:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736681949; x=1737286749; 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=E5BuJ/CgfMGnhCq877MungxJulK/ZfX46Mt9P4NHQK4=; b=Q67H8ejrGV0aVV7J3VELIkvIbbmbVIo5CoCF68dn4AC+xZ1dVUrfPFoII06kJwGWHH 5roZ6juTkrALhktyQl7+T0uOWY6lpxh7+g8hVNCjgJ5TRSRYpNk21eshaO7kmkfhuD94 I81EC0RGS8qfebdxe2yStwcQ9+ObtY6u7/nem8gxstGwkMg6HY3zk1LN4OSTk0OnslHb USnIlYR4ZEoHH1ny30EXq3lpZH2Osh2E9Afn8pWtgJFdv4z5c9p5rhZr8J21gSXgHAX/ YxO8rLUl29pjL7pWQiFuZN21wwSr4wp3RkeKEKSK09EDR8TbQr9qsT/rQ/BrwkYiwXMb rKxQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736681949; x=1737286749; 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=E5BuJ/CgfMGnhCq877MungxJulK/ZfX46Mt9P4NHQK4=; b=wn4lmWCQjMFLUUgFn9WHQzad4vXExxNugDKGIRMaSkRfy69YKw/Gb/Rar5W6IvY16V iV9p/0qqLXho59/UT2uqlvLycrY/QNeaHdOm/E52orGsAmjKM7J+J+SGYXu4DscWIE+1 l/WT6Z1iqQ1S80O0wq3M2JVRMsyjL4LjrhcceQDzqx7tEFR7FB3Ec1VSoev41pEmESKz AWEF6cXCSJxmXg9SpEnG3Wab2APjF8Cq5J3l3M8khtNItlMMoQyhU0w9K0xq6qqHIFE2 QB3wpt3BnPc1IZ0DEsKX0RrkYfLEZ+ZG0elH7w/tWecJI4IDvEpqWMyQeGwiRTPFcwPG xtrw== X-Forwarded-Encrypted: i=1; AJvYcCUZQUxeLbH1nDDS00GJK7HbJ9NzffPJxCJfECNmbAN/xnYuRnNOKlKjN4nRQ56ZdVGAe2Kf9zc=@vger.kernel.org X-Gm-Message-State: AOJu0Yz+l3VQXWaB0dO7ghMGDM6IMOu2GpNLQzyhdbt+aqyfK6aM1ZGE 9huAsxiQnUb9rcdofrsFr3mlN0rbfdcofcu8r49B/1EP18UjoM1C X-Gm-Gg: ASbGncv49VQqmF1cWhNUOseqQUJWJpsKJIMvQma2P/qwCbA74uK+ds2kGhfZzB7OU/v AeclRoU6Lf5VS/Mt3tNCk2CihWhKp70WyOgH5jT+mMZmb1t4D4qaWFEy/uPV0JrIEaQEPkVvcUk wrTtiOabvRyy1Sda5vd9AdRSAquGBIynPmsXP2HE+JrcG6vkO6bXGnnxc1MkNMYuoRfJUA7nmC6 E9sceJJqarazhlk2KdmvG0OiBHsd3xJxCwyzL6mTLUoIeF285HTf1k/07G8TYwnp9V45qI37DKJ yFbFfzCJd3fE61Jfm9c= X-Google-Smtp-Source: AGHT+IE0Tp/nkeQoAgxyOrKCqmlHOOVkx2bN3Y5izXGaS+n1h7frSEZjQ3iqwC2l7+Dq1Tz03RhONw== X-Received: by 2002:a17:902:ec84:b0:218:a4ea:a786 with SMTP id d9443c01a7336-21a8400b2a8mr273245095ad.53.1736681949401; Sun, 12 Jan 2025 03:39:09 -0800 (PST) Received: from KERNELXING-MC1.tencent.com ([111.201.29.174]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21a9f253a98sm37353765ad.224.2025.01.12.03.39.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 12 Jan 2025 03:39:08 -0800 (PST) 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, horms@kernel.org Cc: bpf@vger.kernel.org, netdev@vger.kernel.org, Jason Xing Subject: [PATCH net-next v5 13/15] net-timestamp: support tcp_sendmsg for bpf extension Date: Sun, 12 Jan 2025 19:37:46 +0800 Message-Id: <20250112113748.73504-14-kerneljasonxing@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20250112113748.73504-1-kerneljasonxing@gmail.com> References: <20250112113748.73504-1-kerneljasonxing@gmail.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org Introduce tskey_bpf to correlate tcp_sendmsg timestamp with other three points (SND/SW/ACK). More details can be found in the selftest. For TCP, tskey_bpf is used to store the initial write_seq value the moment tcp_sendmsg is called, so that the last skb of this call will have the same tskey_bpf with tcp_sendmsg bpf callback. UDP works similarly because tskey_bpf can increase by one everytime udp_sendmsg gets called. It will be implemented soon. Signed-off-by: Jason Xing --- include/linux/skbuff.h | 2 ++ include/uapi/linux/bpf.h | 3 +++ net/core/sock.c | 3 ++- net/ipv4/tcp.c | 10 ++++++++-- tools/include/uapi/linux/bpf.h | 3 +++ 5 files changed, 18 insertions(+), 3 deletions(-) diff --git a/include/linux/skbuff.h b/include/linux/skbuff.h index d3ef8db94a94..3b7b470d5d89 100644 --- a/include/linux/skbuff.h +++ b/include/linux/skbuff.h @@ -609,6 +609,8 @@ struct skb_shared_info { }; unsigned int gso_type; u32 tskey; + /* For TCP, it records the initial write_seq when sendmsg is called */ + u32 tskey_bpf; /* * Warning : all fields before dataref are cleared in __alloc_skb() diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index a0aff1b4eb61..87420c0f2235 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -7037,6 +7037,9 @@ enum { * feature is on. It indicates the * recorded timestamp. */ + BPF_SOCK_OPS_TS_TCP_SND_CB, /* Called when every tcp_sendmsg + * syscall is triggered + */ }; /* List of TCP states. There is a build check in net/ipv4/tcp.c to detect diff --git a/net/core/sock.c b/net/core/sock.c index 2f54e60a50d4..e74ab0e2979d 100644 --- a/net/core/sock.c +++ b/net/core/sock.c @@ -958,7 +958,8 @@ void bpf_skops_tx_timestamping(struct sock *sk, struct sk_buff *skb, int op) if (sk_is_tcp(sk) && sk_fullsock(sk)) sock_ops.is_fullsock = 1; sock_ops.sk = sk; - bpf_skops_init_skb(&sock_ops, skb, 0); + if (skb) + bpf_skops_init_skb(&sock_ops, skb, 0); sock_ops.timestamp_used = 1; __cgroup_bpf_run_filter_sock_ops(sk, &sock_ops, CGROUP_SOCK_OPS); } diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c index 0a41006b10d1..b6e0db5e4ead 100644 --- a/net/ipv4/tcp.c +++ b/net/ipv4/tcp.c @@ -477,7 +477,7 @@ void tcp_init_sock(struct sock *sk) } EXPORT_SYMBOL(tcp_init_sock); -static void tcp_tx_timestamp(struct sock *sk, struct sockcm_cookie *sockc) +static void tcp_tx_timestamp(struct sock *sk, struct sockcm_cookie *sockc, u32 first_write_seq) { struct sk_buff *skb = tcp_write_queue_tail(sk); u32 tsflags = sockc->tsflags; @@ -500,6 +500,7 @@ static void tcp_tx_timestamp(struct sock *sk, struct sockcm_cookie *sockc) tcb->txstamp_ack_bpf = 1; shinfo->tx_flags |= SKBTX_BPF; shinfo->tskey = TCP_SKB_CB(skb)->seq + skb->len - 1; + shinfo->tskey_bpf = first_write_seq; } } @@ -1067,10 +1068,15 @@ int tcp_sendmsg_locked(struct sock *sk, struct msghdr *msg, size_t size) int flags, err, copied = 0; int mss_now = 0, size_goal, copied_syn = 0; int process_backlog = 0; + u32 first_write_seq = 0; int zc = 0; long timeo; flags = msg->msg_flags; + if (SK_BPF_CB_FLAG_TEST(sk, SK_BPF_CB_TX_TIMESTAMPING)) { + first_write_seq = tp->write_seq; + bpf_skops_tx_timestamping(sk, NULL, BPF_SOCK_OPS_TS_TCP_SND_CB); + } if ((flags & MSG_ZEROCOPY) && size) { if (msg->msg_ubuf) { @@ -1331,7 +1337,7 @@ int tcp_sendmsg_locked(struct sock *sk, struct msghdr *msg, size_t size) out: if (copied) { - tcp_tx_timestamp(sk, &sockc); + tcp_tx_timestamp(sk, &sockc, first_write_seq); tcp_push(sk, flags, mss_now, tp->nonagle, size_goal); } out_nopush: diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index 0fe7d663a244..3769e38e052d 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -7030,6 +7030,9 @@ enum { * feature is on. It indicates the * recorded timestamp. */ + BPF_SOCK_OPS_TS_TCP_SND_CB, /* Called when every tcp_sendmsg + * syscall is triggered + */ }; /* List of TCP states. There is a build check in net/ipv4/tcp.c to detect From patchwork Sun Jan 12 11:37:47 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Xing X-Patchwork-Id: 13936326 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-pj1-f42.google.com (mail-pj1-f42.google.com [209.85.216.42]) (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 18B911A9B4E; Sun, 12 Jan 2025 11:39:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736681957; cv=none; b=bxBgpSF4NkDLTRnzLgLxsWfbJY+MZspPYR8C2xOh38kZwJTUerD/foY6JqRlzhLmj9/suQTDiLmByIr94p5KHI2AV/FdX0X4e0wuRZJuu6S/lbjKxC3nQNSQtXvXJfkae4R3NZaX/qX1Sp5twwxJWKkKdgNKM54dvW7FeIR0CsQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736681957; c=relaxed/simple; bh=D6eygZ0r58bPXo5cBNoylp6xEqCzVP7Hpv38DoQPzo0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=jpSxH9TQbn9R9/xGiym4ai3LgCfhJkDqDLCFRrDdtyhWB2EjHMxVXpCaM61Wh7XOH2B4Plfh8FQ38hWXTipZv6y5bpWaXrjj609VVP/I4JMARsP2H4Gmqkjj+yNJlbQhVkCELFVKoQOyzWW0kR6B7DWt07dUYPeepdOHIsoyADI= 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=VVR7mBSy; arc=none smtp.client-ip=209.85.216.42 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="VVR7mBSy" Received: by mail-pj1-f42.google.com with SMTP id 98e67ed59e1d1-2ef72924e53so5778918a91.3; Sun, 12 Jan 2025 03:39:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736681955; x=1737286755; 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=KqIIJkr1zLbPbBJUXgaDRdNWAJmvG4arTspiCJjcCiE=; b=VVR7mBSyaT5qhrdrS+qx1C1j3/0RgCqIRD9fCuf/D5WcqsxYgCvgHAkxhnmRRoRk/D R//j7OCOqk9coPcvYMOgSEYNwYQIk8YV6h7davSX9qbtOIaNsx4DhAnwzIi60oVOUtTy O3cj87yEQ5C2yNKWmWy5LirIswHyHdB05R5DEBW9YsXXx+53Z5kHlinRSisWV1jCWc6a Y/Vt50eFUztFNBulV61yXr1CJ35lo1LnwtKjzjSML481oFmokx8cCsCQf0uVXhYGHenK H35xlTbxEl6wREF5EItRXHQx3Q6DondMC3OCTcGwDH+MQI+k3htCPju+2F2FTAjuTSVU HU8A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736681955; x=1737286755; 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=KqIIJkr1zLbPbBJUXgaDRdNWAJmvG4arTspiCJjcCiE=; b=FyNccrMzMq8L4Ca/CsJKMB1eZqKarai/uMHLzjIedR+Ne4mzHmMlDGJ9yeNz/f4DT/ KTj0EHEYy559P1dOTIdNCh/haLcnogDWwnKqXdFUUqumhyXmbQrsDsAmYd1RgPwINOVF tEi4ghLYuW9IhWXequfxtZYipgJYVmiW3pCosjJkNgq02fl76o2YEWC/9EI0AJY5Qkwz bWczJk0Y6xc6t4BadogPVqMkhxGiKs+FCDmj+akKwxHvprspokob/HdPfnJLJ58g4ZRE SS9f/qckByl1rOT1/I9OSOB8RSsZnKcSH3E7sCcigGInQpJhGyYDdXp4nOiZqeImYvGY dk9w== X-Forwarded-Encrypted: i=1; AJvYcCUATAYRW3qeVt5GNPjqFvmNSDrJT1cF6DP5NohiajIsZXMDqBhhihV8KfuEccsSIE3z/6C4+fE=@vger.kernel.org X-Gm-Message-State: AOJu0YzNaGxe2QubFs8xZ3vVOje1oWyvYKgoetlEA7O2VP2mvI71Nn4t GREQo38v7Df6Y5/2mKhku/q+aDo942ymGpioZgHg/Hp/qrIA7ngy X-Gm-Gg: ASbGncvlljwuCN4HX1SO0meEmW0w0EHvQoo7fearwId2ibz1K0NK3ZHuYgjcyftg9RS noIxhlOLGR42ZkQC0k7Ck+gDlAFABJbasO4mlFXMQc93oAMlwqMjj5z7EzViiGeKnCbs5vUq2UJ o4/WCRRVlo42qPgF07+Bjcr1iT7VDoS3WdE35WnACWRp+qGgU8CQoSYrknP7OSrcr+7jiE3pKm0 xypvojgc8zYH2KdEMH2NB193bHefRKuwssXkRWfJ/fUXADP7R0/G4X16+yN7eYylahU2pPwF3vE psoBb6UA0xG2BhoTfhI= X-Google-Smtp-Source: AGHT+IFEbka9OrWPnD2OtSEWpcXSD2+HpwdP+NbCpFWZG85Kc8cxf3MaX185an3EARWUFYSG9kAQfQ== X-Received: by 2002:a17:90a:fc4f:b0:2ee:6d04:9dac with SMTP id 98e67ed59e1d1-2f548f7ed36mr21029876a91.32.1736681955520; Sun, 12 Jan 2025 03:39:15 -0800 (PST) Received: from KERNELXING-MC1.tencent.com ([111.201.29.174]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21a9f253a98sm37353765ad.224.2025.01.12.03.39.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 12 Jan 2025 03:39:15 -0800 (PST) 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, horms@kernel.org Cc: bpf@vger.kernel.org, netdev@vger.kernel.org, Jason Xing Subject: [PATCH net-next v5 14/15] net-timestamp: introduce cgroup lock to avoid affecting non-bpf cases Date: Sun, 12 Jan 2025 19:37:47 +0800 Message-Id: <20250112113748.73504-15-kerneljasonxing@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20250112113748.73504-1-kerneljasonxing@gmail.com> References: <20250112113748.73504-1-kerneljasonxing@gmail.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org Introducing the lock to avoid affecting the applications which are not using timestamping bpf feature. Signed-off-by: Jason Xing --- net/core/skbuff.c | 4 +++- net/ipv4/tcp.c | 6 ++++-- net/ipv4/tcp_input.c | 3 ++- net/ipv4/tcp_output.c | 3 ++- 4 files changed, 11 insertions(+), 5 deletions(-) diff --git a/net/core/skbuff.c b/net/core/skbuff.c index 4bc7a424eb8a..ce445e49ddc1 100644 --- a/net/core/skbuff.c +++ b/net/core/skbuff.c @@ -5602,7 +5602,9 @@ void __skb_tstamp_tx(struct sk_buff *orig_skb, if (!sk) return; - if (skb_shinfo(orig_skb)->tx_flags & SKBTX_BPF) + + if (cgroup_bpf_enabled(CGROUP_SOCK_OPS) && + skb_shinfo(orig_skb)->tx_flags & SKBTX_BPF) __skb_tstamp_tx_bpf(orig_skb, sk, tstype); if (!skb_enable_app_tstamp(orig_skb, tstype, sw)) diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c index b6e0db5e4ead..07326f56cc42 100644 --- a/net/ipv4/tcp.c +++ b/net/ipv4/tcp.c @@ -493,7 +493,8 @@ static void tcp_tx_timestamp(struct sock *sk, struct sockcm_cookie *sockc, u32 f shinfo->tskey = TCP_SKB_CB(skb)->seq + skb->len - 1; } - if (SK_BPF_CB_FLAG_TEST(sk, SK_BPF_CB_TX_TIMESTAMPING) && skb) { + if (cgroup_bpf_enabled(CGROUP_SOCK_OPS) && + SK_BPF_CB_FLAG_TEST(sk, SK_BPF_CB_TX_TIMESTAMPING) && skb) { struct skb_shared_info *shinfo = skb_shinfo(skb); struct tcp_skb_cb *tcb = TCP_SKB_CB(skb); @@ -1073,7 +1074,8 @@ int tcp_sendmsg_locked(struct sock *sk, struct msghdr *msg, size_t size) long timeo; flags = msg->msg_flags; - if (SK_BPF_CB_FLAG_TEST(sk, SK_BPF_CB_TX_TIMESTAMPING)) { + if (cgroup_bpf_enabled(CGROUP_SOCK_OPS) && + SK_BPF_CB_FLAG_TEST(sk, SK_BPF_CB_TX_TIMESTAMPING)) { first_write_seq = tp->write_seq; bpf_skops_tx_timestamping(sk, NULL, BPF_SOCK_OPS_TS_TCP_SND_CB); } diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c index 0f2e6e73de9f..5493bc911593 100644 --- a/net/ipv4/tcp_input.c +++ b/net/ipv4/tcp_input.c @@ -3324,7 +3324,8 @@ static void tcp_ack_tstamp(struct sock *sk, struct sk_buff *skb, /* Avoid cache line misses to get skb_shinfo() and shinfo->tx_flags */ if (likely(!TCP_SKB_CB(skb)->txstamp_ack && - !TCP_SKB_CB(skb)->txstamp_ack_bpf)) + !(cgroup_bpf_enabled(CGROUP_SOCK_OPS) && + TCP_SKB_CB(skb)->txstamp_ack_bpf))) return; shinfo = skb_shinfo(skb); diff --git a/net/ipv4/tcp_output.c b/net/ipv4/tcp_output.c index aa1da7c89383..2675540c4faf 100644 --- a/net/ipv4/tcp_output.c +++ b/net/ipv4/tcp_output.c @@ -1556,7 +1556,8 @@ static void tcp_adjust_pcount(struct sock *sk, const struct sk_buff *skb, int de static bool tcp_has_tx_tstamp(const struct sk_buff *skb) { return TCP_SKB_CB(skb)->txstamp_ack || - TCP_SKB_CB(skb)->txstamp_ack_bpf || + (cgroup_bpf_enabled(CGROUP_SOCK_OPS) && + TCP_SKB_CB(skb)->txstamp_ack_bpf) || (skb_shinfo(skb)->tx_flags & SKBTX_ANY_TSTAMP); } From patchwork Sun Jan 12 11:37:48 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Xing X-Patchwork-Id: 13936327 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 A79311ACECA; Sun, 12 Jan 2025 11:39:21 +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=1736681963; cv=none; b=rBfbQf+ZJMiTEi9GH28oyUXelgrtwDdg+fP7PtNQLRPBNBM+yo9BaebkWJ+bLz67wBiZM2YwZjJ6/oVCxAkPnGEFz5g27rL+cgfr1CdeQ1LzVvlMRlKrN9qsfpbA49iLf8adu9jxK1B8+moufWq8B6w//UvwLOFxfgf0DZpfekw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736681963; c=relaxed/simple; bh=qie5cqASySC/U0j2QNp25OG67rs+j7Wa6PDaYEkj/F0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ZXGbKucam8P/92H1y4BEWI1r0IMoZF5L8Xy8tn1MSTdhrLDzq7sLajIepiiGrRqML+szUBPCIhE4s0Wuj+gY5ytlTH5WOVgwfZQmY8T+Uyv5xsTmZuHdDXMRp1XCKid5Sa0acRJcWuQdxK2Af7t6jZx/uNf5E6jYXuWf8o+xxxY= 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=ZsTEQ0II; 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="ZsTEQ0II" Received: by mail-pl1-f182.google.com with SMTP id d9443c01a7336-216395e151bso41035435ad.0; Sun, 12 Jan 2025 03:39:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736681961; x=1737286761; 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=G58t1Ur4vm0+cBMS/y3fsRI/xjMWJbv6YU/kfITP+O4=; b=ZsTEQ0IIM0GX6OBM6rhxfjyRJXJXU+qRE0xarS6PAMdlbwrQNePADtNCRrYOLeftAO y5GG/Rbi7we9JrouufJAyXAh/LkD7lAzd4PfNgzDWKtSDw/qN2fNMH94JpIbtMyD89Oe 4spOePTu1CSNT/8rctHGOLNIJ50tSYxkVZeuR5fEwV7dWLEdocThG2hHOFAwrbxPuBsZ cWQCYJrpVabqeg84lN/TUTJMr/KgBVluRpPOtag2TVH0Lxomabua1Ooojo/W4dC1hZNO aRirdsOfyw5EgB8xAdPQT9H/VQ9LcqXpCdHGgB6IMMqeiYwkQQ+uILaogjDwkz1NV6e5 IJMA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736681961; x=1737286761; 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=G58t1Ur4vm0+cBMS/y3fsRI/xjMWJbv6YU/kfITP+O4=; b=CEQsOuY6r2tCUol5OewS5eTwZoWroBCbK7ZAqr7rIqmWXhngLolQkZjBYg48OtfEPe HZTQjxxoICitOnq6j9sb5emUI4u7cnFXoM49dgG4XbB3wQzb3QdhyCfMkX9p6zE2GEPI qSlsI3pRXMEvTQBs3RRbn/yI3nOICB6v1VyxAlLzxZN10kLEx3waOzsY6pf0TdIxKq3F 6EBwi/Bd/afO4ZPwbwqDv81XG+BUGORKnU2FZRz6XTmEmg3xeGFIy8Zg9mA/8GVzC9vy M4fd824Du7Z7k1GBTTJqEOf4FESrsAV0s1vi9kqkFgTIJBH7a00hDARtUrBWSHHQlf+M O02w== X-Forwarded-Encrypted: i=1; AJvYcCXJ22wHEDZu/oH3IgIO2YXMfC97DXdUiH4ayCAc/yOAWoHDz41fLfhQpaJk0f1xlqVRPVcdjcs=@vger.kernel.org X-Gm-Message-State: AOJu0YwRypKpnj2IOz6KYD2E2qONRdFDdMdDGWRBwnu/c9xZ3bagC8Uh xCOFjcBsCYj83LS5PZgejG1m/qQViCc1TmXrmmxEuePTZc2HAkMx X-Gm-Gg: ASbGncvaIjAXsm83wZunsxuLySlY/hsyuL6h0nCFHGtisZqLYufpuCDrxRtnz3m4F74 K7y9UoZFWMMw0VZrfX1aqVHYEXbaDAg33syRPd49pypduYKy5w0gzMN6pVKvgwzUsS1ZJeRX8L+ l2/9uXlNJyLBlb9QaqerEwOVVmjaXt/H0f6kiS3Lj/3TWcT6uafmyqcv25Fd5Y+U4dgyTm6onzg DpNLCC2AWMz2sBOax84XgTWjEftcodzaRsHTS+JqDZo/wdSiOAH3v3Q8SG/wue5RJRvppNXtkI/ KIcCpAo3BuSuE0mHn1g= X-Google-Smtp-Source: AGHT+IFlpVkANH+aE4mzVMrILualvAgNHHQdAQk43kMBp4xgnTaZvWrW6v+zbtUUDUm5oKZq15avfw== X-Received: by 2002:a17:903:1cd:b0:216:59f1:c7d9 with SMTP id d9443c01a7336-21ad9f95d63mr68263195ad.19.1736681961030; Sun, 12 Jan 2025 03:39:21 -0800 (PST) Received: from KERNELXING-MC1.tencent.com ([111.201.29.174]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21a9f253a98sm37353765ad.224.2025.01.12.03.39.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 12 Jan 2025 03:39:20 -0800 (PST) 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, horms@kernel.org Cc: bpf@vger.kernel.org, netdev@vger.kernel.org, Jason Xing Subject: [PATCH net-next v5 15/15] bpf: add simple bpf tests in the tx path for so_timestamping feature Date: Sun, 12 Jan 2025 19:37:48 +0800 Message-Id: <20250112113748.73504-16-kerneljasonxing@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20250112113748.73504-1-kerneljasonxing@gmail.com> References: <20250112113748.73504-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 Only check if we pass those three key points after we enable the bpf extension for so_timestamping. During each point, we can choose whether to print the current timestamp. Signed-off-by: Jason Xing --- .../bpf/prog_tests/so_timestamping.c | 95 +++++++++ .../selftests/bpf/progs/so_timestamping.c | 186 ++++++++++++++++++ 2 files changed, 281 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/so_timestamping.c create mode 100644 tools/testing/selftests/bpf/progs/so_timestamping.c diff --git a/tools/testing/selftests/bpf/prog_tests/so_timestamping.c b/tools/testing/selftests/bpf/prog_tests/so_timestamping.c new file mode 100644 index 000000000000..77c347701e50 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/so_timestamping.c @@ -0,0 +1,95 @@ +#define _GNU_SOURCE +#include +#include +#include +#include + +#include "test_progs.h" +#include "cgroup_helpers.h" +#include "network_helpers.h" + +#include "so_timestamping.skel.h" + +#define CG_NAME "/so-timestamping-test" + +static const char addr4_str[] = "127.0.0.1"; +static const char addr6_str[] = "::1"; +static struct so_timestamping *skel; +static int cg_fd; + +static int create_netns(void) +{ + if (!ASSERT_OK(unshare(CLONE_NEWNET), "create netns")) + return -1; + + if (!ASSERT_OK(system("ip link set dev lo up"), "set lo up")) + return -1; + + return 0; +} + +static void test_tcp(int family) +{ + struct so_timestamping__bss *bss = skel->bss; + char buf[] = "testing testing"; + int sfd = -1, cfd = -1; + int n; + + memset(bss, 0, sizeof(*bss)); + + sfd = start_server(family, SOCK_STREAM, + family == AF_INET6 ? addr6_str : addr4_str, 0, 0); + if (!ASSERT_GE(sfd, 0, "start_server")) + goto out; + + cfd = connect_to_fd(sfd, 0); + if (!ASSERT_GE(cfd, 0, "connect_to_fd_server")) { + close(sfd); + goto out; + } + + n = write(cfd, buf, sizeof(buf)); + if (!ASSERT_EQ(n, sizeof(buf), "send to server")) + goto out; + + ASSERT_EQ(bss->nr_active, 1, "nr_active"); + ASSERT_EQ(bss->nr_tcp, 1, "nr_tcp"); + ASSERT_EQ(bss->nr_sched, 1, "nr_sched"); + ASSERT_EQ(bss->nr_txsw, 1, "nr_txsw"); + ASSERT_EQ(bss->nr_ack, 1, "nr_ack"); + +out: + if (sfd >= 0) + close(sfd); + if (cfd >= 0) + close(cfd); +} + +void test_so_timestamping(void) +{ + cg_fd = test__join_cgroup(CG_NAME); + if (cg_fd < 0) + return; + + if (create_netns()) + goto done; + + skel = so_timestamping__open(); + if (!ASSERT_OK_PTR(skel, "open skel")) + goto done; + + if (!ASSERT_OK(so_timestamping__load(skel), "load skel")) + goto done; + + skel->links.skops_sockopt = + bpf_program__attach_cgroup(skel->progs.skops_sockopt, cg_fd); + if (!ASSERT_OK_PTR(skel->links.skops_sockopt, "attach cgroup")) + goto done; + + test_tcp(AF_INET6); + test_tcp(AF_INET); + +done: + so_timestamping__destroy(skel); + close(cg_fd); +} diff --git a/tools/testing/selftests/bpf/progs/so_timestamping.c b/tools/testing/selftests/bpf/progs/so_timestamping.c new file mode 100644 index 000000000000..db0582564fba --- /dev/null +++ b/tools/testing/selftests/bpf/progs/so_timestamping.c @@ -0,0 +1,186 @@ +#include "vmlinux.h" +#include "bpf_tracing_net.h" +#include +#include +#include +//#include +#include "bpf_misc.h" +#include "bpf_kfuncs.h" + +#define SK_BPF_CB_FLAGS 1009 +#define SK_BPF_CB_TX_TIMESTAMPING 1 + +int nr_active; +int nr_tcp; +int nr_passive; +int nr_sched; +int nr_txsw; +int nr_ack; + +struct sockopt_test { + int opt; + int new; +}; + +static const struct sockopt_test sol_socket_tests[] = { + { .opt = SK_BPF_CB_FLAGS, .new = SK_BPF_CB_TX_TIMESTAMPING, }, + { .opt = 0, }, +}; + +struct loop_ctx { + void *ctx; + const struct sock *sk; +}; + +struct delay_info { + u64 sendmsg_ns; /* record ts when sendmsg is called */ + u32 sched_delay; /* SCHED_OPT_CB - sendmsg_ns */ + u32 sw_snd_delay; /* SW_OPT_CB - SCHED_OPT_CB */ + u32 ack_delay; /* ACK_OPT_CB - SW_OPT_CB */ +}; + +struct { + __uint(type, BPF_MAP_TYPE_HASH); + __type(key, u32); + __type(value, struct delay_info); + __uint(max_entries, 1024); +} time_map SEC(".maps"); + +static u64 delay_tolerance_nsec = 1000000000; /* 1 second as an example */ + +static int bpf_test_sockopt_int(void *ctx, const struct sock *sk, + const struct sockopt_test *t, + int level) +{ + int new, opt; + + opt = t->opt; + new = t->new; + + if (bpf_setsockopt(ctx, level, opt, &new, sizeof(new))) + return 1; + + return 0; +} + +static int bpf_test_socket_sockopt(__u32 i, struct loop_ctx *lc) +{ + const struct sockopt_test *t; + + if (i >= ARRAY_SIZE(sol_socket_tests)) + return 1; + + t = &sol_socket_tests[i]; + if (!t->opt) + return 1; + + return bpf_test_sockopt_int(lc->ctx, lc->sk, t, SOL_SOCKET); +} + +static int bpf_test_sockopt(void *ctx, const struct sock *sk) +{ + struct loop_ctx lc = { .ctx = ctx, .sk = sk, }; + int n; + + n = bpf_loop(ARRAY_SIZE(sol_socket_tests), bpf_test_socket_sockopt, &lc, 0); + if (n != ARRAY_SIZE(sol_socket_tests)) + return -1; + + return 0; +} + +static bool bpf_test_delay(struct bpf_sock_ops *skops, const struct sock *sk) +{ + const struct tcp_sock *tp = tcp_sk(sk); + struct bpf_sock_ops_kern *skops_kern; + u64 timestamp = bpf_ktime_get_ns(); + struct skb_shared_info *shinfo; + struct delay_info dinfo = {0}; + struct delay_info *val; + struct sk_buff *skb; + u32 delay, tskey; + u64 prior_ts; + + skops_kern = bpf_cast_to_kern_ctx(skops); + skb = skops_kern->skb; + if (skb) { + shinfo = bpf_core_cast(skb->head + skb->end, struct skb_shared_info); + tskey = shinfo->tskey_bpf; + } else if (skops->op == BPF_SOCK_OPS_TS_TCP_SND_CB) { + dinfo.sendmsg_ns = timestamp; + tskey = tp->write_seq; + val = &dinfo; + goto out; + } else { + return false; + } + + val = bpf_map_lookup_elem(&time_map, &tskey); + if (!val) + return false; + + switch (skops->op) { + case BPF_SOCK_OPS_TS_SCHED_OPT_CB: + delay = val->sched_delay = timestamp - val->sendmsg_ns; + break; + case BPF_SOCK_OPS_TS_SW_OPT_CB: + prior_ts = val->sched_delay + val->sendmsg_ns; + delay = val->sw_snd_delay = timestamp - prior_ts; + break; + case BPF_SOCK_OPS_TS_ACK_OPT_CB: + prior_ts = val->sw_snd_delay + val->sched_delay + val->sendmsg_ns; + delay = val->ack_delay = timestamp - prior_ts; + break; + } + + if (delay <= 0 || delay >= delay_tolerance_nsec) + return false; + + /* Since it's the last one, remove from the map after latency check */ + if (skops->op == BPF_SOCK_OPS_TS_ACK_OPT_CB) + bpf_map_delete_elem(&time_map, &tskey); + +out: + bpf_map_update_elem(&time_map, &tskey, val, BPF_ANY); + return true; +} + +SEC("sockops") +int skops_sockopt(struct bpf_sock_ops *skops) +{ + struct bpf_sock *bpf_sk = skops->sk; + const struct sock *sk; + + if (!bpf_sk) + return 1; + + sk = (struct sock *)bpf_skc_to_tcp_sock(bpf_sk); + if (!sk) + return 1; + + switch (skops->op) { + case BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB: + nr_active += !bpf_test_sockopt(skops, sk); + break; + case BPF_SOCK_OPS_TS_TCP_SND_CB: + if (bpf_test_delay(skops, sk)) + nr_tcp += 1; + break; + case BPF_SOCK_OPS_TS_SCHED_OPT_CB: + if (bpf_test_delay(skops, sk)) + nr_sched += 1; + break; + case BPF_SOCK_OPS_TS_SW_OPT_CB: + if (bpf_test_delay(skops, sk)) + nr_txsw += 1; + break; + case BPF_SOCK_OPS_TS_ACK_OPT_CB: + if (bpf_test_delay(skops, sk)) + nr_ack += 1; + break; + } + + return 1; +} + +char _license[] SEC("license") = "GPL";