From patchwork Thu Dec 2 02:47:21 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tonghao Zhang X-Patchwork-Id: 12651483 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 14C06C433EF for ; Thu, 2 Dec 2021 02:47:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1354675AbhLBCvC (ORCPT ); Wed, 1 Dec 2021 21:51:02 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47136 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229837AbhLBCu7 (ORCPT ); Wed, 1 Dec 2021 21:50:59 -0500 Received: from mail-pg1-x531.google.com (mail-pg1-x531.google.com [IPv6:2607:f8b0:4864:20::531]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8991DC061574 for ; Wed, 1 Dec 2021 18:47:37 -0800 (PST) Received: by mail-pg1-x531.google.com with SMTP id 133so7446587pgc.12 for ; Wed, 01 Dec 2021 18:47:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=8mApNYIlmyYDE92hmrIu9j0ZnkBUvjBpogxont+LFMc=; b=Qdr458L9RsO1m+v3aT9KMK6AVcb/NxGHnpXIEZPtjZnweSxrpCtCbL4oHWKaX1/5PV 8MV24Tzq8ASgdYakCup1KgD0mPtOa0V8e8P49euciLTwymyzqkNhgKqN/ohRo5Y87Qtd 5r4BsFv/xsM98Vb3b3mSxLqdOGOWl5SiSitmcw5JIe0xA4MNyOmm6oEm1j8loI9WLRTW u3OaDBlvv0aCaoliuKcnFHOP1ai+NBNo17HlNPJGIXHlUbBBF9veh8++FsLQpuTQoFkv gzI9JLrr03o0Hl/yWU8Qz+Dg8voaA9an1qcNe2ODKYxJrS1+WtrM6p0FmcyunOYEHKuV DY6g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=8mApNYIlmyYDE92hmrIu9j0ZnkBUvjBpogxont+LFMc=; b=YfyJ6YD1iTntadvaUISVpLXNyw+Mfj/9vhiHQTtE6Xe24pL6YDTZnkkztbR1q87F0o 20mAyHLvdrDDOGwz5CZLPajmAF+XXSbaeGxLfsc5D846WMgpCYtNZvkHNOJbjmK5wt3j /2EhCAAvD7BwfOdtg/LPbMMFFBDkABPZAmnE3gqZ9xe1Cf7u/30Xo7qGNTKEj/HG97dS 481T9pRnKSoi0g8yyalxmfESTTWb+Ut5YrSLDs51Bc5yVhf3u/r3mAcrGZix42/mec6c AIJYcBXmbEdNgBtFFfepprOa1LBkp5Ql3Nh0YhV7IDHtCIY9yiyeCzY+2ERzoRQNiDWI gH+w== X-Gm-Message-State: AOAM531Arie2Up8wSh//abvBU8i9sXDTXL5kl92wz1pvHEpA7mOZPI/g BJCyNM8I7RkX2bmruzEPus+ma1fSjQmY7A== X-Google-Smtp-Source: ABdhPJzKDZ4SWggJ5/ICL4WugXxWxKwop4vPiWzyfk/B2ISYJ6wyH8Mgs+Ko9k4c/60GfKDP5LFVoQ== X-Received: by 2002:a05:6a00:14cb:b0:49f:c028:aea6 with SMTP id w11-20020a056a0014cb00b0049fc028aea6mr9985890pfu.48.1638413256685; Wed, 01 Dec 2021 18:47:36 -0800 (PST) Received: from localhost.localdomain ([111.204.182.106]) by smtp.gmail.com with ESMTPSA id z10sm1183180pfh.188.2021.12.01.18.47.31 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 01 Dec 2021 18:47:36 -0800 (PST) From: xiangxia.m.yue@gmail.com To: netdev@vger.kernel.org Cc: davem@davemloft.net, kuba@kernel.org, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, kafai@fb.com, songliubraving@fb.com, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org, edumazet@google.com, atenart@kernel.org, alexandr.lobakin@intel.com, weiwan@google.com, arnd@arndb.de, Tonghao Zhang Subject: [net v4 1/3] net: core: set skb useful vars in __bpf_tx_skb Date: Thu, 2 Dec 2021 10:47:21 +0800 Message-Id: <20211202024723.76257-2-xiangxia.m.yue@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20211202024723.76257-1-xiangxia.m.yue@gmail.com> References: <20211202024723.76257-1-xiangxia.m.yue@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@kernel.org From: Tonghao Zhang We may use bpf_redirect to redirect the packets to other netdevice (e.g. ifb) in ingress or egress path. The target netdevice may check the *skb_iif, *redirected and *from_ingress. For example, if skb_iif or redirected is 0, ifb will drop the packets. bpf_redirect may be invoked in ingress or egress path, so we set the *skb_iif unconditionally. Fixes: a70b506efe89 ("bpf: enforce recursion limit on redirects") Cc: "David S. Miller" Cc: Jakub Kicinski Cc: Alexei Starovoitov Cc: Daniel Borkmann Cc: Andrii Nakryiko Cc: Martin KaFai Lau Cc: Song Liu Cc: Yonghong Song Cc: John Fastabend Cc: KP Singh Cc: Eric Dumazet Cc: Antoine Tenart Cc: Alexander Lobakin Cc: Wei Wang Cc: Arnd Bergmann Signed-off-by: Tonghao Zhang --- net/core/filter.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/net/core/filter.c b/net/core/filter.c index 8271624a19aa..bcfdce9e99f4 100644 --- a/net/core/filter.c +++ b/net/core/filter.c @@ -2107,9 +2107,19 @@ static inline int __bpf_tx_skb(struct net_device *dev, struct sk_buff *skb) return -ENETDOWN; } - skb->dev = dev; + /* The target netdevice (e.g. ifb) may use the: + * - skb_iif, bpf_redirect invoked in ingress or egress path. + * - redirected + * - from_ingress + */ + skb->skb_iif = skb->dev->ifindex; +#ifdef CONFIG_NET_CLS_ACT + skb_set_redirected(skb, skb->tc_at_ingress); +#else skb->tstamp = 0; +#endif + skb->dev = dev; dev_xmit_recursion_inc(); ret = dev_queue_xmit(skb); dev_xmit_recursion_dec(); From patchwork Thu Dec 2 02:47:22 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tonghao Zhang X-Patchwork-Id: 12651485 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id ED113C433EF for ; Thu, 2 Dec 2021 02:47:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1354955AbhLBCvF (ORCPT ); Wed, 1 Dec 2021 21:51:05 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47152 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229837AbhLBCvD (ORCPT ); Wed, 1 Dec 2021 21:51:03 -0500 Received: from mail-pj1-x1035.google.com (mail-pj1-x1035.google.com [IPv6:2607:f8b0:4864:20::1035]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CA512C061574 for ; Wed, 1 Dec 2021 18:47:41 -0800 (PST) Received: by mail-pj1-x1035.google.com with SMTP id w33-20020a17090a6ba400b001a722a06212so2198022pjj.0 for ; Wed, 01 Dec 2021 18:47:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=HqsZ85E9DvKaTPtdQ0AfLXo0OZ5iGao1T0RRw0COHtM=; b=P2LuyLOD8UZXa7vqSOrt/fe61UbcI/VJHGKyNZhFqVmp0/ON3lKBFI8UHwhctWeqNa SFV66nb6tNwjcMZNmrUzjG2Qd3NWbjAAkd7zolMGzFOvb4L1d90GuhJoyZX9ge79187V rgqoTwaIsptnLrB1UXeeaXUKVJTQUqdfZB2h29W5i0316wV+lMMC6YusByNYCIklM3a7 8f/YD9PKjiZMBVNSh79jWQ8qFcyscYtBnaYP/3k5bfjdFjNEms0t04Rjf8owTiQ+Flci flNn1l3ClKsEHF7xKnaB/u8CwfxRXIam4SLGraEvpyGsCxCtgU+s9T75VtHcYDeGf4ms Mqug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=HqsZ85E9DvKaTPtdQ0AfLXo0OZ5iGao1T0RRw0COHtM=; b=qL6RuoNynBZsBCXHHMV2bkzOLzNg0nEjOeyFhnyw/mj4LuMAps1+14aYKE1n1PkzpL vbsxLfs6uhq4CasrrUokYQpw2ZD1txAZ8ommZSQiVFtwEHVdZMFj+t9kL7R1/0hZF+RB 7NHx0b3kBW2Jv/Dl8y34c7+3LUiIuwHPBDgW0VdtVzUuXn3fkVSHGCnRVXVrNghLYiw/ 3kvcZfrxWJ0OxoCwPJ8e92+IsD9ws4M5B2TuraJLAR9ndnbar4Vtt8cZQ+iDDKgHOu0J waRrS3xtZO2/j4pDCjFnEzZGH6lBTcB+in1IGTMpqpae+hldOSxOo3b3wqKi15GkKopZ 7bRA== X-Gm-Message-State: AOAM533HG/jubYm5v2iAsVTMwmCPjRLt2heUTFZthmj+lXwfWs9HHRhl iB1Fj8KuhD2AMm6tfLoOgNXBrjVX4weMvA== X-Google-Smtp-Source: ABdhPJyRjEB4ewyWmtNd8143n3AArFCRniqZtm/F16Huk5+Sq3q6Y7hQLhiCz/+Omrod8zXaPXolJA== X-Received: by 2002:a17:90b:4c0f:: with SMTP id na15mr2706283pjb.222.1638413261105; Wed, 01 Dec 2021 18:47:41 -0800 (PST) Received: from localhost.localdomain ([111.204.182.106]) by smtp.gmail.com with ESMTPSA id z10sm1183180pfh.188.2021.12.01.18.47.37 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 01 Dec 2021 18:47:40 -0800 (PST) From: xiangxia.m.yue@gmail.com To: netdev@vger.kernel.org Cc: davem@davemloft.net, kuba@kernel.org, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, kafai@fb.com, songliubraving@fb.com, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org, edumazet@google.com, atenart@kernel.org, alexandr.lobakin@intel.com, weiwan@google.com, arnd@arndb.de, Tonghao Zhang Subject: [net v4 2/3] net: sched: add check tc_skip_classify in sch egress Date: Thu, 2 Dec 2021 10:47:22 +0800 Message-Id: <20211202024723.76257-3-xiangxia.m.yue@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20211202024723.76257-1-xiangxia.m.yue@gmail.com> References: <20211202024723.76257-1-xiangxia.m.yue@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@kernel.org From: Tonghao Zhang Try to resolve the issues as below: * We look up and then check tc_skip_classify flag in net sched layer, even though skb don't want to be classified. That case may consume a lot of cpu cycles. Install the rules as below: $ for id in $(seq 1 10000); do $ tc filter add ... egress prio $id ... action mirred egress redirect dev ifb0 $ done netperf: $ taskset -c 1 netperf -t TCP_RR -H ip -- -r 32,32 $ taskset -c 1 netperf -t TCP_STREAM -H ip -- -m 32 Before: 152.04 tps, 0.58 Mbit/s After: 303.07 tps, 1.51 Mbit/s For TCP_RR, there are 99.3% improvement, TCP_STREAM 160.3%. * bpf_redirect may be invoked in egress path. If we don't check the flags and then return immediately, the packets will loopback. $ tc filter add dev eth0 egress bpf direct-action obj \ ifb.o sec ifb Cc: "David S. Miller" Cc: Jakub Kicinski Cc: Alexei Starovoitov Cc: Daniel Borkmann Cc: Andrii Nakryiko Cc: Martin KaFai Lau Cc: Song Liu Cc: Yonghong Song Cc: John Fastabend Cc: KP Singh Cc: Eric Dumazet Cc: Antoine Tenart Cc: Alexander Lobakin Cc: Wei Wang Cc: Arnd Bergmann Signed-off-by: Tonghao Zhang --- net/core/dev.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/net/core/dev.c b/net/core/dev.c index d30adecc2bb2..10bad44e2ec4 100644 --- a/net/core/dev.c +++ b/net/core/dev.c @@ -3823,6 +3823,9 @@ sch_handle_egress(struct sk_buff *skb, int *ret, struct net_device *dev) if (!miniq) return skb; + if (skb_skip_tc_classify(skb)) + return skb; + /* qdisc_skb_cb(skb)->pkt_len was already set by the caller. */ qdisc_skb_cb(skb)->mru = 0; qdisc_skb_cb(skb)->post_ct = false; From patchwork Thu Dec 2 02:47:23 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tonghao Zhang X-Patchwork-Id: 12651487 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4DBE3C433F5 for ; Thu, 2 Dec 2021 02:47:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1354994AbhLBCvJ (ORCPT ); Wed, 1 Dec 2021 21:51:09 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47172 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1354997AbhLBCvI (ORCPT ); Wed, 1 Dec 2021 21:51:08 -0500 Received: from mail-pj1-x102e.google.com (mail-pj1-x102e.google.com [IPv6:2607:f8b0:4864:20::102e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CD9C0C061748 for ; Wed, 1 Dec 2021 18:47:46 -0800 (PST) Received: by mail-pj1-x102e.google.com with SMTP id gt5so19488010pjb.1 for ; Wed, 01 Dec 2021 18:47:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=TrstKECFk8gOGCbVkweAHPFWowtjG3x57DhZCA+QU+4=; b=peXH3V3D3f3qSX7MJ5nPTvdDYjBDjzT2ZVzkR0MktMAj/0bVqMpKGPEifiTVWobtAh R9A5HrOx+lZHSWyM3JNasjE2cmcRshkXC3On3JNgPNXiB9hgMpsSXUmcZxv1g5gevfFd rBFjAb9sVKD+HwZIO3xQ+7YoChtxZ9tFI2YKf8bGZnwSlQbUA/RV/tlSTTCjkGo1Z4e1 wm4Xy3ow/QIXL4QTWRb1rz9reEpFZiKb94Xo5e8YO/gtC1ETdCW0tnHtKGr7og7NxWak emHVyfsU8Pk5UCXN6VnhZQ/s6YvDU6CI2/vTejAPKvL8rT+krFyfh3kgNVE0KbOHWcSx 90Hw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=TrstKECFk8gOGCbVkweAHPFWowtjG3x57DhZCA+QU+4=; b=thHChLXgsjEkgXgLC8cVnOocqlfNMpTUnYdVd/7Dus8qx30ilnDO4KZvDhq4ulyi3h WxkHjGYedos7D5KKr8M4FHhOq9cWraelmpbHJEExWmp9Wyr+VO2qrjpG7BCUzw1CB6u1 WaC160Gjca2oJaWY5KYFhQgPmYwyOxM2NdHtaMgjS6iRq1/22lWwqphRRbEQ8NrtbnU4 +hnaqRDKXkvLjMJgJUCe34o7jYMUY5oTy1y7ImGHhMDlGNY2lH13s1k7zFnXDG8kFq60 vnB8nMe1wsmmFJusI+lsNgXtIzdvrFJ89bMtYiS0+Lx6nCySUnPtftLhPWr89Wm12qpy sUzg== X-Gm-Message-State: AOAM531JZDHzoNhI0e7UYRD+XO2hUe92LLVPcVXFa8nHjqL5jd6TWsxw 9sCHtp4LSOhbGT2Ftp/ACgGkb6IECHsCyg== X-Google-Smtp-Source: ABdhPJzGQcSK4Jx3di9Fhv7welDN2/1EVcc462V0EjrPdLJDJBObxSWilDPoR+IVp8WYfwpccXoVmA== X-Received: by 2002:a17:902:c412:b0:141:f716:e192 with SMTP id k18-20020a170902c41200b00141f716e192mr12075475plk.88.1638413265495; Wed, 01 Dec 2021 18:47:45 -0800 (PST) Received: from localhost.localdomain ([111.204.182.106]) by smtp.gmail.com with ESMTPSA id z10sm1183180pfh.188.2021.12.01.18.47.41 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 01 Dec 2021 18:47:45 -0800 (PST) From: xiangxia.m.yue@gmail.com To: netdev@vger.kernel.org Cc: davem@davemloft.net, kuba@kernel.org, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, kafai@fb.com, songliubraving@fb.com, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org, edumazet@google.com, atenart@kernel.org, alexandr.lobakin@intel.com, weiwan@google.com, arnd@arndb.de, Tonghao Zhang Subject: [net v4 3/3] selftests: bpf: add bpf_redirect to ifb Date: Thu, 2 Dec 2021 10:47:23 +0800 Message-Id: <20211202024723.76257-4-xiangxia.m.yue@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20211202024723.76257-1-xiangxia.m.yue@gmail.com> References: <20211202024723.76257-1-xiangxia.m.yue@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@kernel.org From: Tonghao Zhang ifb netdev is used for queueing incoming traffic for shaping. we may run bpf progs in tc cls hook(ingress or egress), to redirect the packets to ifb. This patch adds this test, for bpf. Cc: "David S. Miller" Cc: Jakub Kicinski Cc: Alexei Starovoitov Cc: Daniel Borkmann Cc: Andrii Nakryiko Cc: Martin KaFai Lau Cc: Song Liu Cc: Yonghong Song Cc: John Fastabend Cc: KP Singh Cc: Eric Dumazet Cc: Antoine Tenart Cc: Alexander Lobakin Cc: Wei Wang Cc: Arnd Bergmann Signed-off-by: Tonghao Zhang --- tools/testing/selftests/bpf/Makefile | 1 + .../bpf/progs/test_bpf_redirect_ifb.c | 13 ++++ .../selftests/bpf/test_bpf_redirect_ifb.sh | 73 +++++++++++++++++++ 3 files changed, 87 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/test_bpf_redirect_ifb.c create mode 100755 tools/testing/selftests/bpf/test_bpf_redirect_ifb.sh diff --git a/tools/testing/selftests/bpf/Makefile b/tools/testing/selftests/bpf/Makefile index 5d42db2e129a..6ec8b97af0ea 100644 --- a/tools/testing/selftests/bpf/Makefile +++ b/tools/testing/selftests/bpf/Makefile @@ -65,6 +65,7 @@ TEST_PROGS := test_kmod.sh \ test_xdp_vlan_mode_native.sh \ test_lwt_ip_encap.sh \ test_tcp_check_syncookie.sh \ + test_bpf_redirect_ifb.sh \ test_tc_tunnel.sh \ test_tc_edt.sh \ test_xdping.sh \ diff --git a/tools/testing/selftests/bpf/progs/test_bpf_redirect_ifb.c b/tools/testing/selftests/bpf/progs/test_bpf_redirect_ifb.c new file mode 100644 index 000000000000..8b960cd8786b --- /dev/null +++ b/tools/testing/selftests/bpf/progs/test_bpf_redirect_ifb.c @@ -0,0 +1,13 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2021 DiDi Global */ + +#include +#include + +SEC("redirect_ifb") +int redirect(struct __sk_buff *skb) +{ + return bpf_redirect(skb->ifindex + 1 /* ifbX */, 0); +} + +char __license[] SEC("license") = "GPL"; diff --git a/tools/testing/selftests/bpf/test_bpf_redirect_ifb.sh b/tools/testing/selftests/bpf/test_bpf_redirect_ifb.sh new file mode 100755 index 000000000000..0933439696ab --- /dev/null +++ b/tools/testing/selftests/bpf/test_bpf_redirect_ifb.sh @@ -0,0 +1,73 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0 +# + +# Topology: +# --------- +# n1 namespace | n2 namespace +# | +# ----------- | ---------------- +# | veth0 | --------- | veth1, ifb1 | +# ----------- peer ---------------- +# + +readonly prefix="ns-$$-" +readonly ns1="${prefix}1" +readonly ns2="${prefix}2" +readonly ns1_addr=192.168.1.1 +readonly ns2_addr=192.168.1.2 + +setup() { + echo "Load ifb module" + if ! /sbin/modprobe -q -n ifb; then + echo "test_bpf_redirect ifb: module ifb is not found [SKIP]" + exit 4 + fi + + modprobe -q ifb numifbs=0 + + ip netns add "${ns1}" + ip netns add "${ns2}" + + ip link add dev veth0 mtu 1500 netns "${ns1}" type veth \ + peer name veth1 mtu 1500 netns "${ns2}" + # ifb1 created after veth1 + ip link add dev ifb1 mtu 1500 netns "${ns2}" type ifb + + ip -netns "${ns1}" link set veth0 up + ip -netns "${ns2}" link set veth1 up + ip -netns "${ns2}" link set ifb1 up + ip -netns "${ns1}" -4 addr add "${ns1_addr}/24" dev veth0 + ip -netns "${ns2}" -4 addr add "${ns2_addr}/24" dev veth1 + + ip netns exec "${ns2}" tc qdisc add dev veth1 clsact +} + +cleanup() { + ip netns del "${ns2}" &>/dev/null + ip netns del "${ns1}" &>/dev/null + modprobe -r ifb +} + +trap cleanup EXIT + +setup + +ip netns exec "${ns2}" tc filter add dev veth1 \ + ingress bpf direct-action obj test_bpf_redirect_ifb.o sec redirect_ifb +ip netns exec "${ns1}" ping -W 2 -c 2 -i 0.2 -q "${ns2_addr}" &>/dev/null +if [ $? -ne 0 ]; then + echo "bpf redirect to ifb on ingress path [FAILED]" + exit 1 +fi + +ip netns exec "${ns2}" tc filter del dev veth1 ingress +ip netns exec "${ns2}" tc filter add dev veth1 \ + egress bpf direct-action obj test_bpf_redirect_ifb.o sec redirect_ifb +ip netns exec "${ns1}" ping -W 2 -c 2 -i 0.2 -q "${ns2_addr}" &>/dev/null +if [ $? -ne 0 ]; then + echo "bpf redirect to ifb on egress path [FAILED]" + exit 1 +fi + +echo OK