BPF List
 help / color / mirror / Atom feed
From: Nimrod Oren <noren@nvidia.com>
To: "David S. Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@google.com>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	Simon Horman <horms@kernel.org>, Shuah Khan <shuah@kernel.org>,
	Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Jesper Dangaard Brouer <hawk@kernel.org>,
	"John Fastabend" <john.fastabend@gmail.com>,
	Stanislav Fomichev <sdf@fomichev.me>,
	Mohsin Bashir <mohsin.bashr@gmail.com>
Cc: Dragos Tatulea <dtatulea@nvidia.com>,
	Tariq Toukan <tariqt@nvidia.com>,
	Carolina Jubran <cjubran@nvidia.com>, <netdev@vger.kernel.org>,
	<linux-kselftest@vger.kernel.org>, <linux-kernel@vger.kernel.org>,
	<bpf@vger.kernel.org>, Nimrod Oren <noren@nvidia.com>
Subject: [PATCH RFC net-next 3/5] selftests: drv-net: Test XDP head adjustment with bpf_dynptr
Date: Tue, 9 Sep 2025 11:52:34 +0300	[thread overview]
Message-ID: <20250909085236.2234306-4-noren@nvidia.com> (raw)
In-Reply-To: <20250909085236.2234306-1-noren@nvidia.com>

Update xdp_adjst_head_shrnk_data to use bpf_dynptr_slice to read the
data right after the headers, instead of bpf_xdp_load_bytes.

This may avoid a copy by returning a direct pointer to the dynptr data.

Also, use bpf_dynptr_read and bpf_dynptr_write to move the headers,
instead of bpf_xdp_load_bytes and bpf_xdp_store_bytes.

Signed-off-by: Nimrod Oren <noren@nvidia.com>
Reviewed-by: Carolina Jubran <cjubran@nvidia.com>
Reviewed-by: Dragos Tatulea <dtatulea@nvidia.com>
---
 .../selftests/net/lib/xdp_native.bpf.c        | 35 +++++++------------
 1 file changed, 12 insertions(+), 23 deletions(-)

diff --git a/tools/testing/selftests/net/lib/xdp_native.bpf.c b/tools/testing/selftests/net/lib/xdp_native.bpf.c
index ff768fbc8606..71172d32c529 100644
--- a/tools/testing/selftests/net/lib/xdp_native.bpf.c
+++ b/tools/testing/selftests/net/lib/xdp_native.bpf.c
@@ -398,10 +398,11 @@ static int xdp_adjst_tail(struct xdp_md *ctx, __u16 port)
 static int xdp_adjst_head_shrnk_data(struct xdp_md *ctx, __u64 hdr_len,
 				     __u32 offset)
 {
-	char tmp_buff[MAX_ADJST_OFFSET];
+	char tmp_buff[MAX_ADJST_OFFSET] = {0};
+	struct bpf_dynptr ptr;
 	struct udphdr *udph;
-	void *offset_ptr;
 	__u32 udp_csum = 0;
+	void *data = NULL;
 
 	/* Update the length information in the IP and UDP headers before
 	 * adjusting the headroom. This simplifies accessing the relevant
@@ -414,37 +415,25 @@ static int xdp_adjst_head_shrnk_data(struct xdp_md *ctx, __u64 hdr_len,
 	if (!udph)
 		return -1;
 
-	offset = (offset & 0x1ff) >= MAX_ADJST_OFFSET ? MAX_ADJST_OFFSET :
-				     offset & 0xff;
-	if (offset == 0)
-		return -1;
-
-	if (bpf_xdp_load_bytes(ctx, hdr_len, tmp_buff, offset) < 0)
+	bpf_dynptr_from_xdp(ctx, 0, &ptr);
+	data = bpf_dynptr_slice(&ptr, hdr_len, tmp_buff, sizeof(tmp_buff));
+	if (!data)
 		return -1;
 
-	udp_csum = bpf_csum_diff((__be32 *)tmp_buff, offset, 0, 0, udp_csum);
+	udp_csum = bpf_csum_diff(data, offset, 0, 0, udp_csum);
 
 	udph->check = (__u16)csum_fold_helper(udp_csum);
 
-	if (bpf_xdp_load_bytes(ctx, 0, tmp_buff, MAX_ADJST_OFFSET) < 0)
-		return -1;
-
-	if (bpf_xdp_adjust_head(ctx, offset) < 0)
-		return -1;
-
-	if (offset > MAX_ADJST_OFFSET)
-		return -1;
-
-	if (hdr_len > MAX_ADJST_OFFSET || hdr_len == 0)
-		return -1;
-
 	/* Added here to handle clang complain about negative value */
 	hdr_len = hdr_len & 0xff;
 
-	if (hdr_len == 0)
+	if (bpf_dynptr_read(tmp_buff, hdr_len, &ptr, 0, 0) < 0)
 		return -1;
 
-	if (bpf_xdp_store_bytes(ctx, 0, tmp_buff, hdr_len) < 0)
+	if (bpf_dynptr_write(&ptr, offset, tmp_buff, hdr_len, 0) < 0)
+		return -1;
+
+	if (bpf_xdp_adjust_head(ctx, offset) < 0)
 		return -1;
 
 	return 0;
-- 
2.45.0


  parent reply	other threads:[~2025-09-09  8:53 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-09-09  8:52 [PATCH RFC net-next 0/5] selftests: drv-net: Convert XDP program to bpf_dynptr Nimrod Oren
2025-09-09  8:52 ` [PATCH RFC net-next 1/5] selftests: drv-net: Test XDP_TX with bpf_dynptr Nimrod Oren
2025-09-09  8:52 ` [PATCH RFC net-next 2/5] selftests: drv-net: Test XDP tail adjustment " Nimrod Oren
2025-09-09  8:52 ` Nimrod Oren [this message]
2025-09-09 17:26   ` [PATCH RFC net-next 3/5] selftests: drv-net: Test XDP head " Martin KaFai Lau
2025-09-09  8:52 ` [PATCH RFC net-next 4/5] selftests: drv-net: Adjust XDP header data " Nimrod Oren
2025-09-09  8:52 ` [PATCH RFC net-next 5/5] selftests: drv-net: Check " Nimrod Oren
2025-09-09 21:12 ` [PATCH RFC net-next 0/5] selftests: drv-net: Convert XDP program to bpf_dynptr Jakub Kicinski

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250909085236.2234306-4-noren@nvidia.com \
    --to=noren@nvidia.com \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=cjubran@nvidia.com \
    --cc=daniel@iogearbox.net \
    --cc=davem@davemloft.net \
    --cc=dtatulea@nvidia.com \
    --cc=edumazet@google.com \
    --cc=hawk@kernel.org \
    --cc=horms@kernel.org \
    --cc=john.fastabend@gmail.com \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=mohsin.bashr@gmail.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=sdf@fomichev.me \
    --cc=shuah@kernel.org \
    --cc=tariqt@nvidia.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox