From patchwork Tue Jul 26 13:00:04 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hou Tao X-Patchwork-Id: 12929245 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 A495ACCA47E for ; Tue, 26 Jul 2022 12:42:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233657AbiGZMmB (ORCPT ); Tue, 26 Jul 2022 08:42:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43244 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238516AbiGZMmA (ORCPT ); Tue, 26 Jul 2022 08:42:00 -0400 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7AACB255AB for ; Tue, 26 Jul 2022 05:41:58 -0700 (PDT) Received: from dggpeml500025.china.huawei.com (unknown [172.30.72.55]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4Lsc1z2BZgzgYtf; Tue, 26 Jul 2022 20:40:07 +0800 (CST) Received: from huawei.com (10.175.124.27) by dggpeml500025.china.huawei.com (7.185.36.35) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Tue, 26 Jul 2022 20:41:55 +0800 From: Hou Tao To: Andrii Nakryiko , Alexei Starovoitov , CC: Daniel Borkmann , Martin KaFai Lau , Yonghong Song , Song Liu , KP Singh , "David S . Miller" , Jakub Kicinski , Stanislav Fomichev , Hao Luo , Jiri Olsa , John Fastabend , Subject: [RFC PATCH bpf-next 2/3] selftests/bpf: add a simple test for qp-trie Date: Tue, 26 Jul 2022 21:00:04 +0800 Message-ID: <20220726130005.3102470-3-houtao1@huawei.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20220726130005.3102470-1-houtao1@huawei.com> References: <20220726130005.3102470-1-houtao1@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.175.124.27] X-ClientProxiedBy: dggems706-chm.china.huawei.com (10.3.19.183) To dggpeml500025.china.huawei.com (7.185.36.35) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net X-Patchwork-State: RFC Add a test to demonstrate that qp-trie doesn't care about the unused part of key. Signed-off-by: Hou Tao --- .../selftests/bpf/prog_tests/str_key.c | 69 +++++++++++++++ tools/testing/selftests/bpf/progs/str_key.c | 85 +++++++++++++++++++ 2 files changed, 154 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/str_key.c create mode 100644 tools/testing/selftests/bpf/progs/str_key.c diff --git a/tools/testing/selftests/bpf/prog_tests/str_key.c b/tools/testing/selftests/bpf/prog_tests/str_key.c new file mode 100644 index 000000000000..8f134dd45902 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/str_key.c @@ -0,0 +1,69 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (C) 2022. Huawei Technologies Co., Ltd */ +#include +#include +#include +#include +#include +#include "str_key.skel.h" + +#define FILE_PATH_SIZE 64 + +struct file_path_str { + unsigned int len; + char raw[FILE_PATH_SIZE]; +}; + +static int setup_maps(struct str_key *skel, const char *name, unsigned int value) +{ + struct file_path_str key; + int fd, err; + + memset(&key, 0, sizeof(key)); + strncpy(key.raw, name, sizeof(key.raw) - 1); + key.len = strlen(name) + 1; + + fd = bpf_map__fd(skel->maps.trie); + err = bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST); + if (!ASSERT_OK(err, "trie add")) + return -EINVAL; + + fd = bpf_map__fd(skel->maps.htab); + err = bpf_map_update_elem(fd, key.raw, &value, BPF_NOEXIST); + if (!ASSERT_OK(err, "htab add")) + return -EINVAL; + + return 0; +} + +void test_str_key(void) +{ + const char *name = "/tmp/str_key_test"; + struct str_key *skel; + unsigned int value; + int err, fd; + + skel = str_key__open_and_load(); + if (!ASSERT_OK_PTR(skel, "open_load str key")) + return; + + value = time(NULL); + if (setup_maps(skel, name, value)) + goto out; + + skel->bss->pid = getpid(); + err = str_key__attach(skel); + if (!ASSERT_OK(err, "attach")) + goto out; + + fd = open(name, O_RDONLY | O_CREAT, 0644); + if (!ASSERT_GE(fd, 0, "open tmp file")) + goto out; + close(fd); + unlink(name); + + ASSERT_EQ(skel->bss->trie_value, value, "trie lookup str"); + ASSERT_EQ(skel->bss->htab_value, -1, "htab lookup str"); +out: + str_key__destroy(skel); +} diff --git a/tools/testing/selftests/bpf/progs/str_key.c b/tools/testing/selftests/bpf/progs/str_key.c new file mode 100644 index 000000000000..2e8becdd58c6 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/str_key.c @@ -0,0 +1,85 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (C) 2022. Huawei Technologies Co., Ltd */ +#include +#include +#include +#include +#include + +char _license[] SEC("license") = "GPL"; + +struct path { +} __attribute__((preserve_access_index)); + +struct file { + struct path f_path; +} __attribute__((preserve_access_index)); + +#define FILE_PATH_SIZE 64 + +struct file_path_str { + unsigned int len; + char raw[FILE_PATH_SIZE]; +}; + +struct { + __uint(type, BPF_MAP_TYPE_ARRAY); + __uint(max_entries, 1); + __type(key, int); + __type(value, struct file_path_str); +} array SEC(".maps"); + +struct { + __uint(type, BPF_MAP_TYPE_QP_TRIE); + __uint(max_entries, 1); + __type(key, struct file_path_str); + __type(value, __u32); + __uint(map_flags, BPF_F_NO_PREALLOC); +} trie SEC(".maps"); + +struct { + __uint(type, BPF_MAP_TYPE_HASH); + __uint(max_entries, 1); + __uint(key_size, FILE_PATH_SIZE); + __uint(value_size, sizeof(__u32)); +} htab SEC(".maps"); + +int pid = 0; +unsigned int trie_value = 0; +unsigned int htab_value = 0; + +SEC("fentry/filp_close") +int BPF_PROG(filp_close, struct file *filp) +{ + struct path *p = &filp->f_path; + struct file_path_str *str; + unsigned int *value; + int idx, len; + + if (bpf_get_current_pid_tgid() >> 32 != pid) + return 0; + + idx = 0; + str = bpf_map_lookup_elem(&array, &idx); + if (!str) + return 0; + + len = bpf_d_path(p, str->raw, sizeof(str->raw)); + if (len < 0 || len > sizeof(str->raw)) + return 0; + + str->len = len; + value = bpf_map_lookup_elem(&trie, str); + if (value) + trie_value = *value; + else + trie_value = -1; + + value = bpf_map_lookup_elem(&htab, str->raw); + if (value) + htab_value = *value; + else + htab_value = -1; + + return 0; +}