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 64CB725A642 for ; Mon, 22 Sep 2025 23:34:06 +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=1758584047; cv=none; b=Wr5osUZH+sMkrNx/n3oBC7cNzv3IAX6AQq/PgIa3jVfCeyZyBzczd4FU8CDHv9OiXrY1zq9F4zaDENLHXHcTpvPPXmKNV89Uv/Tno4AX2BQxUieDhgOV7NKrIGcTm11SGxYJMA11Z8NzqJxZWZIeqo+YFW9W9eImePjcDl9W68U= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758584047; c=relaxed/simple; bh=nDmjbXMAaaARovoem/jblg2PIL/t10TLTSxc9dzhj3Y=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=R3P+OfTrnz8zejbVATz3wLDmBsM08w9LaoEM+K20KsD0T3NjapSwj4s4LpXiHqaZ3/oo0hbCM99Q56hxHBUzXE5f4+lHTNbozvFHxvToUpBpw4MuAYUPEYcGV+FlKn8GHhcMQhte83zCcDqwB38gJNDMHfLHZdXgiQs8Qlfcl9s= 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=f8k/Ld7D; 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="f8k/Ld7D" Received: by mail-pl1-f169.google.com with SMTP id d9443c01a7336-2570bf605b1so64168725ad.2 for ; Mon, 22 Sep 2025 16:34:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1758584045; x=1759188845; 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=f8k/Ld7De/BCCr4UMqMQvheIWP3VgLCRxZAIe2gEOyA80yt1qMqdyp/BzwsfaJ/nka JMgRU9Ett4guyTrxEAlx/zmSntZOt2mI24LwQQHyUV4cTZaZluqksPk6SPBdVBSGcpMX B1Gh10GXmwx0aU27qkn4KNd7cJ9WIpS7MyQVOnFi+EF68TKnaD/SWNGmA9KM9eKOLkmU nvMwnbfAtuvL/ujifkwxlaQEkuRExSEDPXzd2PwxmltOFfxxg76WnT03F/RFs3kdvWT2 FVy6HgQb5St1sfMreRrAVUiWPzV8RSE/vxjPzRyMuUfmdW1CXWz4Qua/fE13kJJLxgjO 4p8g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1758584045; x=1759188845; 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=ev7IGQShHZbaGEMgmiSweYwA+IrKuWuapqdWXcp4IOEd0E9vg81g/4+MiXkYokKmu4 5EEw6maaVD545C3Co3N91Ri+tcCN0YrbrkuXMMFcSbycRggwTmkz9Ba8DEAoN+g6Gbof 1btJQU0F6VZtc6nykOSMD3NB/MZIxRrCWkYc0zNTQ5ypUAN0Dt3NfzexzaRX+bRPRu55 EI4dE1/fVCFj/h5ssYjdsR/90lPdwESxRvfTrlUgV90U1RaQ05FuUFL4JjqFYL6AWWlx TRxXTesj5NUHrZZHkPO7CGxtUerVXwdAQPJMJWWdZAKuuAwUpD6Q9pyqqAPoQDEvZ7ht 51uw== X-Gm-Message-State: AOJu0YyoddUTBmuLDurWTrFde2rLnc64K/RCzhrR3UUNihoFK6/pr/5N lQckuL854BvYEKmFsmnhNtKnhXxXX3TnsZ+B8kBqrkCKdts+VXKaCZGeHTN3gQ== X-Gm-Gg: ASbGncuBtNMyUU+WBPP6yLAX68wTOd9aEJ5HQ4z+xHo18z+K33stN8MnwPgseUjg08y BTKX4eLLl9YjsiX1Tw9JlOFpcv3hRwSb2z9cMHgRjFKcjO9ySNAoVxhnFlQrY7ks2w0resauw9p R8kz9OEnPZsvuKQnr9lra+wf44RlQdOn6RIH/3HBWeP928LrpX/V6d4S3qg/eTTy4cZY+AQRN8u ym5QeF3r0Gew0BauagGo4mxw7E3piN9wCx91GuybaJmC3YrZypO4RBN2UWNl/jKfeih/lo1/Ys8 m8KEV69ZDiiq0gCEaMZ3NWIFy+lnwkaRrCYCsukuQBaSvmLiWFsSXojQNuLdvBuaWvBx+fvbotl EnecYjpAzHS1psqBxXJvCfdA= X-Google-Smtp-Source: AGHT+IHEp9s7BtYi5HjuFwhfu5erFkhZhA5XnxXXJBqXXpGibVuYXFgudGaUdlTZUVaWuYgD27m3ag== X-Received: by 2002:a17:903:1212:b0:262:2ae8:2517 with SMTP id d9443c01a7336-27cc0fa8ebbmr7003675ad.5.1758584045546; Mon, 22 Sep 2025 16:34:05 -0700 (PDT) Received: from localhost ([2a03:2880:ff:1::]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-77ef3aab68esm9852972b3a.85.2025.09.22.16.34.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Sep 2025 16:34:05 -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 v7 8/8] selftests: drv-net: Pull data before parsing headers Date: Mon, 22 Sep 2025 16:33:56 -0700 Message-ID: <20250922233356.3356453-9-ameryhung@gmail.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20250922233356.3356453-1-ameryhung@gmail.com> References: <20250922233356.3356453-1-ameryhung@gmail.com> Precedence: bulk X-Mailing-List: bpf@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