From mboxrd@z Thu Jan 1 00:00:00 1970 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=-7.0 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_PASS,URIBL_BLOCKED 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 4E397C282CE for ; Mon, 8 Apr 2019 17:06:12 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 18B3820880 for ; Mon, 8 Apr 2019 17:06:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729216AbfDHRGJ (ORCPT ); Mon, 8 Apr 2019 13:06:09 -0400 Received: from mail-ed1-f66.google.com ([209.85.208.66]:36319 "EHLO mail-ed1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729169AbfDHRGE (ORCPT ); Mon, 8 Apr 2019 13:06:04 -0400 Received: by mail-ed1-f66.google.com with SMTP id s16so12384776edr.3 for ; Mon, 08 Apr 2019 10:06:02 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:from:to:cc:date:message-id:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=SCrQEgbh7RSgRtblS++H5vqf4cK72LMoC6UNcsq47MU=; b=P0G30zMr6THZJ7lZS1tBuLW5UPfUbBH8oaiNFQrKGfkX+jPj+QS3f9uGp1CGZqB+8K rHW1b6RrbhZDjLCxzSovomwHgH98nqHxEMIsmP6WiABK7gPNB5taXeVfMnw7uZhTDjxw hBHXCFEIZ0SJ3JkHSzxox8RZxpbnfBIomAaBc4rfyEpaDPVo3FbpXCWimw3pZEPiKY/V dOJirpweyAumgJOABcpRO9d3WNl2qiDT+KsDP9qdqW3OUDfCuMNPkYgXQxeonzMT/mzC EVtlLihOCeTV9NaYV/+qyA3UHupoeLxxdF0OQC8f/4LWkKLhOj0Raih5gm6Dp+PQq+DA FSHw== X-Gm-Message-State: APjAAAWd3FRrEyGr6tHvFjf2khk2gv4J19HVa2hQ20N/BspljHDjWp6u WqO2sgQUHEXUG/QSaOwNBaJ5aNsfQHDOsA== X-Google-Smtp-Source: APXvYqzqFt21puIXUPzTvZ1pTRBC7qeR9H7V3B7o9hPP8LmBOFSNqTQjr/Mzssiq4QyPn3LZzcVjKA== X-Received: by 2002:a17:906:a945:: with SMTP id hh5mr17633443ejb.108.1554743161691; Mon, 08 Apr 2019 10:06:01 -0700 (PDT) Received: from alrua-x1.borgediget.toke.dk (alrua-x1.vpn.toke.dk. [2a00:7660:6da:10::2]) by smtp.gmail.com with ESMTPSA id s24sm2652127edq.79.2019.04.08.10.05.57 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 08 Apr 2019 10:06:00 -0700 (PDT) Received: by alrua-x1.borgediget.toke.dk (Postfix, from userid 1000) id 08526181FEC; Mon, 8 Apr 2019 19:05:57 +0200 (CEST) Subject: [PATCH net-next v4 6/6] selftests/bpf: Add test for default devmap allocation From: Toke =?utf-8?q?H=C3=B8iland-J=C3=B8rgensen?= To: David Miller Cc: netdev@vger.kernel.org, Jesper Dangaard Brouer , Daniel Borkmann , Alexei Starovoitov , Jakub Kicinski , =?utf-8?b?QmrDtnJu?= =?utf-8?b?VMO2cGVs?= Date: Mon, 08 Apr 2019 19:05:57 +0200 Message-ID: <155474315697.24432.13044630044539815309.stgit@alrua-x1> In-Reply-To: <155474315642.24432.6179239576879119104.stgit@alrua-x1> References: <155474315642.24432.6179239576879119104.stgit@alrua-x1> User-Agent: StGit/unknown-version MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org This adds a new selftest checking the allocation and de-allocation of default maps in different network namespaces. It loads the two different kinds of programs that need allocation (programs using tail calls, and programs using redirect), and moves interfaces around between namespaces to make sure the default map is correctly allocated and de-allocated in each of the namespaces. Signed-off-by: Toke Høiland-Jørgensen --- tools/testing/selftests/bpf/Makefile | 3 - .../selftests/bpf/progs/test_xdp_tail_call.c | 39 ++++++++ .../testing/selftests/bpf/test_xdp_devmap_alloc.sh | 94 ++++++++++++++++++++ 3 files changed, 135 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/bpf/progs/test_xdp_tail_call.c create mode 100755 tools/testing/selftests/bpf/test_xdp_devmap_alloc.sh diff --git a/tools/testing/selftests/bpf/Makefile b/tools/testing/selftests/bpf/Makefile index 77b73b892136..07f2f54a6a87 100644 --- a/tools/testing/selftests/bpf/Makefile +++ b/tools/testing/selftests/bpf/Makefile @@ -54,7 +54,8 @@ TEST_PROGS := test_kmod.sh \ test_lwt_ip_encap.sh \ test_tcp_check_syncookie.sh \ test_tc_tunnel.sh \ - test_tc_edt.sh + test_tc_edt.sh \ + test_xdp_devmap_alloc.sh TEST_PROGS_EXTENDED := with_addr.sh \ with_tunnels.sh \ diff --git a/tools/testing/selftests/bpf/progs/test_xdp_tail_call.c b/tools/testing/selftests/bpf/progs/test_xdp_tail_call.c new file mode 100644 index 000000000000..6c89dc4ad341 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/test_xdp_tail_call.c @@ -0,0 +1,39 @@ +// SPDX-License-Identifier: GPL-2.0 + +#define KBUILD_MODNAME "xdp_dummy" +#include +#include "bpf_helpers.h" + +struct bpf_map_def SEC("maps") jmp_table = { + .type = BPF_MAP_TYPE_PROG_ARRAY, + .key_size = sizeof(__u32), + .value_size = sizeof(__u32), + .max_entries = 8, +}; + +struct bpf_map_def SEC("maps") arr_map = { + .type = BPF_MAP_TYPE_ARRAY, + .key_size = sizeof(__u32), + .value_size = sizeof(__u32), + .max_entries = 1, +}; + + +SEC("xdp_dummy_tail_call") +int xdp_dummy_prog(struct xdp_md *ctx) +{ + long *value; + __u32 key = 0; + + /* We just need the call instruction in the program, so it is fine that + * this fails, but it should not be optimised out by the compiler (so + * can't just do if (false)). + */ + value = bpf_map_lookup_elem(&arr_map, &key); + if (value) + bpf_tail_call(ctx, &jmp_table, 1); + + return XDP_PASS; +} + +char _license[] SEC("license") = "GPL"; diff --git a/tools/testing/selftests/bpf/test_xdp_devmap_alloc.sh b/tools/testing/selftests/bpf/test_xdp_devmap_alloc.sh new file mode 100755 index 000000000000..0431f94f136a --- /dev/null +++ b/tools/testing/selftests/bpf/test_xdp_devmap_alloc.sh @@ -0,0 +1,94 @@ +#!/bin/bash + +cleanup() +{ + if [ "$?" = "0" ]; then + echo "selftests: test_xdp_devmap_alloc [PASS]"; + else + echo "selftests: test_xdp_devmap_alloc [FAILED]"; + fi + + set +e + ip link del veth1 2> /dev/null + ip netns del ns1 2> /dev/null + ip netns del ns2 2> /dev/null +} + +check_alloc() +{ + ns="$1" + expected="$2 $3" + + if [[ "$ns" == "root" ]]; then + actual=$(< /proc/net/default_dev_map) + else + actual=$(ip netns exec "$ns" cat /proc/net/default_dev_map) + fi + + if [[ "$expected" != "$actual" ]]; then + echo "Expected allocation '$expected' got '$actual'" >&2 + exit 1 + fi +} + +ip link set dev lo xdp off 2>/dev/null > /dev/null +if [ $? -ne 0 ];then + echo "selftests: [SKIP] Could not run test without ip xdp support" + exit 0 +fi +set -e + +ip netns add ns1 +ip netns add ns2 + +trap cleanup 0 2 3 6 9 + +ip link add veth1 type veth peer name veth2 + +ip link set veth1 netns ns1 +ip link set veth2 netns ns2 + +check_alloc ns1 0 0 +check_alloc ns2 0 0 + +# Check that loading an xdp tail call program increases counter, but doesn't +# load a program +ip netns exec ns2 ip link set dev veth2 xdp obj test_xdp_tail_call.o sec xdp_dummy_tail_call +check_alloc ns2 1 0 + +# Check that loading a redirect program allocates a map, and +# removing that program de-allocates the map again. +ip netns exec ns1 ip link set dev veth1 xdp obj test_xdp_redirect.o sec redirect_to_111 +check_alloc ns1 1 1 +# Now we should have a map allocated in the other ns +check_alloc ns2 1 1 +ip netns exec ns1 ip link set dev veth1 xdp off +check_alloc ns1 0 0 +check_alloc ns2 1 0 +ip netns exec ns2 ip link set dev veth2 xdp off +check_alloc ns2 0 0 + +# Check that switching between redirect and non-redirect programs correctly +# allocs/de-allocs map +ip netns exec ns1 ip link set dev veth1 xdp obj xdp_dummy.o sec xdp_dummy +check_alloc ns1 0 0 +ip netns exec ns1 ip -force link set dev veth1 xdp obj test_xdp_redirect.o sec redirect_to_111 +check_alloc ns1 1 1 +ip netns exec ns1 ip -force link set dev veth1 xdp obj xdp_dummy.o sec xdp_dummy +check_alloc ns1 0 0 + +ip netns exec ns1 ip link set dev veth1 xdp off + +# Check that moving an interface into a namespace will allocate the map +ip netns del ns1 +ip netns add ns1 +ip link add veth1 type veth peer name veth2 + +ip link set dev veth1 xdp obj test_xdp_redirect.o sec redirect_to_111 +check_alloc root 1 1 +check_alloc ns1 0 0 +ip link set dev veth1 netns ns1 +check_alloc ns1 1 1 +check_alloc root 0 0 + +exit 0