From patchwork Sun Mar 26 09:22:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 75036 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp844584vqo; Sun, 26 Mar 2023 02:37:53 -0700 (PDT) X-Google-Smtp-Source: AKy350YbQSgrNNen83iuDd5uFmMzrrgzSJIfKjbcZqRaR+kvohrom9T847DkRJz7ayx30uGb3Nml X-Received: by 2002:a17:906:2581:b0:87b:6bbb:11ac with SMTP id m1-20020a170906258100b0087b6bbb11acmr8416325ejb.60.1679823473521; Sun, 26 Mar 2023 02:37:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679823473; cv=none; d=google.com; s=arc-20160816; b=eSzmJtW0yUY/dx5yywuRypc/lA7h0XjgVSnS+OZ8Gkfu7CVvK554/pj0pEhVWyWzKy gw0Mcn6s1vJsHljgwzDr+93Z8jjE5wMjfVLkO5t+NgHuHRwx6x3kcOE+oSXY1HzMcoHf Ow4R22prGZa4B9vkcdSZlBfB9Qyiu9QFM9otELA0c5bU11B56Y5niok6d+htZ5RGhX0r TOwkzuq4nKpKutJ8LOr3DRGP96gPq1VKZFx3y0H5JMbsUlLFVcE0XUOCRtJ3qUsQ9dbO 9vQ1aI1m0DALKbVOIGCqrI24LP0rVQF5GG/H/g/oOyEv5OQJak3IKlfTgQrdxcw5qY17 A/ig== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=O7x7mlObk0mq6o5G4acCq3J/Q3Teazds0AbiZGRGfrs=; b=XrCeGPat7NuNdS1RDPcA2MYZQlKcO1D1q00n8FFHT4BVZQ6vLsFCl5bjIjkV8Y9+5c JRgmzlupj/rzZHikAFxT1hmF9qdNEJ+OsAi9M4JGadSSULbDAWVsWfAyLSaEZrkYz8DD 7JfwQ27Zf7dBwUhBifg/GAkonE9aSHW7gHBezH/mS8iarIuV90/cupGHxxAzMRwQGeME 7u5LZ3hiteCzO2EnT0iUxs0XBxraac60LCcoR+rq/KgW0rziskUgU6RtKpox3avaFNkW Ha/Qj0chzTQUM/w2g4fPXJAV1/NC8+pU2lap0Yr1RaFcxMKoPy5cUsP1p/5HdSJ1r8lv tbJQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=jH3+gOe0; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id hs28-20020a1709073e9c00b0093defbd6287si4289224ejc.1038.2023.03.26.02.37.30; Sun, 26 Mar 2023 02:37:53 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=jH3+gOe0; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232206AbjCZJXM (ORCPT + 99 others); Sun, 26 Mar 2023 05:23:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44724 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231959AbjCZJWv (ORCPT ); Sun, 26 Mar 2023 05:22:51 -0400 Received: from mail-qt1-x831.google.com (mail-qt1-x831.google.com [IPv6:2607:f8b0:4864:20::831]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E7C1EA271; Sun, 26 Mar 2023 02:22:30 -0700 (PDT) Received: by mail-qt1-x831.google.com with SMTP id ga7so5890736qtb.2; Sun, 26 Mar 2023 02:22:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1679822548; 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=O7x7mlObk0mq6o5G4acCq3J/Q3Teazds0AbiZGRGfrs=; b=jH3+gOe0IubDUQiVblq9CC8G+sjt1vhQU+uyAo7rRHDtbXyRUrTQxeklq+IOr53MbF +JYKWg1fZnDvfmrwc+gsil+GBkuqcn2x09nVlYt0LXju/gaIOUm3TRiZfMcRNEKDLmY2 sHZTTsTM7IQQuu7BP0hWxOYAsdg1RiD4HM8lNSWRZc2DiOY9zbVyy4viinugjDtW5OeL HBXxrunxHvVUw08WRg41zi+FecGZpIJ+EJTWpWdbOYwQ06eKSTp0a7ERHLAkglIRyWBf MZrumVFQ/Gt3TqWo47rtHv7aWHQZXeacEhaVZi2bRBHmgiXq+ykd/ZtjMXYCTsaTzyvz vKZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679822548; 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=O7x7mlObk0mq6o5G4acCq3J/Q3Teazds0AbiZGRGfrs=; b=3tSr0Pb7PiLPyRM8+r+jfKfjhskfqOO1gQjF7l55tQ4NxIyYAPsmqqjiHJ5lfEoRum C5ARxjqq8PpAIixfa/P8Koh7UuADOzIUk3w3Ri7thPID4G889z8Myl/7f6Eli0f9L7xE EwmFcuBL75X0hiAsH2f/xEUGIClYVhDYIYDl+29uJYJxnbsucCDuOI1Bh/BkpVAIjEJP ZPVDaEoIrstcu39qAd8hZ/TffdaC/s0ikBvu8SQKRd7jzJ5vBAmUt7OytvW1AFDHBmuV 4VqW51gHKa1cC3UWAJSdZH8fStRT6yFykSOL8tCoin/o6vqyu9dByljSVIWlKRxAr2Z5 r8Ew== X-Gm-Message-State: AAQBX9fsiALRR3nnyX/VR6BN8BgizeZecIbj8xwaNdq5JsmQXP28Fgno NBJePNfP2vLooIUNOSHNMEs= X-Received: by 2002:a05:622a:494:b0:3e4:e5bf:a24c with SMTP id p20-20020a05622a049400b003e4e5bfa24cmr1497323qtx.62.1679822548595; Sun, 26 Mar 2023 02:22:28 -0700 (PDT) Received: from vultr.guest ([2001:19f0:1000:1a1f:5400:4ff:fe5e:1d32]) by smtp.gmail.com with ESMTPSA id y5-20020ac87085000000b003e014845d9esm10257987qto.74.2023.03.26.02.22.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 26 Mar 2023 02:22:28 -0700 (PDT) From: Yafang Shao To: 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, sdf@google.com, haoluo@google.com, jolsa@kernel.org Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, Yafang Shao Subject: [RFC PATCH bpf-next 13/13] selftests/bpf: Add selftest for bpf namespace Date: Sun, 26 Mar 2023 09:22:08 +0000 Message-Id: <20230326092208.13613-14-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230326092208.13613-1-laoar.shao@gmail.com> References: <20230326092208.13613-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-0.2 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1761422578360099662?= X-GMAIL-MSGID: =?utf-8?q?1761422578360099662?= A simple test case is added for the newly introduced bpf namespcae. Signed-off-by: Yafang Shao --- tools/testing/selftests/bpf/Makefile | 3 +- tools/testing/selftests/bpf/test_bpfns.c | 76 ++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/bpf/test_bpfns.c diff --git a/tools/testing/selftests/bpf/Makefile b/tools/testing/selftests/bpf/Makefile index 4a8ef11..55f0aeb 100644 --- a/tools/testing/selftests/bpf/Makefile +++ b/tools/testing/selftests/bpf/Makefile @@ -40,7 +40,7 @@ TEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_lpm_map test test_sock test_sockmap get_cgroup_id_user \ test_cgroup_storage \ test_tcpnotify_user test_sysctl \ - test_progs-no_alu32 + test_progs-no_alu32 test_bpfns # Also test bpf-gcc, if present ifneq ($(BPF_GCC),) @@ -255,6 +255,7 @@ $(OUTPUT)/flow_dissector_load: $(TESTING_HELPERS) $(OUTPUT)/test_maps: $(TESTING_HELPERS) $(OUTPUT)/test_verifier: $(TESTING_HELPERS) $(CAP_HELPERS) $(UNPRIV_HELPERS) $(OUTPUT)/xsk.o: $(BPFOBJ) +$(OUTPUT)/test_bpfns: $(TESTING_HELPERS) BPFTOOL ?= $(DEFAULT_BPFTOOL) $(DEFAULT_BPFTOOL): $(wildcard $(BPFTOOLDIR)/*.[ch] $(BPFTOOLDIR)/Makefile) \ diff --git a/tools/testing/selftests/bpf/test_bpfns.c b/tools/testing/selftests/bpf/test_bpfns.c new file mode 100644 index 0000000..7baebe2 --- /dev/null +++ b/tools/testing/selftests/bpf/test_bpfns.c @@ -0,0 +1,76 @@ +// SPDX-License-Identifier: GPL-2.0 +#ifndef _GNU_SOURCE +#define _GNU_SOURCE 1 +#endif +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +static int create_bpf_map(const char *name) +{ + static struct bpf_map_create_opts map_opts = { + .sz = sizeof(map_opts), + }; + unsigned int value; + unsigned int key; + int map_fd; + + map_fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, name, sizeof(key), + sizeof(value), 1, &map_opts); + if (map_fd < 0) + fprintf(stderr, "%s - Failed to create map\n", strerror(errno)); + return map_fd; +} + + +int main(int argc, char *argv[]) +{ + struct bpf_map_info info = {}; + __u32 info_len = sizeof(info); + struct clone_args args = { + .flags = 0x400000000ULL, /* CLONE_NEWBPF */ + .exit_signal = SIGCHLD, + }; + int map_fd, child_map_fd; + pid_t pid; + + /* Create a map in init bpf namespace. */ + map_fd = create_bpf_map("map_in_init"); + if (map_fd < 0) + exit(EXIT_FAILURE); + pid = syscall(__NR_clone3, &args, sizeof(struct clone_args)); + if (pid < 0) { + fprintf(stderr, "%s - Failed to create new process\n", strerror(errno)); + exit(EXIT_FAILURE); + } + + if (pid == 0) { + struct bpf_map_info info = {}; + + /* In a new bpf namespace, it is the first map. */ + child_map_fd = create_bpf_map("map_in_bpfns"); + if (child_map_fd < 0) + exit(EXIT_FAILURE); + bpf_obj_get_info_by_fd(child_map_fd, &info, &info_len); + assert(info.id == 1); + exit(EXIT_SUCCESS); + } + + if (waitpid(pid, NULL, 0) != pid) { + fprintf(stderr, "Failed to wait on child process\n"); + exit(EXIT_FAILURE); + } + + return 0; +}