From patchwork Tue Oct 8 09:51:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Xing X-Patchwork-Id: 13826137 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 DB270192D9E; Tue, 8 Oct 2024 09:51:22 +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=1728381084; cv=none; b=nyinnNoSufzK5YI7Mvof18M4Q+Y8vOMAVtSmi9SHXgBVEGBgAQtaN/MrdWFWx02+ww4RNvuOH3wO/Cqt9BTG3SMTVTd56wSA76iTBJB1LbBCsJIz1tBr5FBRwND6sxiiJwjPPqKIspH5S5uwTX+kWHDRkYz6XHc0pdb2IF1XeQ0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728381084; c=relaxed/simple; bh=XyKDjomI7bRN80weKUjk082/xw7xQNSA2YquxGBT97g=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=s0776gWNnw7s5kEq3696UQUSKLyP4gpaykzLpN5uSnTFq9gP8NA8tcTs8S7R8VaE1+1JH8Ooa4bN/ot38Iy3mygodFdcq9BMQWaIRaj27MrFn0lOQgtxY3kZDGSbhBPeR2vjNQB1/xdNvapcNSi0x1xwQDpzODGrw1jmz0oyY8U= 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=U2wKhMES; 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="U2wKhMES" Received: by mail-pl1-f173.google.com with SMTP id d9443c01a7336-20b6c311f62so47285925ad.0; Tue, 08 Oct 2024 02:51:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728381082; x=1728985882; 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=CC4xycADMX0MXqW/i8Kf8ekcDmoE5R4kuSDQeZIq8GA=; b=U2wKhMES6KALSKoAUfdadp3K3KlOt9IYxNKeHFePXY2oQqREgmKTKduIYspMAvJgD2 MBlOSD93eqWHr9YHD38tzIwZ1i3brhZ5gz7f7rc1Yca/AurD3iMjF9tPX+W03Cf8CZVl tOQ+/OcBaN6jCGDBpmvVZ/y/1Mve+O90EQ3Ey1wggk0n35tBm1r+aAbGYE/E5BsRYOWQ JOSxqLImUZu8WXMDlClhfUj6h1cnKtDbkSqD8n0mv8p3O4KQ7M26cF64vJTHNAyJbX2L OiIQTbLCuJkHTsP6K1hURoqDt3DDzVEepBnWnnT0ENOngx1UthniNQ7GrOZjxXqO6m9R EtkQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728381082; x=1728985882; 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=CC4xycADMX0MXqW/i8Kf8ekcDmoE5R4kuSDQeZIq8GA=; b=jh7OE1bD18XanxcQtaV5nzRTBN8gLIr19CH6jHQmlqyf6BeydnRfWMUtiFT5wPv31v mQczqGyeE7ikb+L8vOmroHS00QpCPHqX6l5s0aS+hn56R0xDufO491gcNV9273ZifCsi b6C/0HzVnmzcOzFFaeGM28hLEh2BKly+qs+tK8MrMMIG2o97ZdL+/5kqU5UdgQfgn34c 77pH8JEadORQOOvn5T71IohOMBt6+KVlEp1BBRjhN7jGX6lEpVnJ61CLtx+3qrR2LrY8 xcjcsghsemWJrK2tJsR9SyGeGc+fLAmcxRIgFvLKVnzZZgaZY9vZ6p1WOt/A7ubTf9u7 2sOQ== X-Forwarded-Encrypted: i=1; AJvYcCWxI++VmBDd7zrMym+qofWWDhQnfvP9RBbmqV64Qrl86srM26vQAvnJNAYAiDd8Go+dKXTvawE=@vger.kernel.org X-Gm-Message-State: AOJu0Ywds9JDpSVB/FBD/Gw9/67PVnGX/KTFOzqkEroYv+5lleuc+1/O wx0E/jT2ahK6D+Cvhal5cN5OkS02wWwB8CfkZUc24m9yMTa0Zm+O X-Google-Smtp-Source: AGHT+IHZKKTgDgRW+il9PUCNNf+O2wDjQjHDRmo5RLHOU1NRBNkq3Jbt/D8pHAlkUor65q7GCEZEFQ== X-Received: by 2002:a17:902:e745:b0:20b:c1e4:2d70 with SMTP id d9443c01a7336-20bfe294c52mr226434235ad.23.1728381082143; Tue, 08 Oct 2024 02:51:22 -0700 (PDT) Received: from KERNELXING-MB0.tencent.com ([43.132.141.21]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-20c138cfd25sm52527345ad.73.2024.10.08.02.51.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Oct 2024 02:51:21 -0700 (PDT) From: Jason Xing To: davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, dsahern@kernel.org, willemdebruijn.kernel@gmail.com, willemb@google.com, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com, jolsa@kernel.org Cc: bpf@vger.kernel.org, netdev@vger.kernel.org, Jason Xing Subject: [PATCH net-next 1/9] net-timestamp: add bpf infrastructure to allow exposing more information later Date: Tue, 8 Oct 2024 17:51:01 +0800 Message-Id: <20241008095109.99918-2-kerneljasonxing@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20241008095109.99918-1-kerneljasonxing@gmail.com> References: <20241008095109.99918-1-kerneljasonxing@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org From: Jason Xing Implement basic codes so that we later can easily add each tx points. Introducing BPF_SOCK_OPS_ALL_CB_FLAGS used as a test statement can help use control whether to output or not. Signed-off-by: Jason Xing --- include/uapi/linux/bpf.h | 5 ++++- net/core/skbuff.c | 18 ++++++++++++++++++ tools/include/uapi/linux/bpf.h | 5 ++++- 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index c6cd7c7aeeee..157e139ed6fc 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -6900,8 +6900,11 @@ enum { * options first before the BPF program does. */ BPF_SOCK_OPS_WRITE_HDR_OPT_CB_FLAG = (1<<6), + /* Call bpf when the kernel is generating tx timestamps. + */ + BPF_SOCK_OPS_TX_TIMESTAMPING_OPT_CB_FLAG = (1<<7), /* Mask of all currently supported cb flags */ - BPF_SOCK_OPS_ALL_CB_FLAGS = 0x7F, + BPF_SOCK_OPS_ALL_CB_FLAGS = 0xFF, }; /* List of known BPF sock_ops operators. diff --git a/net/core/skbuff.c b/net/core/skbuff.c index 74149dc4ee31..5ff1a91c1204 100644 --- a/net/core/skbuff.c +++ b/net/core/skbuff.c @@ -5539,6 +5539,21 @@ void skb_complete_tx_timestamp(struct sk_buff *skb, } EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp); +static bool bpf_skb_tstamp_tx(struct sock *sk, u32 scm_flag, + struct skb_shared_hwtstamps *hwtstamps) +{ + struct tcp_sock *tp; + + if (!sk_is_tcp(sk)) + return false; + + tp = tcp_sk(sk); + if (BPF_SOCK_OPS_TEST_FLAG(tp, BPF_SOCK_OPS_TX_TIMESTAMPING_OPT_CB_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, @@ -5551,6 +5566,9 @@ void __skb_tstamp_tx(struct sk_buff *orig_skb, if (!sk) return; + if (bpf_skb_tstamp_tx(sk, tstype, hwtstamps)) + return; + tsflags = READ_ONCE(sk->sk_tsflags); if (!hwtstamps && !(tsflags & SOF_TIMESTAMPING_OPT_TX_SWHW) && skb_shinfo(orig_skb)->tx_flags & SKBTX_IN_PROGRESS) diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index 1fb3cb2636e6..93853d9d4922 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -6899,8 +6899,11 @@ enum { * options first before the BPF program does. */ BPF_SOCK_OPS_WRITE_HDR_OPT_CB_FLAG = (1<<6), + /* Call bpf when the kernel is generating tx timestamps. + */ + BPF_SOCK_OPS_TX_TIMESTAMPING_OPT_CB_FLAG = (1<<7), /* Mask of all currently supported cb flags */ - BPF_SOCK_OPS_ALL_CB_FLAGS = 0x7F, + BPF_SOCK_OPS_ALL_CB_FLAGS = 0xFF, }; /* List of known BPF sock_ops operators. From patchwork Tue Oct 8 09:51:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Xing X-Patchwork-Id: 13826138 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-pl1-f179.google.com (mail-pl1-f179.google.com [209.85.214.179]) (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 E771218C92D; Tue, 8 Oct 2024 09:51:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728381090; cv=none; b=sqoHizxAQ05WwHKJh6kAXcMlm2wowu0jK3Uk/WrAw7q5y/g5V1L0U3hc/6QJaAoBbm30NOFGfNQROaEbtMq954K6Fb/RKCTjIeXvtGpNjPPoElWaVfq/DtRvcgI/pekjOILLjHdYyhUJL1melh4JfkY7258ONoKwSQQwDiYx82I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728381090; c=relaxed/simple; bh=gfIGOMois/XfufsYDX1Tjk7Q5MqWeIoi2YOgvJxM9RM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=oxRYITj+Iv83Ba2idz+DXqNZ7X1A5QanvAyrBi45wxf2CSKdwMY79RAeUiKLjCtthLBgsiOYZhAKULl58QqgcRo2haDB59Wy2TdP4oYN3P1+BWPlEiRsNbQk0OcPO7nSNpWfUWiFUjQ4HY7EdR+MgDyf0CYlVAm011kgcr1N/XM= 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=injmPPme; arc=none smtp.client-ip=209.85.214.179 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="injmPPme" Received: by mail-pl1-f179.google.com with SMTP id d9443c01a7336-20c56b816faso4410805ad.2; Tue, 08 Oct 2024 02:51:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728381087; x=1728985887; 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=98ykS8Ga57uf+D8UKSlgOLD18H1UXyqadxSpnrjw8jg=; b=injmPPmeWJysV2PaISLsWyvyzD06Z0GQ5yfbd5t1LNAcv8thMXaqHYaVu7bRX4CGem Qxprit4r2DfC61lUttCBgrCfG5i904jSONNOocraUYxxYgbdmnmkUoB/USlLIUFy6I+g snr5gjV5plEndAMcZXcXDo16qsJDgFjbSyj7TtyIemZJB5XWnShYlNvOZA960pZXFUaQ bPBG6Yhh8P2WuB8I/akQHzn06r4KnAHzBnPi5Kochhoe/NCJ6yROBqEDtt6G3TIpUjKe l7Gg/NC5/Dy7DnIlre6PttOepxVPFEwBssFpcvcAxD/O2h9PVoKC7arrcVot4SLHReU7 XL6g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728381087; x=1728985887; 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=98ykS8Ga57uf+D8UKSlgOLD18H1UXyqadxSpnrjw8jg=; b=aCNhROrbi0grZM51HJRQp6bBcNqXjDuU8wfTLDouC09e05a99zyRzqpAh/VolkPoo0 SNBrp0PEeHL33F+iQ6S4ve9hHCLAk/C/XfiLt5pn59bB3EliigLNKxkrdEN50jiAlo+Y a9sYXYitwyjQPT8DPUBys7XD1pI1swnXbvAhG+PeoCJGW/nO5upd3HW61HvL1NJRtC8k 6ru+d8fQSGqCG7N3AKLw3xtPPZnb9PAuBgC50tzGhdooupzhb4b1C5v9yEYM4NHWyFiR INysX67IB4IGfpaITilfLyO63iOjFuXruZg25rTy0oIkrgo6wbMF3hAZ+jx6+O24RX3y lLBA== X-Forwarded-Encrypted: i=1; AJvYcCVvqsVroRUTjjJckBQKThszWRoFya9NieXhPJ1DQLY+F4GTEZu59063HnFhSJ6tukgVFxSB1sk=@vger.kernel.org X-Gm-Message-State: AOJu0YzW6/q0DejunAIpT6/8A5RgxSG3xHJQIZf2CrlUnylPzB+xMaGt e6t3WxIM5z8COWcim9CI4ZxfAStYHI9/vQx5sthaQmnj2C77XjwF X-Google-Smtp-Source: AGHT+IHE+S/SdAwkomRxpQvFPULcgvDI3kpZMfoP/Dz3A8+rjr3YJCiDUVMQSHWUhSKjej6ZkpPB3Q== X-Received: by 2002:a17:902:d4c7:b0:20b:bac2:88f6 with SMTP id d9443c01a7336-20bff17d57dmr215597065ad.52.1728381087019; Tue, 08 Oct 2024 02:51:27 -0700 (PDT) Received: from KERNELXING-MB0.tencent.com ([43.132.141.21]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-20c138cfd25sm52527345ad.73.2024.10.08.02.51.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Oct 2024 02:51:26 -0700 (PDT) From: Jason Xing To: davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, dsahern@kernel.org, willemdebruijn.kernel@gmail.com, willemb@google.com, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com, jolsa@kernel.org Cc: bpf@vger.kernel.org, netdev@vger.kernel.org, Jason Xing Subject: [PATCH net-next 2/9] net-timestamp: introduce TS_SCHED_OPT_CB to generate dev xmit timestamp Date: Tue, 8 Oct 2024 17:51:02 +0800 Message-Id: <20241008095109.99918-3-kerneljasonxing@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20241008095109.99918-1-kerneljasonxing@gmail.com> References: <20241008095109.99918-1-kerneljasonxing@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org From: Jason Xing Introduce BPF_SOCK_OPS_TS_SCHED_OPT_CB flag so that we can decide to print timestamps when the skb just passes the dev layer. Signed-off-by: Jason Xing --- include/uapi/linux/bpf.h | 5 +++++ net/core/skbuff.c | 16 +++++++++++++++- tools/include/uapi/linux/bpf.h | 5 +++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 157e139ed6fc..3cf3c9c896c7 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -7019,6 +7019,11 @@ enum { * by the kernel or the * earlier bpf-progs. */ + BPF_SOCK_OPS_TS_SCHED_OPT_CB, /* Called when skb is passing through + * dev layer when SO_TIMESTAMPING + * feature is on. It indicates the + * recorded timestamp. + */ }; /* List of TCP states. There is a build check in net/ipv4/tcp.c to detect diff --git a/net/core/skbuff.c b/net/core/skbuff.c index 5ff1a91c1204..e697f50d1182 100644 --- a/net/core/skbuff.c +++ b/net/core/skbuff.c @@ -5548,8 +5548,22 @@ static bool bpf_skb_tstamp_tx(struct sock *sk, u32 scm_flag, return false; tp = tcp_sk(sk); - if (BPF_SOCK_OPS_TEST_FLAG(tp, BPF_SOCK_OPS_TX_TIMESTAMPING_OPT_CB_FLAG)) + if (BPF_SOCK_OPS_TEST_FLAG(tp, BPF_SOCK_OPS_TX_TIMESTAMPING_OPT_CB_FLAG)) { + struct timespec64 tstamp; + u32 cb_flag; + + switch (scm_flag) { + case SCM_TSTAMP_SCHED: + cb_flag = BPF_SOCK_OPS_TS_SCHED_OPT_CB; + break; + default: + return true; + } + + tstamp = ktime_to_timespec64(ktime_get_real()); + tcp_call_bpf_2arg(sk, cb_flag, tstamp.tv_sec, tstamp.tv_nsec); return true; + } return false; } diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index 93853d9d4922..d60675e1a5a0 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -7018,6 +7018,11 @@ enum { * by the kernel or the * earlier bpf-progs. */ + BPF_SOCK_OPS_TS_SCHED_OPT_CB, /* Called when skb is passing through + * dev layer when SO_TIMESTAMPING + * feature is on. It indicates the + * recorded timestamp. + */ }; /* List of TCP states. There is a build check in net/ipv4/tcp.c to detect From patchwork Tue Oct 8 09:51:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Xing X-Patchwork-Id: 13826139 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 0CDA818C036; Tue, 8 Oct 2024 09:51:32 +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=1728381094; cv=none; b=MEVh1js/BUPRCoobSeGK2GV1W9EOjI3gsNX1m9DnDVqKJ4XoBKYWHuR0Fu/q8rNzlk0vrB/Qg+2V+bdqwLLwM5ktkSPO6gPK8DhhzvXVwUYOTnPeVsyqnfThP8hB4APhscK3x5FiK2BAn/gYJIR3CcP9wNXWSaXaNpEew7eFDKY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728381094; c=relaxed/simple; bh=+6Z1rn6ppe+Tr7GvfJzb8ihJP1TZEXIeogF0Vwy118Q=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=OSzFAKl6677nrSpQMXCtn9GWOOLXIF7LwGD70zR+xS2vsWkpH8THkS5i3CCjuC2dO2d0CPME/+padpcSADXgAw4oPjdIYhc/tQHflgGSEaQqYTl6ZQESuGa6R50DSU1wq61oR8EtG0m1cU4+PC9K6nI92cYHFQmrS/BtHnxqBi0= 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=LLzf9/ZF; 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="LLzf9/ZF" Received: by mail-pl1-f182.google.com with SMTP id d9443c01a7336-20c56b816faso4411705ad.2; Tue, 08 Oct 2024 02:51:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728381092; x=1728985892; 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=dvjTOC4Sr8/XyCnIZg/tJ7FeUwowYM7uPRWpo4Tvou4=; b=LLzf9/ZFT2VwRg6gogu5M9MDaCslOIzaoGUQD3CGv4x0ta5OiHlqec4ZUeoxrT6V5q vLzu8ODP2Zj0jmayzTFwIqxjDu6Y/HixNyFLD6/k6GS1X/rvZtjwhdLi/MmXsgf16v6C JQkX/3gc1QGYX7Js9JxlOUR41XEn/3ScpLC+6t2S7xjkZD7ehLa1GZeAZ/GOKZkRTBnd gHIWlY6UR5vgKmqqDDaOY0Op/ragGsYTyUUZoYID83Cak/RgIyhRQxHF3pOT4WVMN26x +qiAgw0vI/9Hs6jvZ0A6QJX35EYblwLmRm2fK4kmo0EnvneBJK14lVkrp3MA+7yThDzP 3xug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728381092; x=1728985892; 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=dvjTOC4Sr8/XyCnIZg/tJ7FeUwowYM7uPRWpo4Tvou4=; b=i8MaqAXL6bhzbVj5oKiERamHC7tqcuQKzZMVjK7r+jJPkBsWywPhphY4DOTGlHWV6h XyW9kDq5ucXAFGI+2U9HWO5Y6IXntI/2zYY6JIFmKKDxc3izxcMnbgE7ks7BcZ0204O9 7ADD8NuZcbE5Kz8hRDaP+9W6/9okKFEy7zUGSScOGZvL5xZJCxqBNW/Cw49DQmdqmjrE OzLVhtW3z4yO+ZIM4wXt3jdniyBANaIejYQLKBE8LGOPNY0x3sp9QjzPCi9f7Z9OzDDO WpNpldBRcvqQg3SLDMuG+kR5hY1ZBztu1EpU3DAzX7HPMPvJQB3ESj0uDWortWBk4Vq3 fgmg== X-Forwarded-Encrypted: i=1; AJvYcCWm9/3fqO4TyvpiUnBsCjXYFe2ldM0c2Jy+yhzAirSx8rmCNeVpSJoK3MM6D+tAZZCog8T38Ng=@vger.kernel.org X-Gm-Message-State: AOJu0YxXh1Wpai8fEnROZQANyOVOqCR+6YBLdo7Vj3xscO6deph8Mosj 7kuD/zBuhJs2+E+kY88bOWE21ZyFTV690AT/IIOXYqZKKJjPhjDu X-Google-Smtp-Source: AGHT+IHVVQAtq/gbTsj4clmk/yxX7RID4Gxoyq1AZ2EOoaw0Zd3BfduQgYXIvTnyBayOuDBlSJrQ3Q== X-Received: by 2002:a17:903:32c1:b0:20b:58f2:e1a0 with SMTP id d9443c01a7336-20bfdfd4340mr202494945ad.18.1728381092193; Tue, 08 Oct 2024 02:51:32 -0700 (PDT) Received: from KERNELXING-MB0.tencent.com ([43.132.141.21]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-20c138cfd25sm52527345ad.73.2024.10.08.02.51.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Oct 2024 02:51:31 -0700 (PDT) From: Jason Xing To: davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, dsahern@kernel.org, willemdebruijn.kernel@gmail.com, willemb@google.com, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com, jolsa@kernel.org Cc: bpf@vger.kernel.org, netdev@vger.kernel.org, Jason Xing Subject: [PATCH net-next 3/9] net-timestamp: introduce TS_SW_OPT_CB to generate driver timestamp Date: Tue, 8 Oct 2024 17:51:03 +0800 Message-Id: <20241008095109.99918-4-kerneljasonxing@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20241008095109.99918-1-kerneljasonxing@gmail.com> References: <20241008095109.99918-1-kerneljasonxing@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org From: Jason Xing When the skb is about to send from driver to nic, we can print timestamp by setting BPF_SOCK_OPS_TS_SW_OPT_CB in bpf program. Signed-off-by: Jason Xing --- include/uapi/linux/bpf.h | 5 +++++ net/core/skbuff.c | 8 +++++++- tools/include/uapi/linux/bpf.h | 5 +++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 3cf3c9c896c7..0d00539f247a 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -7024,6 +7024,11 @@ enum { * feature is on. It indicates the * recorded timestamp. */ + BPF_SOCK_OPS_TS_SW_OPT_CB, /* Called when skb is about to send + * to the nic when SO_TIMESTAMPING + * feature is on. It indicates the + * recorded timestamp. + */ }; /* List of TCP states. There is a build check in net/ipv4/tcp.c to detect diff --git a/net/core/skbuff.c b/net/core/skbuff.c index e697f50d1182..8faaa96c026b 100644 --- a/net/core/skbuff.c +++ b/net/core/skbuff.c @@ -5556,11 +5556,17 @@ static bool bpf_skb_tstamp_tx(struct sock *sk, u32 scm_flag, case SCM_TSTAMP_SCHED: cb_flag = BPF_SOCK_OPS_TS_SCHED_OPT_CB; break; + case SCM_TSTAMP_SND: + cb_flag = BPF_SOCK_OPS_TS_SW_OPT_CB; + break; default: return true; } - tstamp = ktime_to_timespec64(ktime_get_real()); + if (hwtstamps) + tstamp = ktime_to_timespec64(hwtstamps->hwtstamp); + else + tstamp = ktime_to_timespec64(ktime_get_real()); tcp_call_bpf_2arg(sk, cb_flag, tstamp.tv_sec, tstamp.tv_nsec); return true; } diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index d60675e1a5a0..020ec14ffae6 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -7023,6 +7023,11 @@ enum { * feature is on. It indicates the * recorded timestamp. */ + BPF_SOCK_OPS_TS_SW_OPT_CB, /* Called when skb is about to send + * to the nic when SO_TIMESTAMPING + * feature is on. It indicates the + * recorded timestamp. + */ }; /* List of TCP states. There is a build check in net/ipv4/tcp.c to detect From patchwork Tue Oct 8 09:51:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Xing X-Patchwork-Id: 13826140 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 B23BE18C036; Tue, 8 Oct 2024 09:51:37 +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=1728381099; cv=none; b=Lbi96bGEED+gdBU9GzU9xXfbvM6At8TTaQ7dTbRUDv5pEaoqfj6oINmldjzNTcFa/Dwev3vZBaXOly4PDcmcCykJ5WHWOJWT85qaU7rsv1z5/ikrdLG860WAjGQX/yAKqyGyyf0HDWkmLEOVkRiFr9mLIN/ib6ZMzgQlLHXkMCo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728381099; c=relaxed/simple; bh=8OSNU7mieaAQeLBXSucsuY/25TVKLVtBL09ISEs48SA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Ptps4/qJftjMVPuJQcDvXq7VTjI7PD8KqpJF5apRvpay/kCRyTF1hc5oAblQMxiP5WJ/J3mzbuINLdmQI9s143LSCNJUiQyXBN2YSZF0uQi672VHhkWf0wAu/gvv5NKuWQTTJGxcFNx1lEzxGtxfZpK9VKlHZVC/y+G9voOhnEU= 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=GLyewEiC; 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="GLyewEiC" Received: by mail-pl1-f173.google.com with SMTP id d9443c01a7336-20b7259be6fso59493095ad.0; Tue, 08 Oct 2024 02:51:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728381097; x=1728985897; 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=quADWjfjiuWGufDN5ENxiTYOZZcFg6R0qU2ip7FRzFI=; b=GLyewEiCuqkUOIj+MHVzNGm+a4Mctss34MfxUGgvxbm6whhO9T+UABqise9DM3E+nD d3bW6DxjtcWC5EGueStYVIOBsgL37sUSv+WB6+DycKBSBI4vklMcQrmt/XKBGIKTm07N 2gt5luvW73lrlblKx4q+p+mTq5Z+lE0MtzHDAvzlg7mdIii+0aCVmVeP2EWGMZ9RC4/D zsU3h7n8d5djDOs8lAVw7unD2hSUAPSbItUC5c4PZpfWgQyHsCSNKw6M+iLaFCBR2eKd 51bVx2eaQhA0yIsbgRb4z6Yu1cPknM2vof34XGwPPb4qtRwFNrgVyNVErIUrbXshmt3T c58Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728381097; x=1728985897; 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=quADWjfjiuWGufDN5ENxiTYOZZcFg6R0qU2ip7FRzFI=; b=tqoe2JQWyCnMC8XI+VF5clTgLqHPznolX9NsRi7dNBkhC3UrpoTqTwjyU+0eUVd0ro Tc0rdQAoV9nV27E51YswcfIfoySYTjc47tYm0UKbnre0fQFxDNN5M9FbkNWCj0SMVk5f lIgF/aAE5qn0saKjk/veTiHGfA1L/7Hl06WUSoN6V5lz1I3v4Tg3Go+sFlyCLwUI7aGD SY7kvKV27qnBta3Lmyv+kmwwd/Z7GGKli0Qop48WgqAkn+Whdbl4ZAiL2I8f8P9UkJ23 9VcqLxtc15XRboQXrh6NNZVjgYU3T2qVaTcMbW+yjQl2dbUv2xSGFHbUpKw0Ols2fmks vN+A== X-Forwarded-Encrypted: i=1; AJvYcCUCFE7xuc5RW1G+ey640IOaJ1eR5L0YZ9zoyb/m6wEq5EOjc/B48GElRKCxKLmBxZwwabkMEBw=@vger.kernel.org X-Gm-Message-State: AOJu0YwzsDHDrwup8ISoxLtKPQ26FFsbLop7ZoodIB9ZOgUWvm77bAHo hG6Pzg57Ae7XA3PWBpZHvd0ZEeMvYu/+3hQpgbKxnXae/vK9akwF X-Google-Smtp-Source: AGHT+IFwzIm6ObBTvUC419Yc30oGlwpkyQujouLzvxxd+x7/D42VC6ZhVjGTd3ub+HdIgukNhvzypQ== X-Received: by 2002:a17:902:da8d:b0:205:3e6d:9949 with SMTP id d9443c01a7336-20bff03973emr207418595ad.52.1728381097019; Tue, 08 Oct 2024 02:51:37 -0700 (PDT) Received: from KERNELXING-MB0.tencent.com ([43.132.141.21]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-20c138cfd25sm52527345ad.73.2024.10.08.02.51.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Oct 2024 02:51:36 -0700 (PDT) From: Jason Xing To: davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, dsahern@kernel.org, willemdebruijn.kernel@gmail.com, willemb@google.com, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com, jolsa@kernel.org Cc: bpf@vger.kernel.org, netdev@vger.kernel.org, Jason Xing Subject: [PATCH net-next 4/9] net-timestamp: introduce TS_ACK_OPT_CB to generate tcp acked timestamp Date: Tue, 8 Oct 2024 17:51:04 +0800 Message-Id: <20241008095109.99918-5-kerneljasonxing@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20241008095109.99918-1-kerneljasonxing@gmail.com> References: <20241008095109.99918-1-kerneljasonxing@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org From: Jason Xing When the last sent skb in each sendmsg() is acknowledged in TCP layer, we can print timestamp by setting BPF_SOCK_OPS_TS_ACK_OPT_CB in bpf program. Signed-off-by: Jason Xing --- include/uapi/linux/bpf.h | 5 +++++ net/core/skbuff.c | 3 +++ tools/include/uapi/linux/bpf.h | 5 +++++ 3 files changed, 13 insertions(+) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 0d00539f247a..1b478ec18ac2 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -7029,6 +7029,11 @@ enum { * feature is on. It indicates the * recorded timestamp. */ + BPF_SOCK_OPS_TS_ACK_OPT_CB, /* Called when all the skbs are + * acknowledged when SO_TIMESTAMPING + * feature is on. It indicates the + * recorded timestamp. + */ }; /* List of TCP states. There is a build check in net/ipv4/tcp.c to detect diff --git a/net/core/skbuff.c b/net/core/skbuff.c index 8faaa96c026b..2b1b2f7d271a 100644 --- a/net/core/skbuff.c +++ b/net/core/skbuff.c @@ -5559,6 +5559,9 @@ static bool bpf_skb_tstamp_tx(struct sock *sk, u32 scm_flag, case SCM_TSTAMP_SND: cb_flag = BPF_SOCK_OPS_TS_SW_OPT_CB; break; + case SCM_TSTAMP_ACK: + cb_flag = BPF_SOCK_OPS_TS_ACK_OPT_CB; + break; default: return true; } diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index 020ec14ffae6..fc9b94de19f2 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -7028,6 +7028,11 @@ enum { * feature is on. It indicates the * recorded timestamp. */ + BPF_SOCK_OPS_TS_ACK_OPT_CB, /* Called when all the skbs are + * acknowledged when SO_TIMESTAMPING + * feature is on. It indicates the + * recorded timestamp. + */ }; /* List of TCP states. There is a build check in net/ipv4/tcp.c to detect From patchwork Tue Oct 8 09:51:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Xing X-Patchwork-Id: 13826141 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-pl1-f171.google.com (mail-pl1-f171.google.com [209.85.214.171]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E585018C036; Tue, 8 Oct 2024 09:51:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728381104; cv=none; b=Dg59cVBbCVPSavGnt+Ykq+vABOQzc0y4yG3sWHDUWEc/kifvwa7cm1bR2AO8AjM5n2UPudGdSJ3V8RyHwvieLtd1dUrUvqHSfpJ+UOY91qXIj0Gd5mZVmrfNE4PIXoEebRk3kreH1tqBdpYbRPUOb/fNrQqHqsqKgR7JGTsFo6Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728381104; c=relaxed/simple; bh=O+rOCs8Km96tOettkmdRxJdlhsVKmwjIfgNQYUpVrIA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=e/FNucjBLuCxRKnx4J7wB2CwiC50wgq+Nl1HFLpVS/giM5foyMI6yZuWSiBzJsRJVbnGlYeXU7oRb9RuGTgm1gtEWc+W6mhxwyBkBVzI0hKtt8Zr5SitKr60vJNolFu28jEOxAa+QwO5sa2DD2X1N6eaDPDFheyh+k0l+Y7DNho= 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=VMsDxYJR; arc=none smtp.client-ip=209.85.214.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="VMsDxYJR" Received: by mail-pl1-f171.google.com with SMTP id d9443c01a7336-20b6c311f62so47288895ad.0; Tue, 08 Oct 2024 02:51:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728381102; x=1728985902; 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=clbNLhCp+XW6B4yk6HCtD08Zy4k9Z0TDt4mDq2HKh3E=; b=VMsDxYJRahd5hktauEs6tM/TXmy3+wRsrVWvcGuRI0Lxh2MEKLzpEdhRyd3o+Ei/5D kZOahmzlMK/o/Cu4ErhfhDeTlnIkmoFnSw37RZsKpN6G8lSqFlJO7t6AFAXWsKtExzxa bM48SUMrdTAsnEFTHoj//rqd06kG8h/ouNR4/K6DNFOnm5tMWSkR5+5AExQsTQ/bolBU XGTIVkdopaDt2ILC62GH/z4ZGLHaf214uQU+bvWfceMM615HMrJjKeOXehxD8tzCLb8p bB8KVfJXqgB0OXOgP73a+Jg534iJxdaTskuNE3Y9ys30talnvcthegB1DmqzVs+UPEUR 6GIg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728381102; x=1728985902; 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=clbNLhCp+XW6B4yk6HCtD08Zy4k9Z0TDt4mDq2HKh3E=; b=vzId6M5mQF6X62w/rr3y96TDJG/FDCz+ZHwmi5VCu5quJAcQXMiU8YPigcNmKaq3nd G+3tVyWvCryalElZWd5hmTxj89o95or3UtxCPmR0rykHAB3YRgcGGKghnVmUuBY5JEEF B0G+wVzlOBm9FdVszMmhVBS/gTJt7hFC+K5YMwZcxox8igFmGC0OTN3UMaX6adG4CJ/T ofMi8ckcIV3rwlBuZM1oSyfGMIBeMWuDaMpFH54hDxy/LMTHfPnJjA7nCOlfxLQrpCXv NwXvCCSHOOY7au67stcJlGOUgVGC2U+7PNcW6bw8mAii+maxs06Bf/9VqHP6CxVdA3ZW P9mQ== X-Forwarded-Encrypted: i=1; AJvYcCWVAo3G7wXV56XwI8JArYWYdP/3coPfZ2N48hhWD/wdwMw6ALYDEkyDpbyUr/JCzPgeYwpSOME=@vger.kernel.org X-Gm-Message-State: AOJu0YwXDisHWdRcQhyqapxsOi1TUYinyjyqmF/AQkVHo3QnZ1xM1DiU mumx616draNdpseZkCOHNsMCT9KVBTjgggs7EvIQpzhT3gZDmo02 X-Google-Smtp-Source: AGHT+IHQUWiP5VPlQ5FbrqdDcAGrJrUGqd/XwD/1PvwRofhB6VtQnZaj7ZoDd9IfM8cvklLAN0wVmQ== X-Received: by 2002:a17:902:e848:b0:20b:96b6:9fc2 with SMTP id d9443c01a7336-20bfe022a3emr199451565ad.10.1728381102073; Tue, 08 Oct 2024 02:51:42 -0700 (PDT) Received: from KERNELXING-MB0.tencent.com ([43.132.141.21]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-20c138cfd25sm52527345ad.73.2024.10.08.02.51.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Oct 2024 02:51:41 -0700 (PDT) From: Jason Xing To: davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, dsahern@kernel.org, willemdebruijn.kernel@gmail.com, willemb@google.com, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com, jolsa@kernel.org Cc: bpf@vger.kernel.org, netdev@vger.kernel.org, Jason Xing Subject: [PATCH net-next 5/9] net-timestamp: ready to turn on the button to generate tx timestamps Date: Tue, 8 Oct 2024 17:51:05 +0800 Message-Id: <20241008095109.99918-6-kerneljasonxing@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20241008095109.99918-1-kerneljasonxing@gmail.com> References: <20241008095109.99918-1-kerneljasonxing@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org From: Jason Xing Once we set BPF_SOCK_OPS_TX_TIMESTAMP_OPT_CB_FLAG flag here, there are three points in the previous patches where generating timestamps works. Let us make the basic bpf mechanism for timestamping feature work finally. We can use like this as a simple example in bpf program: __section("sockops") case BPF_SOCK_OPS_TX_TIMESTAMP_OPT_CB: dport = bpf_ntohl(skops->remote_port); sport = skops->local_port; skops->reply = SOF_TIMESTAMPING_TX_SCHED; bpf_sock_ops_cb_flags_set(skops, BPF_SOCK_OPS_TX_TIMESTAMP_OPT_CB_FLAG); case BPF_SOCK_OPS_TS_SCHED_OPT_CB: bpf_printk(...); Signed-off-by: Jason Xing --- include/uapi/linux/bpf.h | 8 ++++++++ net/ipv4/tcp.c | 27 ++++++++++++++++++++++++++- tools/include/uapi/linux/bpf.h | 8 ++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 1b478ec18ac2..6bf3f2892776 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -7034,6 +7034,14 @@ enum { * feature is on. It indicates the * recorded timestamp. */ + BPF_SOCK_OPS_TX_TS_OPT_CB, /* Called when the last skb from + * sendmsg is going to push when + * SO_TIMESTAMPING feature is on. + * Let user have a chance to switch + * on BPF_SOCK_OPS_TX_TIMESTAMPING_OPT_CB_FLAG + * flag for other three tx timestamp + * use. + */ }; /* List of TCP states. There is a build check in net/ipv4/tcp.c to detect diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c index 82cc4a5633ce..ddf4089779b5 100644 --- a/net/ipv4/tcp.c +++ b/net/ipv4/tcp.c @@ -477,12 +477,37 @@ void tcp_init_sock(struct sock *sk) } EXPORT_SYMBOL(tcp_init_sock); +static u32 bpf_tcp_tx_timestamp(struct sock *sk) +{ + u32 flags; + + flags = tcp_call_bpf(sk, BPF_SOCK_OPS_TX_TS_OPT_CB, 0, NULL); + if (flags <= 0) + return 0; + + if (flags & ~SOF_TIMESTAMPING_MASK) + return 0; + + if (!(flags & SOF_TIMESTAMPING_TX_RECORD_MASK)) + return 0; + + return flags; +} + static void tcp_tx_timestamp(struct sock *sk, struct sockcm_cookie *sockc) { struct sk_buff *skb = tcp_write_queue_tail(sk); u32 tsflags = sockc->tsflags; + u32 flags; + + if (!skb) + return; + + flags = bpf_tcp_tx_timestamp(sk); + if (flags) + tsflags = flags; - if (tsflags && skb) { + if (tsflags) { struct skb_shared_info *shinfo = skb_shinfo(skb); struct tcp_skb_cb *tcb = TCP_SKB_CB(skb); diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index fc9b94de19f2..d3bf538846da 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -7033,6 +7033,14 @@ enum { * feature is on. It indicates the * recorded timestamp. */ + BPF_SOCK_OPS_TX_TS_OPT_CB, /* Called when the last skb from + * sendmsg is going to push when + * SO_TIMESTAMPING feature is on. + * Let user have a chance to switch + * on BPF_SOCK_OPS_TX_TIMESTAMPING_OPT_CB_FLAG + * flag for other three tx timestamp + * use. + */ }; /* List of TCP states. There is a build check in net/ipv4/tcp.c to detect From patchwork Tue Oct 8 09:51:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Xing X-Patchwork-Id: 13826142 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 93BA017C21E; Tue, 8 Oct 2024 09:51: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=1728381109; cv=none; b=B/wTTm3E2aJB7NDdm99/PZMB9iDKyNh+apwXdNHPS3dy3SsYNvaN3/ivJNJqskUDR1qgL+ACxK0ikycFAw+XBXSQl9gW1izqqfFIf07p1O/zAahN6en75EmApN4M7xIZq+38/D7i1blabTfbHwgLChAYkOZD2bNi/Pz97xzgc/Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728381109; c=relaxed/simple; bh=CdcNbFmj/bL/6uD9+d/679NJU9rum9bMsXDAJjKvDOU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=BssBzSuq4zeMWhqTO8cYEoBt/XoiilCWSVOwFxMOpgc5g4/uJsW9R2dd0b/iZRMCWKEcbcV669MaNfciTrhKpMTsLq8Xquiv6aRzsUck8MB20vWTP9ohslFig5OxY/ega1J6tzSartXeupetKIL4OnoP+oQj9ZEhlcZ8FC9+vnU= 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=DfQu+o/M; 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="DfQu+o/M" Received: by mail-pl1-f177.google.com with SMTP id d9443c01a7336-20bc2970df5so40747505ad.3; Tue, 08 Oct 2024 02:51:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728381107; x=1728985907; 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=jsFjHqgPX322hbM4j/dBTNJ41SGcpn/JA6FQ4Q+UX+g=; b=DfQu+o/MVysqC3PbSn6ecM24IC8wsnWZCORjIX1ymuS+sFZa52Ppk1il2e+jOKwy8z euHWlhtsvJ2MCw/kj4I9F+Vmi/0JsFw6re38mNED5I46ID4c6jFJDzXG6x7pqp7ouk+f dVqQMqzrJw1q8phHH/4YEFamPXbKuMr+PLV6hSXWSw5j8JchwVQBEb2je31LJoYxKUmv L9Is+/mypoJcUlCEPCjQK9zx72KQGrKRr56nPFjFy3S6ctpbaw+pnNB64F9YpBvcBc9J CiaKxBp6aKO+if/+tm6/A61xDP7oqEFBkX8XtQP/Kirlf0YA2juAubKbqbSAYxQETTvJ Li9A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728381107; x=1728985907; 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=jsFjHqgPX322hbM4j/dBTNJ41SGcpn/JA6FQ4Q+UX+g=; b=qr9E8W2sPlLI/3Bc2yljhcmcx5j5CiN+JUaK8Y9EIT/MuSMiIYkv7xFGCFLiNbeutB MxvZ9LWWAVCxgl/Blbi8FGNmlQcsVeqGhT/vKDNfda61b1PbiDfqKYBBCw9Ci+1E4ByS DE4PAIcoCZpO2uviv/nuVIdmjSgbKRhMtlf0ZzjlI3yvtSd6Ru/e2SkPm/vaO5xvImTK T6gMBEuZmFgU/qqu7GjX+EaKSio717p4YYl84pOX0xEpIUyOdxs+H5jBVpqP2B+16Pur BOIf8ARWUoi+SS3VGNXtHsZaRarAGS8mXAz1BB8vej0gvWLVibPzjijiM3TYpEP+XrsN MkbA== X-Forwarded-Encrypted: i=1; AJvYcCXZRJ8DUdkrCJcGGkoHvilNvWiSQrGqXfSHOOfKy7P2HN1uTTJngLWh+jP3JjNj0DhbeB5bS1I=@vger.kernel.org X-Gm-Message-State: AOJu0YwnQ35s3HKu8wzklgcN9b2pwG6Xd6OwILr+djOUbA6ReGSqr3p4 I3sQTakuLotMc8n01639KowvRzTvYt6rz04ZCmPy0veN7egg8fXe X-Google-Smtp-Source: AGHT+IGuc9HxuUtVZ/7LheyhiZWU8gxyIjufa2GjwQ7uvRWNvozrn52GobYqVaKJXYbOJ5u3eP5D7g== X-Received: by 2002:a17:902:ccc1:b0:20b:951f:6dff with SMTP id d9443c01a7336-20bfd9987c4mr222171255ad.0.1728381106985; Tue, 08 Oct 2024 02:51:46 -0700 (PDT) Received: from KERNELXING-MB0.tencent.com ([43.132.141.21]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-20c138cfd25sm52527345ad.73.2024.10.08.02.51.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Oct 2024 02:51:46 -0700 (PDT) From: Jason Xing To: davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, dsahern@kernel.org, willemdebruijn.kernel@gmail.com, willemb@google.com, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com, jolsa@kernel.org Cc: bpf@vger.kernel.org, netdev@vger.kernel.org, Jason Xing Subject: [PATCH net-next 6/9] net-timestamp: add tx OPT_ID_TCP support for bpf case Date: Tue, 8 Oct 2024 17:51:06 +0800 Message-Id: <20241008095109.99918-7-kerneljasonxing@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20241008095109.99918-1-kerneljasonxing@gmail.com> References: <20241008095109.99918-1-kerneljasonxing@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org From: Jason Xing We can set OPT_ID|OPT_ID_TCP before we initialize the last skb from each sendmsg. We only set the socket once like how we use setsockopt() with OPT_ID|OPT_ID_TCP flags. Signed-off-by: Jason Xing --- net/core/skbuff.c | 16 +++++++++++++--- net/ipv4/tcp.c | 19 +++++++++++++++---- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/net/core/skbuff.c b/net/core/skbuff.c index 2b1b2f7d271a..a60aec450970 100644 --- a/net/core/skbuff.c +++ b/net/core/skbuff.c @@ -5540,6 +5540,7 @@ void skb_complete_tx_timestamp(struct sk_buff *skb, EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp); static bool bpf_skb_tstamp_tx(struct sock *sk, u32 scm_flag, + struct sk_buff *skb, struct skb_shared_hwtstamps *hwtstamps) { struct tcp_sock *tp; @@ -5550,7 +5551,7 @@ static bool bpf_skb_tstamp_tx(struct sock *sk, u32 scm_flag, tp = tcp_sk(sk); if (BPF_SOCK_OPS_TEST_FLAG(tp, BPF_SOCK_OPS_TX_TIMESTAMPING_OPT_CB_FLAG)) { struct timespec64 tstamp; - u32 cb_flag; + u32 cb_flag, key = 0; switch (scm_flag) { case SCM_TSTAMP_SCHED: @@ -5566,11 +5567,20 @@ static bool bpf_skb_tstamp_tx(struct sock *sk, u32 scm_flag, return true; } + /* We require user to set OPT_ID_TCP to generate key value + * in a robust way. + */ + if (READ_ONCE(sk->sk_tsflags) & SOF_TIMESTAMPING_OPT_ID && + READ_ONCE(sk->sk_tsflags) & SOF_TIMESTAMPING_OPT_ID_TCP) { + key = skb_shinfo(skb)->tskey; + key -= atomic_read(&sk->sk_tskey); + } + if (hwtstamps) tstamp = ktime_to_timespec64(hwtstamps->hwtstamp); else tstamp = ktime_to_timespec64(ktime_get_real()); - tcp_call_bpf_2arg(sk, cb_flag, tstamp.tv_sec, tstamp.tv_nsec); + tcp_call_bpf_3arg(sk, cb_flag, key, tstamp.tv_sec, tstamp.tv_nsec); return true; } @@ -5589,7 +5599,7 @@ void __skb_tstamp_tx(struct sk_buff *orig_skb, if (!sk) return; - if (bpf_skb_tstamp_tx(sk, tstype, hwtstamps)) + if (bpf_skb_tstamp_tx(sk, tstype, orig_skb, hwtstamps)) return; tsflags = READ_ONCE(sk->sk_tsflags); diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c index ddf4089779b5..1d52640f9ff4 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 u32 bpf_tcp_tx_timestamp(struct sock *sk) +static u32 bpf_tcp_tx_timestamp(struct sock *sk, int copied) { u32 flags; @@ -491,10 +491,21 @@ static u32 bpf_tcp_tx_timestamp(struct sock *sk) if (!(flags & SOF_TIMESTAMPING_TX_RECORD_MASK)) return 0; + /* We require users to set both OPT_ID and OPT_ID_TCP flags + * together here, or else the key might be inaccurate. + */ + if (flags & SOF_TIMESTAMPING_OPT_ID && + flags & SOF_TIMESTAMPING_OPT_ID_TCP && + !(sk->sk_tsflags & (SOF_TIMESTAMPING_OPT_ID | SOF_TIMESTAMPING_OPT_ID_TCP))) { + atomic_set(&sk->sk_tskey, (tcp_sk(sk)->write_seq - copied)); + sk->sk_tsflags |= (SOF_TIMESTAMPING_OPT_ID | SOF_TIMESTAMPING_OPT_ID_TCP); + } + return flags; } -static void tcp_tx_timestamp(struct sock *sk, struct sockcm_cookie *sockc) +static void tcp_tx_timestamp(struct sock *sk, struct sockcm_cookie *sockc, + int copied) { struct sk_buff *skb = tcp_write_queue_tail(sk); u32 tsflags = sockc->tsflags; @@ -503,7 +514,7 @@ static void tcp_tx_timestamp(struct sock *sk, struct sockcm_cookie *sockc) if (!skb) return; - flags = bpf_tcp_tx_timestamp(sk); + flags = bpf_tcp_tx_timestamp(sk, copied); if (flags) tsflags = flags; @@ -1347,7 +1358,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, copied); tcp_push(sk, flags, mss_now, tp->nonagle, size_goal); } out_nopush: From patchwork Tue Oct 8 09:51:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Xing X-Patchwork-Id: 13826143 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7E1FF13B5B6; Tue, 8 Oct 2024 09:51:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728381113; cv=none; b=groCkKNFPJPcJaMAA7O/++JKGfp+Y3hbDVXOe9HB8m9QN1+48pQqOW1Lo+Np6obGRUaDKg+pjpzvsm3xXVbDOebrDPJhgQrTTb7UPxBaGzAaU1/YDc0b6IztqHZkOZ9sKfoKe15sjRGtx++hpILDvwdyEYOhoBEP0B8Uc6V8HSw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728381113; c=relaxed/simple; bh=xFBByvFkLIu4AQ27aNKVqCxmgrN9nMCQNauZKfY+88U=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=qhjMc6fqHEWaAGlzAk+0LxzBq4MbXmJeNWSwJSiET/N7k04GqTIgq2Q15m6+THxB64zQRWIjpR2WE1l0uL5WXk19/Opl4gr7cIzGsMLAH0thfBeVaq3IKuY4+moGXlIAGVifKE7+0FGpbylAbcH2FufwhOEbeHRh/BEPk/zEBK0= 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=J20Bhgsk; arc=none smtp.client-ip=209.85.214.174 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="J20Bhgsk" Received: by mail-pl1-f174.google.com with SMTP id d9443c01a7336-20c544d345cso3482505ad.1; Tue, 08 Oct 2024 02:51:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728381112; x=1728985912; 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=/11O91Y2noQGtDmKmdMCq9aNUAJv1qVR10ooAo+aIXE=; b=J20BhgskpHD036l6J+J0g+WxZ48Cq7aKD4T636z9wkSQujWeD925SU2gZFm4aOsw8Y Twabo58EqydTsjs+BtM/O6wZZY4YJN9FWFhtncNDkHml4oCo1kZ+SzI/bsgw6+DEgUXm H9wZSJg4EdJ8wezYKbg0mW3odHykIvKIo2ueeoTus9Ioov8esrvIC+6AOPWh4+x5nP/M Lwm5akyiDfnZvjJPE0ZQowdv3fTTNoBsmf8vQARqa28bbFV3R2UXVCPYedaLcYHDcG0F OnqT2PtR59GeY2ZSWRdJOli2klJ3ak5UVGDQ/LCq4/PbXTkzPinGBvTD8jVRE+FpHkMx UJbw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728381112; x=1728985912; 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=/11O91Y2noQGtDmKmdMCq9aNUAJv1qVR10ooAo+aIXE=; b=OmzvblyhahZ0QmIXrvyXyQ+CAnzH7JJK+Yx3pgvOJvLprPax7MQe/fo97N4fV6g9q9 cZZR1IwDyXKpf+hZfycUdUo6Zh1JnWKfPd6i+I1WlDJm9cb1PXRQpzuL+theZljStqXh jUT8pJi9SNoI+mWXjbQRc7bHWQ/zADJIdwCPs96U9RBZLD/88yF14yNPwOD5OtfB+SJ9 ZQ6ifPNJihM1IAbDcHXzu29GUzvUnkzCIn/ht8uFHDtkYZPkFeC/GqH+h4PJ2zGEGG8J wtM8zC/GQnCXOwuSQUFiArazB1JfvIIgCTOCeIU2H39Nv0qsWg/3afy2A5ZS0aE26MVa ReRw== X-Forwarded-Encrypted: i=1; AJvYcCV5meUXP4njNNOIW432KDhSS+54qXRmGrm6r4duEStzRUj3yZLuR1Uw26rhnwnHkucFCkGB67U=@vger.kernel.org X-Gm-Message-State: AOJu0YxCINmHzg5Ly2SZqQYSaXfe8bCbAZ4ZfTxqNrpL+O2DcX4CU0vC H7dSa2Rn7fwPnCcrvngXJ0fziNiaCMwZtM5p56d8zzG2QP3SK1MI3alvlQ== X-Google-Smtp-Source: AGHT+IF0opgbN45nBSvuuEM8DQ9bXHWOWn6ywaXJ/mPro9T0DAwTBOueT9K5q4kq2bL7pSVRWdZz5A== X-Received: by 2002:a17:902:ce0a:b0:205:4d27:616e with SMTP id d9443c01a7336-20c4e3164bamr45045205ad.22.1728381111818; Tue, 08 Oct 2024 02:51:51 -0700 (PDT) Received: from KERNELXING-MB0.tencent.com ([43.132.141.21]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-20c138cfd25sm52527345ad.73.2024.10.08.02.51.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Oct 2024 02:51:51 -0700 (PDT) From: Jason Xing To: davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, dsahern@kernel.org, willemdebruijn.kernel@gmail.com, willemb@google.com, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com, jolsa@kernel.org Cc: bpf@vger.kernel.org, netdev@vger.kernel.org, Jason Xing Subject: [PATCH net-next 7/9] net-timestamp: open gate for bpf_setsockopt Date: Tue, 8 Oct 2024 17:51:07 +0800 Message-Id: <20241008095109.99918-8-kerneljasonxing@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20241008095109.99918-1-kerneljasonxing@gmail.com> References: <20241008095109.99918-1-kerneljasonxing@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org From: Jason Xing Now we allow users to set tsflags through bpf_setsockopt. What I want to do is passing SOF_TIMESTAMPING_RX_SOFTWARE flag, so that we can generate rx timestamps the moment the skb traverses through driver. Here is an example: case BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB: case BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB: sock_opt = SOF_TIMESTAMPING_RX_SOFTWARE; bpf_setsockopt(skops, SOL_SOCKET, SO_TIMESTAMPING, &sock_opt, sizeof(sock_opt)); break; In this way, we can use bpf program that help us generate and report rx timestamp. Signed-off-by: Jason Xing --- net/core/filter.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/net/core/filter.c b/net/core/filter.c index bd0d08bf76bb..9ce99d320571 100644 --- a/net/core/filter.c +++ b/net/core/filter.c @@ -5225,6 +5225,9 @@ static int sol_socket_sockopt(struct sock *sk, int optname, break; case SO_BINDTODEVICE: break; + case SO_TIMESTAMPING_NEW: + case SO_TIMESTAMPING_OLD: + break; default: return -EINVAL; } From patchwork Tue Oct 8 09:51:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Xing X-Patchwork-Id: 13826144 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 682BC19306A; Tue, 8 Oct 2024 09:51:57 +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=1728381118; cv=none; b=NVzO8jHrEpbvwQdkNgmVja89GA41o103a6n0EU2MKKoNfDVhF/tzV+FoD8S9Hy4I25ydQK1Qu39ZYmj5ec7XsIDwEGEvej0iGpijc0fcQtznhC5TIXc8x47nx9f0Hm1YHMzUHq4Vd/DHk9TivRgmuURysYsPnDOsEHhZMVqXzBg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728381118; c=relaxed/simple; bh=9s9tiUCmpImBPc47pKVxWqPQw0HbL0lnTHINMAEmuNU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=KfrSHfFOetsEyGx7lBiq9i3aU8yqoPUUSFJ15Z0gwrxziks8OvNohNhJ33jcCLnXfKzrv5RDavgB3Oc8fpUXWkdtUwZtRLlb0B1HzW9GFa3nU54A/dqiwiS5NNMTi7kE0zx1XJW/P7uln30ttS7C8IldVfUVRrgHqFrswbJIQYk= 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=IpBXrqkz; 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="IpBXrqkz" Received: by mail-pl1-f181.google.com with SMTP id d9443c01a7336-20c56b816faso4415525ad.2; Tue, 08 Oct 2024 02:51:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728381117; x=1728985917; 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=CIhWhCgJWsvdGp4bMg47enlFeTpz7yKdIzPKml3Bzck=; b=IpBXrqkz+04LYEbvW1CnD0SeDcq2QAM/ScqDgTZn7k98I074WffEy4Kna5ASWaVce9 dpEfUyGZHg1TlHnL4TDNPghM7NVTv/3iEB3B1ZGRD4AXwmFiCyfRDGcsK8GbZPNA5GWf rmejvm38ZCOi0lP7sb277Fj7Ab6LUNazrzS2Dg/9L1wdwQWJF+TR9TNhRDJCBg1gJwuO zQz1JMnE8C8CCp/30UFopRSx9dn8fvoVrtwF0ZhYB2bzlmoceAJnhjWg8ml5iDSlvb4C +V9LnaldPrhSbXVOR6ZGYxkPC13WC97cuEeYEn9BCXJYpZewtLJj/RqcaUBJdRBJom5+ HJ5w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728381117; x=1728985917; 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=CIhWhCgJWsvdGp4bMg47enlFeTpz7yKdIzPKml3Bzck=; b=e9doNbBh7DseilIJZk1aRgH4fp7ao3Ne8xXUqcpjj5S4NaDOmdW/imS2xOIzaO38zr 2zEXzlfAS986HpWgD0FLETfYiRV2+8MpZh/Ml+FrReght/jt+0wwO9PrkOQ9Mpb+YXDL MwB7/BSfkqgbSp+O5iW6ZjbEzF3qPphyo6t34NcWghqdRH9Bn/JlC/hJ5XNs9xJjOJ/I 9sqNvoq3BNZPMxe1St/6CBssfC/qbTqWtjFYCNagcsp+4BwxBr+Sy8JMGt37gosTJGz7 rM40VoDl580SwkgjqLtaAeBiOPOJooFTY4n4BPJTPSh0BtiSVbVa7HzZdWPCxMv91TZS 85kw== X-Forwarded-Encrypted: i=1; AJvYcCVsiFHXljpNtxCCnDJpl20JFrvrUjSnF1kw/I/HIUm8rvLMioBxBjxys12RwrFtsBZG0Kl8mEo=@vger.kernel.org X-Gm-Message-State: AOJu0Yzb14zT/mTePE3dmWVm1xL8o6GgrOAupUbOIydg8yuukfSSCteY wI+S09Enlx6KY5tdSUvOh58kqlWVnD9HyPAmSOZljntzn7HCqfIeY7+tkg== X-Google-Smtp-Source: AGHT+IGbZeqMg/RUtIu9e4yP9Z2QIgfcjPygcmyFmZ7EqlalK9G12Uirt1yaXE5WRNcPyxbENMD4cg== X-Received: by 2002:a17:902:ec8f:b0:20b:7731:e3dd with SMTP id d9443c01a7336-20bfdf6d0damr227314535ad.2.1728381116690; Tue, 08 Oct 2024 02:51:56 -0700 (PDT) Received: from KERNELXING-MB0.tencent.com ([43.132.141.21]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-20c138cfd25sm52527345ad.73.2024.10.08.02.51.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Oct 2024 02:51:56 -0700 (PDT) From: Jason Xing To: davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, dsahern@kernel.org, willemdebruijn.kernel@gmail.com, willemb@google.com, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com, jolsa@kernel.org Cc: bpf@vger.kernel.org, netdev@vger.kernel.org, Jason Xing Subject: [PATCH net-next 8/9] net-timestamp: add bpf framework for rx timestamps Date: Tue, 8 Oct 2024 17:51:08 +0800 Message-Id: <20241008095109.99918-9-kerneljasonxing@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20241008095109.99918-1-kerneljasonxing@gmail.com> References: <20241008095109.99918-1-kerneljasonxing@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org From: Jason Xing Prepare for later changes in this series. Here I use u32 for bpf_sock_ops_cb_flags for better extension and introduce a new rx bpf flag to control separately. Main change is let userside set through bpf_setsockopt() for SO_TIMESTAMPING feature. Signed-off-by: Jason Xing --- include/linux/tcp.h | 2 +- include/uapi/linux/bpf.h | 5 ++++- net/ipv4/tcp.c | 13 +++++++++++++ tools/include/uapi/linux/bpf.h | 5 ++++- 4 files changed, 22 insertions(+), 3 deletions(-) diff --git a/include/linux/tcp.h b/include/linux/tcp.h index 6a5e08b937b3..e21fd3035962 100644 --- a/include/linux/tcp.h +++ b/include/linux/tcp.h @@ -446,7 +446,7 @@ struct tcp_sock { /* Sock_ops bpf program related variables */ #ifdef CONFIG_BPF - u8 bpf_sock_ops_cb_flags; /* Control calling BPF programs + u32 bpf_sock_ops_cb_flags; /* Control calling BPF programs * values defined in uapi/linux/tcp.h */ u8 bpf_chg_cc_inprogress:1; /* In the middle of diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 6bf3f2892776..3c28d74d14ea 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -6903,8 +6903,11 @@ enum { /* Call bpf when the kernel is generating tx timestamps. */ BPF_SOCK_OPS_TX_TIMESTAMPING_OPT_CB_FLAG = (1<<7), + /* Call bpf when the kernel is generating rx timestamps. + */ + BPF_SOCK_OPS_RX_TIMESTAMPING_OPT_CB_FLAG = (1<<8), /* Mask of all currently supported cb flags */ - BPF_SOCK_OPS_ALL_CB_FLAGS = 0xFF, + BPF_SOCK_OPS_ALL_CB_FLAGS = 0x1FF, }; /* List of known BPF sock_ops operators. diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c index 1d52640f9ff4..938e2bff4fa6 100644 --- a/net/ipv4/tcp.c +++ b/net/ipv4/tcp.c @@ -2276,6 +2276,16 @@ static int tcp_zerocopy_receive(struct sock *sk, } #endif +static bool tcp_bpf_recv_timestamp(struct sock *sk, struct scm_timestamping_internal *tss) +{ + struct tcp_sock *tp = tcp_sk(sk); + + if (BPF_SOCK_OPS_TEST_FLAG(tp, BPF_SOCK_OPS_RX_TIMESTAMPING_OPT_CB_FLAG)) + return true; + + return false; +} + /* Similar to __sock_recv_timestamp, but does not require an skb */ void tcp_recv_timestamp(struct msghdr *msg, const struct sock *sk, struct scm_timestamping_internal *tss) @@ -2284,6 +2294,9 @@ void tcp_recv_timestamp(struct msghdr *msg, const struct sock *sk, u32 tsflags = READ_ONCE(sk->sk_tsflags); bool has_timestamping = false; + if (tcp_bpf_recv_timestamp(sk, tss)) + return; + if (tss->ts[0].tv_sec || tss->ts[0].tv_nsec) { if (sock_flag(sk, SOCK_RCVTSTAMP)) { if (sock_flag(sk, SOCK_RCVTSTAMPNS)) { diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index d3bf538846da..ff17cd820bde 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -6902,8 +6902,11 @@ enum { /* Call bpf when the kernel is generating tx timestamps. */ BPF_SOCK_OPS_TX_TIMESTAMPING_OPT_CB_FLAG = (1<<7), + /* Call bpf when the kernel is generating rx timestamps. + */ + BPF_SOCK_OPS_RX_TIMESTAMPING_OPT_CB_FLAG = (1<<8), /* Mask of all currently supported cb flags */ - BPF_SOCK_OPS_ALL_CB_FLAGS = 0xFF, + BPF_SOCK_OPS_ALL_CB_FLAGS = 0x1FF, }; /* List of known BPF sock_ops operators. From patchwork Tue Oct 8 09:51:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Xing X-Patchwork-Id: 13826145 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-pl1-f171.google.com (mail-pl1-f171.google.com [209.85.214.171]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4187C199225; Tue, 8 Oct 2024 09:52:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728381123; cv=none; b=XzQxbzq2pZVtOS6lALhCVnz4fWRYTvSx99WLiDW/fIqWrIH2BUlDnbfJZiSNkGQx+q57YLVcswWGmYgoj9FV/lR3q/alOyEFeP+XaThusr/mRNT+VpiCguyBHh+xcQ1XMk7ZxD4vzKIgpFMsLuVRMFYhmh1trHsEzRcNuuoFB2c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728381123; c=relaxed/simple; bh=MJshw9h40ymJ3Nc5HZrkDmJA46p/ympzLgaX5/Ab3AM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=VDxWWoTFmpIl/94P8WzHuu2wQJcZqEUjNPfzM6KuXmmQxqbXILyOxrnh3TwhADtGTqzM3B/JH9Fz0qT0Chgg5t7MjU8ubqxo1u8hLBLz3jA/OmrxKE08yAXlxRoitC9n+0q0j/L34Z1curKrTL0YtUuHdOzpT7nICr85YHSzgXY= 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=eT4qJswF; arc=none smtp.client-ip=209.85.214.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="eT4qJswF" Received: by mail-pl1-f171.google.com with SMTP id d9443c01a7336-20b7eb9e81eso64436535ad.2; Tue, 08 Oct 2024 02:52:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728381122; x=1728985922; 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=QUComqhPeUxBF0aXy60LKQQCYmV3hHWBJE0MF5/cSug=; b=eT4qJswF5N860zomFa1gdpM31oc/eIcBQPTL8qcL+rqj4WFVrSBmuGmxipJjN355bA kM7L4rzIVEY5KZ+VTv5obxaRcYXwik66Sj5PEw+a9i87q6f9Rib4bGPGZFxyyCOYYu00 nDVt6oQqUVc5sOYU/3cpOhKIB8XO+mpc6sixf0ARwmbuZjCj0d+8RXI67Okm9wsDyu22 y5Ga8c1s586nh0Cbg8wbZXy4PsCpAGlAWj1P0lrb6WU73UngATBJIPHTszP7agpj/dt8 q6dPpO0gIW5nVjGy8NGUDK57TExErgOLbEg/qMLWNOk27xyqLM17qqZwrwROBmcDt+90 WRvw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728381122; x=1728985922; 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=QUComqhPeUxBF0aXy60LKQQCYmV3hHWBJE0MF5/cSug=; b=eVaNjZJdTrdrBuqTPy1H1uvMB7uqpiVZ4azJ47S+GRaKpS6jXeo/gZzN2FLTNeYYjm 9oZ7OPslN6qTV52bl/nLVgE9kRz/Ekn/6mrR6hcWegPTCI31OG0pvXsFyWno7M3M0bjy DWXTyvDYDsd941RMTBfzfkz+xqDPM+AmIG6SNdY1lzH9JVLFAH6rUarOGP0f4QBZK9Sl iiOiJ9WZCuyI2Em5+YZBL2QSZrOextTMoy33Uof99i9SCizXCJX+QpyizNFaEOhNgvoB rV4PCnEIvinX8PElTwkdohPh8Lh25tWG0PqelA1Am7prJM6V91gR+UblLhx/dCoOBO1N xo8Q== X-Forwarded-Encrypted: i=1; AJvYcCW8bx/i8ZNk72pP+moolnSKQ4Ikd9pTy5hmrOWJ2xYKQVYvOBS8gQQAfwSoo1dLILR6cXFquwg=@vger.kernel.org X-Gm-Message-State: AOJu0YwjersDqjWoLWCpgnk4oOrGiM4hGqgQjXAG3yya82ZVBfMEDRHG tIEROJm26VScbsgu6Yqgmy1sRfvK9ejngdURBHGZTvwN1nHS5DwTO3GLig== X-Google-Smtp-Source: AGHT+IELp7iJAl1g8RI4hS7+vxFcLa36F2p1sSzf3KervtMfeEq37sWtPBKzjDFft6AlQ0rnKrBJJw== X-Received: by 2002:a17:903:2452:b0:20b:8c13:533f with SMTP id d9443c01a7336-20bfdff0141mr175711685ad.24.1728381121713; Tue, 08 Oct 2024 02:52:01 -0700 (PDT) Received: from KERNELXING-MB0.tencent.com ([43.132.141.21]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-20c138cfd25sm52527345ad.73.2024.10.08.02.51.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Oct 2024 02:52:01 -0700 (PDT) From: Jason Xing To: davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, dsahern@kernel.org, willemdebruijn.kernel@gmail.com, willemb@google.com, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com, jolsa@kernel.org Cc: bpf@vger.kernel.org, netdev@vger.kernel.org, Jason Xing Subject: [PATCH net-next 9/9] net-timestamp: add bpf support for rx software/hardware timestamp Date: Tue, 8 Oct 2024 17:51:09 +0800 Message-Id: <20241008095109.99918-10-kerneljasonxing@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20241008095109.99918-1-kerneljasonxing@gmail.com> References: <20241008095109.99918-1-kerneljasonxing@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org From: Jason Xing Now it's time to let the bpf for rx timestamp take effect. Signed-off-by: Jason Xing --- include/net/tcp.h | 14 ++++++++++++++ include/uapi/linux/bpf.h | 5 +++++ net/ipv4/tcp.c | 28 +++++++++++++++++++++++++++- tools/include/uapi/linux/bpf.h | 5 +++++ 4 files changed, 51 insertions(+), 1 deletion(-) diff --git a/include/net/tcp.h b/include/net/tcp.h index 739a9fb83d0c..416a039da472 100644 --- a/include/net/tcp.h +++ b/include/net/tcp.h @@ -2676,6 +2676,14 @@ static inline int tcp_call_bpf_3arg(struct sock *sk, int op, u32 arg1, u32 arg2, return tcp_call_bpf(sk, op, 3, args); } +static inline int tcp_call_bpf_4arg(struct sock *sk, int op, u32 arg1, u32 arg2, + u32 arg3, u32 arg4) +{ + u32 args[4] = {arg1, arg2, arg3, arg4}; + + return tcp_call_bpf(sk, op, 4, args); +} + #else static inline int tcp_call_bpf(struct sock *sk, int op, u32 nargs, u32 *args) { @@ -2693,6 +2701,12 @@ static inline int tcp_call_bpf_3arg(struct sock *sk, int op, u32 arg1, u32 arg2, return -EPERM; } +static inline int tcp_call_bpf_4arg(struct sock *sk, int op, u32 arg1, u32 arg2, + u32 arg3, u32 arg4) +{ + return -EPERM; +} + #endif static inline u32 tcp_timeout_init(struct sock *sk) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 3c28d74d14ea..ffaa483f1362 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -7045,6 +7045,11 @@ enum { * flag for other three tx timestamp * use. */ + BPF_SOCK_OPS_TS_RX_OPT_CB, /* Called when tcp layer tries to + * receive skbs with timestamps when + * SO_TIMESTAMPING feature is on + * It indicates the recorded timestamp. + */ }; /* List of TCP states. There is a build check in net/ipv4/tcp.c to detect diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c index 938e2bff4fa6..f6addd26db9f 100644 --- a/net/ipv4/tcp.c +++ b/net/ipv4/tcp.c @@ -2278,10 +2278,36 @@ static int tcp_zerocopy_receive(struct sock *sk, static bool tcp_bpf_recv_timestamp(struct sock *sk, struct scm_timestamping_internal *tss) { + u32 tsflags = READ_ONCE(sk->sk_tsflags); struct tcp_sock *tp = tcp_sk(sk); - if (BPF_SOCK_OPS_TEST_FLAG(tp, BPF_SOCK_OPS_RX_TIMESTAMPING_OPT_CB_FLAG)) + if (BPF_SOCK_OPS_TEST_FLAG(tp, BPF_SOCK_OPS_RX_TIMESTAMPING_OPT_CB_FLAG)) { + u32 hw_sec, hw_nsec, sw_sec, sw_nsec; + + if (!(tsflags & (SOF_TIMESTAMPING_RX_SOFTWARE | + SOF_TIMESTAMPING_RX_HARDWARE))) + return true; + + if (tsflags & SOF_TIMESTAMPING_RX_SOFTWARE) { + sw_sec = tss->ts[0].tv_sec; + sw_nsec = tss->ts[0].tv_nsec; + } else { + sw_sec = 0; + sw_nsec = 0; + } + + if (tsflags & SOF_TIMESTAMPING_RX_HARDWARE) { + hw_sec = tss->ts[2].tv_sec; + hw_nsec = tss->ts[2].tv_nsec; + } else { + hw_sec = 0; + hw_nsec = 0; + } + + tcp_call_bpf_4arg(sk, BPF_SOCK_OPS_TS_RX_OPT_CB, + sw_sec, sw_nsec, hw_sec, hw_nsec); return true; + } return false; } diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index ff17cd820bde..8a87fee2e012 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -7044,6 +7044,11 @@ enum { * flag for other three tx timestamp * use. */ + BPF_SOCK_OPS_TS_RX_OPT_CB, /* Called when tcp layer tries to + * receive skbs with timestamps when + * SO_TIMESTAMPING feature is on + * It indicates the recorded timestamp. + */ }; /* List of TCP states. There is a build check in net/ipv4/tcp.c to detect