From patchwork Fri Apr 24 20:14:27 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Ahern X-Patchwork-Id: 220587 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-10.1 required=3.0 tests=DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9368EC54FCB for ; Fri, 24 Apr 2020 20:14:54 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 71A0E215A4 for ; Fri, 24 Apr 2020 20:14:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1587759294; bh=Vw2FCOouxN7bROtFES0en98jMRaoYmLfRNQ/ko0YO0M=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-ID:From; b=mbjPFBU+M1ygGVtxw/hNY474kejkmbnozapb4JIaVYsIsCnLCWnt2oiil52UrG8Q7 H/WgPvFlE+KkyHSnbyJzQg8RSzALGKcut3ElPB7L6ex5EnuVXW/2rAyVBcJexebsmK S5qO4eeRtLFct5I2XxEMqbw9hn3TGFhdXZPJauoc= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729564AbgDXUOx (ORCPT ); Fri, 24 Apr 2020 16:14:53 -0400 Received: from mail.kernel.org ([198.145.29.99]:57614 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729537AbgDXUOq (ORCPT ); Fri, 24 Apr 2020 16:14:46 -0400 Received: from C02YQ0RWLVCF.internal.digitalocean.com (c-73-181-34-237.hsd1.co.comcast.net [73.181.34.237]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id CCF3721BE5; Fri, 24 Apr 2020 20:14:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1587759285; bh=Vw2FCOouxN7bROtFES0en98jMRaoYmLfRNQ/ko0YO0M=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=SaPxq2UXoBMIusaFk1ItIpCTmTKXZrbM4NeVoOCMhc9b+VWkizUZt2bhkLH9rX7Nx TSmeH5Lmh+bXTYCn1h4L/mCrkXVRF/vbk0rQ8oL+0KjHN2uTNWv+Xy+/ZBSez86jPI 50sRN5IW7v2aWC3P46CmnAfLjlEcIxSC34CmrEro= From: David Ahern To: netdev@vger.kernel.org Cc: davem@davemloft.net, kuba@kernel.org, prashantbhole.linux@gmail.com, jasowang@redhat.com, brouer@redhat.com, toke@redhat.com, toshiaki.makita1@gmail.com, daniel@iogearbox.net, john.fastabend@gmail.com, ast@kernel.org, kafai@fb.com, songliubraving@fb.com, yhs@fb.com, andriin@fb.com, dsahern@gmail.com, David Ahern Subject: [PATCH v3 bpf-next 14/15] selftest: Add xdp_egress attach tests Date: Fri, 24 Apr 2020 14:14:27 -0600 Message-Id: <20200424201428.89514-15-dsahern@kernel.org> X-Mailer: git-send-email 2.21.1 (Apple Git-122.3) In-Reply-To: <20200424201428.89514-1-dsahern@kernel.org> References: <20200424201428.89514-1-dsahern@kernel.org> MIME-Version: 1.0 Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org From: David Ahern Add xdp_egress attach tests: 1. verify egress programs cannot access ingress entries in xdp context 2. verify ability to load, attach, and detach xdp egress to a device. Signed-off-by: David Ahern --- .../bpf/prog_tests/xdp_egress_attach.c | 56 +++++++++++++++++++ .../selftests/bpf/progs/test_xdp_egress.c | 12 ++++ .../bpf/progs/test_xdp_egress_fail.c | 16 ++++++ 3 files changed, 84 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/xdp_egress_attach.c create mode 100644 tools/testing/selftests/bpf/progs/test_xdp_egress.c create mode 100644 tools/testing/selftests/bpf/progs/test_xdp_egress_fail.c diff --git a/tools/testing/selftests/bpf/prog_tests/xdp_egress_attach.c b/tools/testing/selftests/bpf/prog_tests/xdp_egress_attach.c new file mode 100644 index 000000000000..5253754b27de --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/xdp_egress_attach.c @@ -0,0 +1,56 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include + +#define IFINDEX_LO 1 + +void test_xdp_egress_attach(void) +{ + struct bpf_prog_load_attr attr = { + .prog_type = BPF_PROG_TYPE_XDP, + .expected_attach_type = BPF_XDP_EGRESS, + }; + struct bpf_prog_info info = {}; + __u32 id, len = sizeof(info); + struct bpf_object *obj; + __u32 duration = 0; + int err, fd = -1; + + /* should fail - accesses rx queue info */ + attr.file = "./test_xdp_egress_fail.o", + err = bpf_prog_load_xattr(&attr, &obj, &fd); + if (CHECK(err == 0 && fd >= 0, "xdp_egress with rx failed to load", + "load of xdp_egress with rx succeeded instead of failed")) + return; + + attr.file = "./test_xdp_egress.o", + err = bpf_prog_load_xattr(&attr, &obj, &fd); + if (CHECK_FAIL(err)) + return; + + err = bpf_obj_get_info_by_fd(fd, &info, &len); + if (CHECK_FAIL(err)) + goto out_close; + + err = bpf_set_link_xdp_fd(IFINDEX_LO, fd, XDP_FLAGS_EGRESS_MODE); + if (CHECK(err, "xdp attach", "xdp attach failed")) + goto out_close; + + err = bpf_get_link_xdp_id(IFINDEX_LO, &id, XDP_FLAGS_EGRESS_MODE); + if (CHECK(err || id != info.id, "id_check", + "loaded prog id %u != id %u, err %d", info.id, id, err)) + goto out; + +out: + err = bpf_set_link_xdp_fd(IFINDEX_LO, -1, XDP_FLAGS_EGRESS_MODE); + if (CHECK(err, "xdp detach", "xdp detach failed")) + goto out_close; + + err = bpf_get_link_xdp_id(IFINDEX_LO, &id, XDP_FLAGS_EGRESS_MODE); + if (CHECK(err || id, "id_check", + "failed to detach program %u", id)) + goto out; + +out_close: + bpf_object__close(obj); +} diff --git a/tools/testing/selftests/bpf/progs/test_xdp_egress.c b/tools/testing/selftests/bpf/progs/test_xdp_egress.c new file mode 100644 index 000000000000..0477e8537b7f --- /dev/null +++ b/tools/testing/selftests/bpf/progs/test_xdp_egress.c @@ -0,0 +1,12 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include +#include + +SEC("xdp_egress") +int xdp_egress_good(struct xdp_md *ctx) +{ + __u32 idx = ctx->egress_ifindex; + + return idx == 1 ? XDP_DROP : XDP_PASS; +} diff --git a/tools/testing/selftests/bpf/progs/test_xdp_egress_fail.c b/tools/testing/selftests/bpf/progs/test_xdp_egress_fail.c new file mode 100644 index 000000000000..76b47b1d3bc3 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/test_xdp_egress_fail.c @@ -0,0 +1,16 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include +#include + +SEC("xdp_egress") +int xdp_egress_fail(struct xdp_md *ctx) +{ + __u32 rxq = ctx->rx_queue_index; + __u32 idx = ctx->ingress_ifindex; + + if (idx == 1) + return XDP_DROP; + + return rxq ? XDP_DROP : XDP_PASS; +}