From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f169.google.com (mail-pl1-f169.google.com [209.85.214.169]) (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 056A22D9EFA for ; Fri, 19 Sep 2025 23:10:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.169 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758323403; cv=none; b=hgFXFVhvgvqOTpnxRAjlMaz59bAFWB2d+kKmL7+ob5rTHVtS8hFppipirf7U8LwecVJXB2HVRAI1ZGbOPuWbuEB+LSQt3l4azsfWv+02G91X0lyyCSL4M8IsKZ/HnhXUD1cnihymh0d1G593zQ6AlKweSwAp1Lzsx8zkUofOWik= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758323403; c=relaxed/simple; bh=nDmjbXMAaaARovoem/jblg2PIL/t10TLTSxc9dzhj3Y=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=TtBIc9plXsjGGTbHvUfulLW39eao1UnSnTl3WVIKDQYvmJLor3JwX4HmzlnYa/4ZBGZmTnHb9cKAmqK7mC+1616weVREH8GObrTs1FZ/goM/SuWL13/U8ze5rngv+9WDEHYnHlVRazCm0AT6cl6V/dJaOPaCYTYaFVW47tJpbKI= 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=jrMiTKuJ; arc=none smtp.client-ip=209.85.214.169 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="jrMiTKuJ" Received: by mail-pl1-f169.google.com with SMTP id d9443c01a7336-26f0c5455e2so3051085ad.3 for ; Fri, 19 Sep 2025 16:10:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1758323400; x=1758928200; 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=OETsLPmcZGNpLSshaaiLeOj8LUgrE6er4wP4vFEBZK0=; b=jrMiTKuJVOCtcsqj9vz5zc73MAgAZkrkhb3rNlxqsqo/v8zEtqePb95Z4D9Ff1qJ12 hBaP9YI3snKMnvAN9OczzkLQgSNDLYneGmWlChMhviBj0RxnnS1XBmBhg080TA1rGDEo vbUCtmPL1h6ObNbMKue53iiQ77KtXgpuw1jDojmdTCAqsYtX8A+ygqxMK86OtVy4RHYt aNMioN51chQiWhfd08S5sLmd3IkmuoKb/sVeBt/YPbluNeGtPmGPXJYtUe+jsWZetGI3 iyb9IvzoaL0IV+BF6xoQptA7pwAKtChXvrslZtDAKa1U6TdjPIUWEuL5aoQrje+e7ppe Igcw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1758323400; x=1758928200; 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=OETsLPmcZGNpLSshaaiLeOj8LUgrE6er4wP4vFEBZK0=; b=Y8GydtiiLGL25bedxty29Oa8XIlAHJjU8R7YJSvHi8bF1+tnEWxeUoTx7QRRK8UPpy 8PtmECk2Q4Xo2kHP/6yzp2rOtNP0ZKzIy8EIjFtvHhfYs7cFMVyNIRVgQVgt6U1p+yC/ KjQvggMgtBFsvUBU4HucM0K8dph612OzKIlLzt9VlVRPSh3iB0VhnvGwE2/14m1bWzIq ejaUXM1GIZV5STG4kQak266oVglcCYyXD5tkzD+sTrqY34AAOCmYyttccPvdhoLBX89J pTGShg8+YPfssP+ZF7zRnRT7jl3iyWheQED0Y1G61KkMFEQNEtzBAL6Q1uspz9WxTMoX 1U2Q== X-Gm-Message-State: AOJu0YzLFo8vClbBAirTYosp759Ag2RBAFdoMPBdKw1su04OpdHMf1RC l8lb6LpbV+vqLjMkiN2p8IZQ90q20nDxPNzRb/F926P2AZQ+wUj6D6R9 X-Gm-Gg: ASbGnctWpIMbYxaQ9Kc5VBiEbjQado259LBj3m8lrYe3vIcUqlFtn0agnp8rXZmjA5l 8mmQl9ry/ZUkqHHRsZaNfYRH94iL0F7l0tz4uPgcigqUervxuyK+TJdOSKX3XRihXbf0ov3rgxU wXkZPh14ByNxhh7qOMNWjh24NOSEe6vZKPDQI2ay68TUYASQxAUahi9pJssTJ1KszSWn0gz9mZr IbV8T377Fdz/usJCHN/XttA9vzNBWUS78JeaPRBSm4gxNI++qzOOMNk0eaAzQqFMRE43beUEpAi nMaGLOJS+4PuPEwt+wF0YgDV2EECGAbzzbQpVOX5Sup2h2/KedMmhEb4Lzu+2iydAuIuQ4WqUBt FNLGctqAS1E1u X-Google-Smtp-Source: AGHT+IFcysuzP3rduPjropxaTA4EuKYQcJiZtfFTX6ouuJ2uh66xahLuCndpNW2YEKS+WRm1cLEPNw== X-Received: by 2002:a17:902:f64f:b0:269:96a1:d96e with SMTP id d9443c01a7336-269ba46699fmr63403595ad.20.1758323400267; Fri, 19 Sep 2025 16:10:00 -0700 (PDT) Received: from localhost ([2a03:2880:ff:1::]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-26980339e89sm63843625ad.130.2025.09.19.16.09.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Sep 2025 16:09:59 -0700 (PDT) From: Amery Hung To: bpf@vger.kernel.org Cc: netdev@vger.kernel.org, alexei.starovoitov@gmail.com, andrii@kernel.org, daniel@iogearbox.net, paul.chaignon@gmail.com, kuba@kernel.org, stfomichev@gmail.com, martin.lau@kernel.org, mohsin.bashr@gmail.com, noren@nvidia.com, dtatulea@nvidia.com, saeedm@nvidia.com, tariqt@nvidia.com, mbloch@nvidia.com, maciej.fijalkowski@intel.com, kernel-team@meta.com Subject: [PATCH bpf-next v6 7/7] selftests: drv-net: Pull data before parsing headers Date: Fri, 19 Sep 2025 16:09:52 -0700 Message-ID: <20250919230952.3628709-8-ameryhung@gmail.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20250919230952.3628709-1-ameryhung@gmail.com> References: <20250919230952.3628709-1-ameryhung@gmail.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit It is possible for drivers to generate xdp packets with data residing entirely in fragments. To keep parsing headers using direcy packet access, call bpf_xdp_pull_data() to pull headers into the linear data area. Signed-off-by: Amery Hung --- .../selftests/net/lib/xdp_native.bpf.c | 89 +++++++++++++++---- 1 file changed, 74 insertions(+), 15 deletions(-) diff --git a/tools/testing/selftests/net/lib/xdp_native.bpf.c b/tools/testing/selftests/net/lib/xdp_native.bpf.c index 521ba38f2ddd..df4eea5c192b 100644 --- a/tools/testing/selftests/net/lib/xdp_native.bpf.c +++ b/tools/testing/selftests/net/lib/xdp_native.bpf.c @@ -14,6 +14,8 @@ #define MAX_PAYLOAD_LEN 5000 #define MAX_HDR_LEN 64 +extern int bpf_xdp_pull_data(struct xdp_md *xdp, __u32 len) __ksym __weak; + enum { XDP_MODE = 0, XDP_PORT = 1, @@ -68,30 +70,57 @@ static void record_stats(struct xdp_md *ctx, __u32 stat_type) static struct udphdr *filter_udphdr(struct xdp_md *ctx, __u16 port) { - void *data_end = (void *)(long)ctx->data_end; - void *data = (void *)(long)ctx->data; struct udphdr *udph = NULL; - struct ethhdr *eth = data; + void *data, *data_end; + struct ethhdr *eth; + int err; + + err = bpf_xdp_pull_data(ctx, sizeof(*eth)); + if (err) + return NULL; + + data_end = (void *)(long)ctx->data_end; + data = eth = (void *)(long)ctx->data; if (data + sizeof(*eth) > data_end) return NULL; if (eth->h_proto == bpf_htons(ETH_P_IP)) { - struct iphdr *iph = data + sizeof(*eth); + struct iphdr *iph; + + err = bpf_xdp_pull_data(ctx, sizeof(*eth) + sizeof(*iph) + + sizeof(*udph)); + if (err) + return NULL; + + data_end = (void *)(long)ctx->data_end; + data = (void *)(long)ctx->data; + + iph = data + sizeof(*eth); if (iph + 1 > (struct iphdr *)data_end || iph->protocol != IPPROTO_UDP) return NULL; - udph = (void *)eth + sizeof(*iph) + sizeof(*eth); - } else if (eth->h_proto == bpf_htons(ETH_P_IPV6)) { - struct ipv6hdr *ipv6h = data + sizeof(*eth); + udph = data + sizeof(*iph) + sizeof(*eth); + } else if (eth->h_proto == bpf_htons(ETH_P_IPV6)) { + struct ipv6hdr *ipv6h; + + err = bpf_xdp_pull_data(ctx, sizeof(*eth) + sizeof(*ipv6h) + + sizeof(*udph)); + if (err) + return NULL; + + data_end = (void *)(long)ctx->data_end; + data = (void *)(long)ctx->data; + + ipv6h = data + sizeof(*eth); if (ipv6h + 1 > (struct ipv6hdr *)data_end || ipv6h->nexthdr != IPPROTO_UDP) return NULL; - udph = (void *)eth + sizeof(*ipv6h) + sizeof(*eth); + udph = data + sizeof(*ipv6h) + sizeof(*eth); } else { return NULL; } @@ -145,17 +174,34 @@ static void swap_machdr(void *data) static int xdp_mode_tx_handler(struct xdp_md *ctx, __u16 port) { - void *data_end = (void *)(long)ctx->data_end; - void *data = (void *)(long)ctx->data; struct udphdr *udph = NULL; - struct ethhdr *eth = data; + void *data, *data_end; + struct ethhdr *eth; + int err; + + err = bpf_xdp_pull_data(ctx, sizeof(*eth)); + if (err) + return XDP_PASS; + + data_end = (void *)(long)ctx->data_end; + data = eth = (void *)(long)ctx->data; if (data + sizeof(*eth) > data_end) return XDP_PASS; if (eth->h_proto == bpf_htons(ETH_P_IP)) { - struct iphdr *iph = data + sizeof(*eth); - __be32 tmp_ip = iph->saddr; + struct iphdr *iph; + __be32 tmp_ip; + + err = bpf_xdp_pull_data(ctx, sizeof(*eth) + sizeof(*iph) + + sizeof(*udph)); + if (err) + return XDP_PASS; + + data_end = (void *)(long)ctx->data_end; + data = (void *)(long)ctx->data; + + iph = data + sizeof(*eth); if (iph + 1 > (struct iphdr *)data_end || iph->protocol != IPPROTO_UDP) @@ -169,8 +215,10 @@ static int xdp_mode_tx_handler(struct xdp_md *ctx, __u16 port) return XDP_PASS; record_stats(ctx, STATS_RX); + eth = data; swap_machdr((void *)eth); + tmp_ip = iph->saddr; iph->saddr = iph->daddr; iph->daddr = tmp_ip; @@ -178,9 +226,19 @@ static int xdp_mode_tx_handler(struct xdp_md *ctx, __u16 port) return XDP_TX; - } else if (eth->h_proto == bpf_htons(ETH_P_IPV6)) { - struct ipv6hdr *ipv6h = data + sizeof(*eth); + } else if (eth->h_proto == bpf_htons(ETH_P_IPV6)) { struct in6_addr tmp_ipv6; + struct ipv6hdr *ipv6h; + + err = bpf_xdp_pull_data(ctx, sizeof(*eth) + sizeof(*ipv6h) + + sizeof(*udph)); + if (err) + return XDP_PASS; + + data_end = (void *)(long)ctx->data_end; + data = (void *)(long)ctx->data; + + ipv6h = data + sizeof(*eth); if (ipv6h + 1 > (struct ipv6hdr *)data_end || ipv6h->nexthdr != IPPROTO_UDP) @@ -194,6 +252,7 @@ static int xdp_mode_tx_handler(struct xdp_md *ctx, __u16 port) return XDP_PASS; record_stats(ctx, STATS_RX); + eth = data; swap_machdr((void *)eth); __builtin_memcpy(&tmp_ipv6, &ipv6h->saddr, sizeof(tmp_ipv6)); -- 2.47.3