BPF List
 help / color / mirror / Atom feed
From: Yafang Shao <laoar.shao@gmail.com>
To: roman.gushchin@linux.dev, 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,
	shuah@kernel.org
Cc: netdev@vger.kernel.org, bpf@vger.kernel.org,
	linux-kselftest@vger.kernel.org,
	Yafang Shao <laoar.shao@gmail.com>
Subject: [PATCH 13/14] bpf: selftests: Add test case for BPF_F_NO_CHARTE
Date: Sat, 19 Mar 2022 17:30:35 +0000	[thread overview]
Message-ID: <20220319173036.23352-14-laoar.shao@gmail.com> (raw)
In-Reply-To: <20220319173036.23352-1-laoar.shao@gmail.com>

BPF_F_NO_CHARTE test case for various maps. Below is the result,
$ ./test_maps
...
test_no_charge:PASS
...

Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
---
 .../selftests/bpf/map_tests/no_charg.c        | 79 +++++++++++++++++++
 1 file changed, 79 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/map_tests/no_charg.c

diff --git a/tools/testing/selftests/bpf/map_tests/no_charg.c b/tools/testing/selftests/bpf/map_tests/no_charg.c
new file mode 100644
index 000000000000..db18685a53f7
--- /dev/null
+++ b/tools/testing/selftests/bpf/map_tests/no_charg.c
@@ -0,0 +1,79 @@
+// SPDX-License-Identifier: GPL-2.0-only
+#include <sys/syscall.h>
+#include <linux/bpf.h>
+#include <string.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <errno.h>
+
+#include <test_maps.h>
+
+struct map_attr {
+	__u32 map_type;
+	char *map_name;
+	__u32 key_size;
+	__u32 value_size;
+	__u32 max_entries;
+	__u32 map_flags;
+};
+
+static struct map_attr attrs[] = {
+	{BPF_MAP_TYPE_HASH, "BPF_MAP_TYPE_HASH", 4, 4, 10},
+	{BPF_MAP_TYPE_ARRAY, "BPF_MAP_TYPE_ARRAY", 4, 4, 10},
+	{BPF_MAP_TYPE_PROG_ARRAY, "BPF_MAP_TYPE_PROG_ARRAY", 4, 4, 10},
+	{BPF_MAP_TYPE_PERF_EVENT_ARRAY, "BPF_MAP_TYPE_PERF_EVENT_ARRAY", 4, 4, 10},
+	{BPF_MAP_TYPE_PERCPU_HASH, "BPF_MAP_TYPE_PERCPU_HASH", 4, 4, 10},
+	{BPF_MAP_TYPE_PERCPU_ARRAY, "BPF_MAP_TYPE_PERCPU_ARRAY", 4, 4, 10},
+	{BPF_MAP_TYPE_STACK_TRACE, "BPF_MAP_TYPE_STACK_TRACE", 4, 8, 10},
+	{BPF_MAP_TYPE_CGROUP_ARRAY, "BPF_MAP_TYPE_CGROUP_ARRAY", 4, 4, 10},
+	{BPF_MAP_TYPE_LRU_HASH, "BPF_MAP_TYPE_LRU_HASH", 4, 4, 10},
+	{BPF_MAP_TYPE_LRU_PERCPU_HASH, "BPF_MAP_TYPE_LRU_PERCPU_HASH", 4, 4, 10},
+	{BPF_MAP_TYPE_LPM_TRIE, "BPF_MAP_TYPE_LPM_TRIE", 32, 4, 10, BPF_F_NO_PREALLOC},
+	{BPF_MAP_TYPE_DEVMAP, "BPF_MAP_TYPE_DEVMAP", 4, 4, 10},
+	{BPF_MAP_TYPE_SOCKMAP, "BPF_MAP_TYPE_SOCKMAP", 4, 4, 10},
+	{BPF_MAP_TYPE_CPUMAP, "BPF_MAP_TYPE_CPUMAP", 4, 4, 10},
+	{BPF_MAP_TYPE_XSKMAP, "BPF_MAP_TYPE_XSKMAP", 4, 4, 10},
+	{BPF_MAP_TYPE_SOCKHASH, "BPF_MAP_TYPE_SOCKHASH", 4, 4, 10},
+	{BPF_MAP_TYPE_REUSEPORT_SOCKARRAY, "BPF_MAP_TYPE_REUSEPORT_SOCKARRAY", 4, 4, 10},
+	{BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE, "BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE", 8, 4, 0},
+	{BPF_MAP_TYPE_QUEUE, "BPF_MAP_TYPE_QUEUE", 0, 4, 10},
+	{BPF_MAP_TYPE_DEVMAP_HASH, "BPF_MAP_TYPE_DEVMAP_HASH", 4, 4, 10},
+	{BPF_MAP_TYPE_RINGBUF, "BPF_MAP_TYPE_RINGBUF", 0, 0, 4096},
+	{BPF_MAP_TYPE_BLOOM_FILTER, "BPF_MAP_TYPE_BLOOM_FILTER", 0, 4, 10},
+};
+
+static __u32 flags[] = {
+	BPF_F_NO_CHARGE,
+};
+
+void test_map_flags(union bpf_attr *attr, char *name)
+{
+	int mfd;
+
+	mfd = syscall(SYS_bpf, BPF_MAP_CREATE, attr, sizeof(*attr));
+	CHECK(mfd <= 0 && mfd != -EPERM, "no_charge", "%s error: %s\n",
+		name, strerror(errno));
+
+	if (mfd > 0)
+		close(mfd);
+}
+
+void test_no_charge(void)
+{
+	union bpf_attr attr;
+	int i, j;
+
+	memset(&attr, 0, sizeof(attr));
+	for (i = 0; i < sizeof(flags) / sizeof(__u32); i++) {
+		for (j = 0; j < sizeof(attrs) / sizeof(struct map_attr); j++) {
+			attr.map_type = attrs[j].map_type;
+			attr.key_size = attrs[j].key_size;
+			attr.value_size = attrs[j].value_size;
+			attr.max_entries = attrs[j].max_entries;
+			attr.map_flags = attrs[j].map_flags | flags[i];
+			test_map_flags(&attr, attrs[j].map_name);
+		}
+	}
+
+	printf("%s:PASS\n", __func__);
+}
-- 
2.17.1


  parent reply	other threads:[~2022-03-19 17:31 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-19 17:30 [PATCH 00/14] bpf: Allow not to charge bpf memory Yafang Shao
2022-03-19 17:30 ` [PATCH 01/14] bpf: Introduce no charge flag for bpf map Yafang Shao
2022-03-19 17:30 ` [PATCH 02/14] bpf: Only sys admin can set no charge flag Yafang Shao
2022-03-19 17:30 ` [PATCH 03/14] bpf: Enable no charge in map _CREATE_FLAG_MASK Yafang Shao
2022-03-19 17:30 ` [PATCH 04/14] bpf: Introduce new parameter bpf_attr in bpf_map_area_alloc Yafang Shao
2022-03-19 17:30 ` [PATCH 05/14] bpf: Allow no charge " Yafang Shao
2022-03-19 17:30 ` [PATCH 06/14] bpf: Allow no charge for allocation not at map creation time Yafang Shao
2022-03-19 17:30 ` [PATCH 07/14] bpf: Allow no charge in map specific allocation Yafang Shao
2022-03-19 17:30 ` [PATCH 08/14] bpf: Aggregate flags for BPF_PROG_LOAD command Yafang Shao
2022-03-19 17:30 ` [PATCH 09/14] bpf: Add no charge flag for bpf prog Yafang Shao
2022-03-19 17:30 ` [PATCH 10/14] bpf: Only sys admin can set " Yafang Shao
2022-03-19 17:30 ` [PATCH 11/14] bpf: Set __GFP_ACCOUNT at the callsite of bpf_prog_alloc Yafang Shao
2022-03-19 17:30 ` [PATCH 12/14] bpf: Allow no charge for bpf prog Yafang Shao
2022-03-19 17:30 ` Yafang Shao [this message]
2022-03-19 17:30 ` [PATCH 14/14] bpf: selftests: Add test case for BPF_F_PROG_NO_CHARGE Yafang Shao
2022-03-21 22:52 ` [PATCH 00/14] bpf: Allow not to charge bpf memory Roman Gushchin
2022-03-22 16:10   ` Yafang Shao
2022-03-22 19:10     ` Roman Gushchin
2022-03-23  1:37       ` Yafang Shao

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=20220319173036.23352-14-laoar.shao@gmail.com \
    --to=laoar.shao@gmail.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=kpsingh@kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=roman.gushchin@linux.dev \
    --cc=shuah@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox