From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-yx1-f48.google.com (mail-yx1-f48.google.com [74.125.224.48]) (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 0F3E53A5456 for ; Wed, 11 Mar 2026 23:59:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.224.48 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773273571; cv=none; b=O4c2aw/8a6PEo+5MAnjXviWurr0l64+cA0aliYmgc4vArLpcULYognowSo6nQgbLycy34jyQ47vt54gGUGDyib4nJJlQUWtkYyonRnOnxUuh01Kx5zR2Goh5Qh3HTlRqZn/RpeAas+xm4qnIRY/SYNCQJEQQkDp4TwAxLbtQWoA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773273571; c=relaxed/simple; bh=zT2RqGd3nP3UCMWCKPndkuHNOyLMnz4Yhzwh6iq/cJM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=JjNZNkRIGTWdVYgUvIU0DX3DZN3tRyF7uPowGVQgwh6wglQCQYx9q3keP1kvtpukYoAWEpqq7CUEg83HVYV/sg7WwMQvJyAQZE1wJkJDTD8UYgph1WVxwLY6X/jU4ahyiMYswFAKY8rQzi8yGxLIXiDYrkyuhUwpErDPbvlSKTw= 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=ECzYzTyN; arc=none smtp.client-ip=74.125.224.48 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="ECzYzTyN" Received: by mail-yx1-f48.google.com with SMTP id 956f58d0204a3-64ae58222aaso1055d50.1 for ; Wed, 11 Mar 2026 16:59:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1773273569; x=1773878369; 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=LYF9EK7KgmEP8ho0Nd5jF1xsN/C9b5ZrS1epvozHDgQ=; b=ECzYzTyNAWUmCBJDtTPcNN9f6jGAUmdWBlfbdixKP0SdIX9g7QGzCCjv4lqrbvoAyh E1o6ULyh7LBj4aufUKsmXxpB1oKkvMLuZdn9Hf0r/53VvKje3OxgOJ6gbEn7JLX58SaB SJ0KVqVlekWkPaJwKblx65AoW/wLfM1fg8ukSR0Pkt564Utt0R54nDvkLJj4y9VdX/kV wz2SCylwktY65wn0+8CYoAsH4RKyz0ni3iFcS3/ks1UBQcPAOduMTAvTfU+mtgU83jFN 3k6ZwoElBUWqYi2IxRAvZHHPg9Wp6JpgnV9CtvZEOpe7rPw+H4yLXmluxLeNgPtF95Zz Q6MQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1773273569; x=1773878369; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=LYF9EK7KgmEP8ho0Nd5jF1xsN/C9b5ZrS1epvozHDgQ=; b=Pv+2QOcz13ZaEls5sIRMgx0MXcsauDDJPWYggyqbCb17lUQoXHEBMZg5iVlI3xun1M eEuMm4mqxSsxa1yNCDDZSUKjDX8uB/VSyHKjAhGL13v+HvAjt+0x8lgMVSiKlNbWOQYK 1H+IzgE94g1dxZ33AmI/bk1xUrelcWjV9u/QpMO807rmQn+wSWcf/lmyhPA3s3lvztAR r+o5v9Ft+m2dQLI35rVXm9XlyUH9mqROHBZrQjidpUlbNSjghe1gWhBTfMTTrDsoLQyE uLFeiy53xqkPUbfGfWoAuYmHCMLKH5hW1o229UQ+JEJ9TJfYYixYibWYcxPQ63RxUD66 9z9Q== X-Gm-Message-State: AOJu0YzwyF+EbFlZGFoYmYB7St60ZHpa7tymqiqPZPbT7yBGyeud5tjo vkxf0o4+PV2fdyhV6KYhKGjzy3tzYmOtlDVDgdjssZANTrDcTD81QSxNRQEHXdbT X-Gm-Gg: ATEYQzzOyZrdNOD9rYOHsAWbCvFYCn0MsaOpBjxhm7zYAOM/4M0fm/yBPa89PZAiRel aGzWYmYZ58h6RwejkTYMSCj48xYpQxJj6T6IctgDu04lvU/HfN3qmsrAv6ru9k8lvput6N5TBMX MLWcUihdgn40PWX7Blfrl9xaIvZZGQoK09tcbnVT4/HDP8UPiGI7rFV2eJ+yuxC5ZTFk+1WDBzy 5gxYPDFMghSyma8/J+2vJ+uxO3LRg18UPV9C8swy494KSRlQmV1FNF9JWVt0hDTYhVpvCOgDs8M X/k8uC4UxBt0DN72VdbaXzQfmyS5302FjUGM8UKHr6kJJnu47fmv3PHuHK83T9aA8Kf7YvXdxIt +ECgGvFIu7X6U3k0G8Mk57wi4zPzmmnXKX4k5WYHTQsmcOgJH85PFZYr5PcRLcrfpPTTW84AblC kBbsxWXIJ1zSw64WJwfWK/vZo= X-Received: by 2002:a05:690c:f03:b0:798:59fd:fd60 with SMTP id 00721157ae682-79917e92df1mr39123557b3.1.1773273568866; Wed, 11 Mar 2026 16:59:28 -0700 (PDT) Received: from localhost ([2a03:2880:21ff:b::]) by smtp.gmail.com with ESMTPSA id 00721157ae682-79917de8c44sm21791927b3.9.2026.03.11.16.59.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Mar 2026 16:59:28 -0700 (PDT) From: Wei Wang To: netdev@vger.kernel.org, Jakub Kicinski , Daniel Zahka , Willem de Bruijn , David Wei , Andrew Lunn , "David S . Miller" , Eric Dumazet Cc: Wei Wang , Bobby Eshleman Subject: [PATCH v4 net-next 4/5] selftests/net: Add bpf skb forwarding program Date: Wed, 11 Mar 2026 16:59:02 -0700 Message-ID: <20260311235904.2392010-5-weibunny.kernel@gmail.com> X-Mailer: git-send-email 2.52.0 In-Reply-To: <20260311235904.2392010-1-weibunny.kernel@gmail.com> References: <20260311235904.2392010-1-weibunny.kernel@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 From: Wei Wang Add nk_redirect.bpf.c, a BPF program that forwards skbs matching some IPv6 prefix received on eth0 ifindex to a specified dev ifindex. bpf_redirect_neigh() is used to make sure neighbor lookup is performed and proper MAC addr is being used. Signed-off-by: Wei Wang Reviewed-by: Bobby Eshleman Tested-by: Bobby Eshleman --- .../drivers/net/hw/nk_redirect.bpf.c | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 tools/testing/selftests/drivers/net/hw/nk_redirect.bpf.c diff --git a/tools/testing/selftests/drivers/net/hw/nk_redirect.bpf.c b/tools/testing/selftests/drivers/net/hw/nk_redirect.bpf.c new file mode 100644 index 000000000000..7ac9ffd50f15 --- /dev/null +++ b/tools/testing/selftests/drivers/net/hw/nk_redirect.bpf.c @@ -0,0 +1,60 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * BPF program for redirecting traffic using bpf_redirect_neigh(). + * Unlike bpf_redirect() which preserves L2 headers, bpf_redirect_neigh() + * performs neighbor lookup and fills in the correct L2 addresses for the + * target interface. This is necessary when redirecting across different + * device types (e.g., from netdevsim to netkit). + */ +#include +#include +#include +#include +#include +#include +#include + +#define TC_ACT_OK 0 +#define ETH_P_IPV6 0x86DD + +#define ctx_ptr(field) ((void *)(long)(field)) + +#define v6_p64_equal(a, b) (a.s6_addr32[0] == b.s6_addr32[0] && \ + a.s6_addr32[1] == b.s6_addr32[1]) + +volatile __u32 redirect_ifindex; +volatile __u8 ipv6_prefix[16]; + +SEC("tc/ingress") +int tc_redirect(struct __sk_buff *skb) +{ + void *data_end = ctx_ptr(skb->data_end); + void *data = ctx_ptr(skb->data); + struct in6_addr *match_prefix; + struct ipv6hdr *ip6h; + struct ethhdr *eth; + + match_prefix = (struct in6_addr *)ipv6_prefix; + + if (skb->protocol != bpf_htons(ETH_P_IPV6)) + return TC_ACT_OK; + + eth = data; + if ((void *)(eth + 1) > data_end) + return TC_ACT_OK; + + ip6h = data + sizeof(struct ethhdr); + if ((void *)(ip6h + 1) > data_end) + return TC_ACT_OK; + + if (!v6_p64_equal(ip6h->daddr, (*match_prefix))) + return TC_ACT_OK; + + /* + * Use bpf_redirect_neigh() to perform neighbor lookup and fill in + * correct L2 addresses for the target interface. + */ + return bpf_redirect_neigh(redirect_ifindex, NULL, 0, 0); +} + +char __license[] SEC("license") = "GPL"; -- 2.52.0