All of lore.kernel.org
 help / color / mirror / Atom feed
From: Hou Tao <houtao1@huawei.com>
To: Alexei Starovoitov <ast@kernel.org>
Cc: Martin KaFai Lau <kafai@fb.com>, Yonghong Song <yhs@fb.com>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Andrii Nakryiko <andrii@kernel.org>,
	Song Liu <songliubraving@fb.com>,
	John Fastabend <john.fastabend@gmail.com>,
	<netdev@vger.kernel.org>, <bpf@vger.kernel.org>,
	<houtao1@huawei.com>
Subject: [RFC PATCH bpf-next v2 2/3] selftests/bpf: add a simple test for htab str key
Date: Mon, 14 Feb 2022 19:13:36 +0800	[thread overview]
Message-ID: <20220214111337.3539-3-houtao1@huawei.com> (raw)
In-Reply-To: <20220214111337.3539-1-houtao1@huawei.com>

Add a test to demonstrate that str key doesn't care about
the content of unused part in hash-table key.

Signed-off-by: Hou Tao <houtao1@huawei.com>
---
 .../selftests/bpf/prog_tests/str_key.c        | 71 ++++++++++++++++++
 tools/testing/selftests/bpf/progs/str_key.c   | 75 +++++++++++++++++++
 2 files changed, 146 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..998f12f8b919
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/str_key.c
@@ -0,0 +1,71 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (C) 2022. Huawei Technologies Co., Ltd */
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <test_progs.h>
+#include "str_key.skel.h"
+
+#define HTAB_NAME_SIZE 64
+
+struct str_htab_key {
+	struct bpf_str_key_stor name;
+	char raw[HTAB_NAME_SIZE];
+};
+
+static int setup_maps(struct str_key *skel, const char *name, unsigned int value)
+{
+	struct str_htab_key key;
+	int fd, err;
+
+	memset(&key, 0, sizeof(key));
+	strncpy(key.raw, name, sizeof(key.raw) - 1);
+	key.name.len = strlen(name) + 1;
+
+	fd = bpf_map__fd(skel->maps.str_htab);
+	err = bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST);
+	if (!ASSERT_OK(err, "str htab add"))
+		return -EINVAL;
+
+	fd = bpf_map__fd(skel->maps.byte_htab);
+	err = bpf_map_update_elem(fd, key.raw, &value, BPF_NOEXIST);
+	if (!ASSERT_OK(err, "byte 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;
+
+	srandom(time(NULL));
+	value = random();
+	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->str_htab_value, value, "str htab find");
+	ASSERT_EQ(skel->bss->byte_htab_value, -1, "byte htab find");
+
+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..4d5a4b7cf183
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/str_key.c
@@ -0,0 +1,75 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (C) 2022. Huawei Technologies Co., Ltd */
+#include <linux/types.h>
+#include <linux/bpf.h>
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_core_read.h>
+#include <bpf/bpf_tracing.h>
+
+char _license[] SEC("license") = "GPL";
+
+struct path {
+} __attribute__((preserve_access_index));
+
+struct file {
+	struct path f_path;
+} __attribute__((preserve_access_index));
+
+#define HTAB_NAME_SIZE 64
+
+struct str_htab_key {
+	struct bpf_str_key_stor name;
+	char raw[HTAB_NAME_SIZE];
+};
+
+struct {
+	__uint(type, BPF_MAP_TYPE_HASH);
+	__uint(max_entries, 1);
+	__type(key, struct str_htab_key);
+	__type(value, __u32);
+	__uint(map_flags, BPF_F_STR_IN_KEY);
+} str_htab SEC(".maps");
+
+struct {
+	__uint(type, BPF_MAP_TYPE_HASH);
+	__uint(max_entries, 1);
+	__uint(key_size, HTAB_NAME_SIZE);
+	__uint(value_size, sizeof(__u32));
+} byte_htab SEC(".maps");
+
+int pid = 0;
+unsigned int str_htab_value = 0;
+unsigned int byte_htab_value = 0;
+
+SEC("fentry/filp_close")
+int BPF_PROG(filp_close, struct file *filp)
+{
+	struct path *p = &filp->f_path;
+	struct str_htab_key key;
+	unsigned int *value;
+	int len;
+
+	if (bpf_get_current_pid_tgid() >> 32 != pid)
+		return 0;
+
+	__builtin_memset(key.raw, 0, sizeof(key.raw));
+	len = bpf_d_path(p, key.raw, sizeof(key.raw));
+	if (len < 0 || len > sizeof(key.raw))
+		return 0;
+
+	key.name.hash = 0;
+	key.name.len = len;
+	value = bpf_map_lookup_elem(&str_htab, &key);
+	if (value)
+		str_htab_value = *value;
+	else
+		str_htab_value = -1;
+
+	value = bpf_map_lookup_elem(&byte_htab, key.raw);
+	if (value)
+		byte_htab_value = *value;
+	else
+		byte_htab_value = -1;
+
+	return 0;
+}
-- 
2.25.4


  parent reply	other threads:[~2022-02-14 11:18 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-14 11:13 [RFC PATCH bpf-next v2 0/3] bpf: support string key in htab Hou Tao
2022-02-14 11:13 ` [RFC PATCH bpf-next v2 1/3] bpf: add support for string in hash table key Hou Tao
2022-02-14 11:13 ` Hou Tao [this message]
2022-02-14 11:13 ` [RFC PATCH bpf-next v2 3/3] selftests/bpf: add benchmark for string-key hash table Hou Tao
2022-02-17  3:50 ` [RFC PATCH bpf-next v2 0/3] bpf: support string key in htab Alexei Starovoitov
2022-02-18 13:53   ` Hou Tao
2022-02-19 18:44     ` Alexei Starovoitov
     [not found]       ` <ecc04a70-0b57-62ef-ab52-e7169845d789@huawei.com>
2022-02-27  3:08         ` Alexei Starovoitov
2022-03-09 11:47           ` Hou Tao

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220214111337.3539-3-houtao1@huawei.com \
    --to=houtao1@huawei.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=john.fastabend@gmail.com \
    --cc=kafai@fb.com \
    --cc=netdev@vger.kernel.org \
    --cc=songliubraving@fb.com \
    --cc=yhs@fb.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.