Commit 2fe256a4 authored by Andrii Nakryiko's avatar Andrii Nakryiko Committed by Daniel Borkmann
Browse files

selftests/bpf: Migrate selftests to bpf_map_create()



Conversion is straightforward for most cases. In few cases tests are
using mutable map_flags and attribute structs, but bpf_map_create_opts
can be used in the similar fashion, so there were no problems. Just lots
of repetitive conversions.

Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
Link: https://lore.kernel.org/bpf/20211124193233.3115996-5-andrii@kernel.org
parent 99a12a32
Loading
Loading
Loading
Loading
+3 −10
Original line number Diff line number Diff line
@@ -68,13 +68,6 @@ static void map_batch_verify(int *visited, __u32 max_entries, int *keys,

static void __test_map_lookup_and_update_batch(bool is_pcpu)
{
	struct bpf_create_map_attr xattr = {
		.name = "array_map",
		.map_type = is_pcpu ? BPF_MAP_TYPE_PERCPU_ARRAY :
				      BPF_MAP_TYPE_ARRAY,
		.key_size = sizeof(int),
		.value_size = sizeof(__s64),
	};
	int map_fd, *keys, *visited;
	__u32 count, total, total_success;
	const __u32 max_entries = 10;
@@ -86,10 +79,10 @@ static void __test_map_lookup_and_update_batch(bool is_pcpu)
		.flags = 0,
	);

	xattr.max_entries = max_entries;
	map_fd = bpf_create_map_xattr(&xattr);
	map_fd = bpf_map_create(is_pcpu ? BPF_MAP_TYPE_PERCPU_ARRAY : BPF_MAP_TYPE_ARRAY,
				"array_map", sizeof(int), sizeof(__s64), max_entries, NULL);
	CHECK(map_fd == -1,
	      "bpf_create_map_xattr()", "error:%s\n", strerror(errno));
	      "bpf_map_create()", "error:%s\n", strerror(errno));

	value_size = sizeof(__s64);
	if (is_pcpu)
+3 −10
Original line number Diff line number Diff line
@@ -83,22 +83,15 @@ void __test_map_lookup_and_delete_batch(bool is_pcpu)
	int err, step, value_size;
	bool nospace_err;
	void *values;
	struct bpf_create_map_attr xattr = {
		.name = "hash_map",
		.map_type = is_pcpu ? BPF_MAP_TYPE_PERCPU_HASH :
			    BPF_MAP_TYPE_HASH,
		.key_size = sizeof(int),
		.value_size = sizeof(int),
	};
	DECLARE_LIBBPF_OPTS(bpf_map_batch_opts, opts,
		.elem_flags = 0,
		.flags = 0,
	);

	xattr.max_entries = max_entries;
	map_fd = bpf_create_map_xattr(&xattr);
	map_fd = bpf_map_create(is_pcpu ? BPF_MAP_TYPE_PERCPU_HASH : BPF_MAP_TYPE_HASH,
				"hash_map", sizeof(int), sizeof(int), max_entries, NULL);
	CHECK(map_fd == -1,
	      "bpf_create_map_xattr()", "error:%s\n", strerror(errno));
	      "bpf_map_create()", "error:%s\n", strerror(errno));

	value_size = is_pcpu ? sizeof(value) : sizeof(int);
	keys = malloc(max_entries * sizeof(int));
+5 −10
Original line number Diff line number Diff line
@@ -64,13 +64,7 @@ static void map_batch_verify(int *visited, __u32 max_entries,

void test_lpm_trie_map_batch_ops(void)
{
	struct bpf_create_map_attr xattr = {
		.name = "lpm_trie_map",
		.map_type = BPF_MAP_TYPE_LPM_TRIE,
		.key_size = sizeof(struct test_lpm_key),
		.value_size = sizeof(int),
		.map_flags = BPF_F_NO_PREALLOC,
	};
	LIBBPF_OPTS(bpf_map_create_opts, create_opts, .map_flags = BPF_F_NO_PREALLOC);
	struct test_lpm_key *keys, key;
	int map_fd, *values, *visited;
	__u32 step, count, total, total_success;
@@ -82,9 +76,10 @@ void test_lpm_trie_map_batch_ops(void)
		.flags = 0,
	);

	xattr.max_entries = max_entries;
	map_fd = bpf_create_map_xattr(&xattr);
	CHECK(map_fd == -1, "bpf_create_map_xattr()", "error:%s\n",
	map_fd = bpf_map_create(BPF_MAP_TYPE_LPM_TRIE, "lpm_trie_map",
				sizeof(struct test_lpm_key), sizeof(int),
				max_entries, &create_opts);
	CHECK(map_fd == -1, "bpf_map_create()", "error:%s\n",
	      strerror(errno));

	keys = malloc(max_entries * sizeof(struct test_lpm_key));
+22 −28
Original line number Diff line number Diff line
@@ -19,16 +19,12 @@
#include <test_btf.h>
#include <test_maps.h>

static struct bpf_create_map_attr xattr = {
	.name = "sk_storage_map",
	.map_type = BPF_MAP_TYPE_SK_STORAGE,
	.map_flags = BPF_F_NO_PREALLOC,
	.max_entries = 0,
	.key_size = 4,
	.value_size = 8,
static struct bpf_map_create_opts map_opts = {
	.sz = sizeof(map_opts),
	.btf_key_type_id = 1,
	.btf_value_type_id = 3,
	.btf_fd = -1,
	.map_flags = BPF_F_NO_PREALLOC,
};

static unsigned int nr_sk_threads_done;
@@ -150,13 +146,13 @@ static int create_sk_storage_map(void)
	btf_fd = load_btf();
	CHECK(btf_fd == -1, "bpf_load_btf", "btf_fd:%d errno:%d\n",
	      btf_fd, errno);
	xattr.btf_fd = btf_fd;
	map_opts.btf_fd = btf_fd;

	map_fd = bpf_create_map_xattr(&xattr);
	xattr.btf_fd = -1;
	map_fd = bpf_map_create(BPF_MAP_TYPE_SK_STORAGE, "sk_storage_map", 4, 8, 0, &map_opts);
	map_opts.btf_fd = -1;
	close(btf_fd);
	CHECK(map_fd == -1,
	      "bpf_create_map_xattr()", "errno:%d\n", errno);
	      "bpf_map_create()", "errno:%d\n", errno);

	return map_fd;
}
@@ -463,20 +459,20 @@ static void test_sk_storage_map_basic(void)
		int cnt;
		int lock;
	} value = { .cnt = 0xeB9f, .lock = 0, }, lookup_value;
	struct bpf_create_map_attr bad_xattr;
	struct bpf_map_create_opts bad_xattr;
	int btf_fd, map_fd, sk_fd, err;

	btf_fd = load_btf();
	CHECK(btf_fd == -1, "bpf_load_btf", "btf_fd:%d errno:%d\n",
	      btf_fd, errno);
	xattr.btf_fd = btf_fd;
	map_opts.btf_fd = btf_fd;

	sk_fd = socket(AF_INET6, SOCK_STREAM, 0);
	CHECK(sk_fd == -1, "socket()", "sk_fd:%d errno:%d\n",
	      sk_fd, errno);

	map_fd = bpf_create_map_xattr(&xattr);
	CHECK(map_fd == -1, "bpf_create_map_xattr(good_xattr)",
	map_fd = bpf_map_create(BPF_MAP_TYPE_SK_STORAGE, "sk_storage_map", 4, 8, 0, &map_opts);
	CHECK(map_fd == -1, "bpf_map_create(good_xattr)",
	      "map_fd:%d errno:%d\n", map_fd, errno);

	/* Add new elem */
@@ -560,31 +556,29 @@ static void test_sk_storage_map_basic(void)
	CHECK(!err || errno != ENOENT, "bpf_map_delete_elem()",
	      "err:%d errno:%d\n", err, errno);

	memcpy(&bad_xattr, &xattr, sizeof(xattr));
	memcpy(&bad_xattr, &map_opts, sizeof(map_opts));
	bad_xattr.btf_key_type_id = 0;
	err = bpf_create_map_xattr(&bad_xattr);
	CHECK(!err || errno != EINVAL, "bap_create_map_xattr(bad_xattr)",
	err = bpf_map_create(BPF_MAP_TYPE_SK_STORAGE, "sk_storage_map", 4, 8, 0, &bad_xattr);
	CHECK(!err || errno != EINVAL, "bpf_map_create(bad_xattr)",
	      "err:%d errno:%d\n", err, errno);

	memcpy(&bad_xattr, &xattr, sizeof(xattr));
	memcpy(&bad_xattr, &map_opts, sizeof(map_opts));
	bad_xattr.btf_key_type_id = 3;
	err = bpf_create_map_xattr(&bad_xattr);
	CHECK(!err || errno != EINVAL, "bap_create_map_xattr(bad_xattr)",
	err = bpf_map_create(BPF_MAP_TYPE_SK_STORAGE, "sk_storage_map", 4, 8, 0, &bad_xattr);
	CHECK(!err || errno != EINVAL, "bpf_map_create(bad_xattr)",
	      "err:%d errno:%d\n", err, errno);

	memcpy(&bad_xattr, &xattr, sizeof(xattr));
	bad_xattr.max_entries = 1;
	err = bpf_create_map_xattr(&bad_xattr);
	CHECK(!err || errno != EINVAL, "bap_create_map_xattr(bad_xattr)",
	err = bpf_map_create(BPF_MAP_TYPE_SK_STORAGE, "sk_storage_map", 4, 8, 1, &map_opts);
	CHECK(!err || errno != EINVAL, "bpf_map_create(bad_xattr)",
	      "err:%d errno:%d\n", err, errno);

	memcpy(&bad_xattr, &xattr, sizeof(xattr));
	memcpy(&bad_xattr, &map_opts, sizeof(map_opts));
	bad_xattr.map_flags = 0;
	err = bpf_create_map_xattr(&bad_xattr);
	err = bpf_map_create(BPF_MAP_TYPE_SK_STORAGE, "sk_storage_map", 4, 8, 0, &bad_xattr);
	CHECK(!err || errno != EINVAL, "bap_create_map_xattr(bad_xattr)",
	      "err:%d errno:%d\n", err, errno);

	xattr.btf_fd = -1;
	map_opts.btf_fd = -1;
	close(btf_fd);
	close(map_fd);
	close(sk_fd);
+19 −17
Original line number Diff line number Diff line
@@ -7,32 +7,33 @@

static void test_fail_cases(void)
{
	LIBBPF_OPTS(bpf_map_create_opts, opts);
	__u32 value;
	int fd, err;

	/* Invalid key size */
	fd = bpf_create_map(BPF_MAP_TYPE_BLOOM_FILTER, 4, sizeof(value), 100, 0);
	if (!ASSERT_LT(fd, 0, "bpf_create_map bloom filter invalid key size"))
	fd = bpf_map_create(BPF_MAP_TYPE_BLOOM_FILTER, NULL, 4, sizeof(value), 100, NULL);
	if (!ASSERT_LT(fd, 0, "bpf_map_create bloom filter invalid key size"))
		close(fd);

	/* Invalid value size */
	fd = bpf_create_map(BPF_MAP_TYPE_BLOOM_FILTER, 0, 0, 100, 0);
	if (!ASSERT_LT(fd, 0, "bpf_create_map bloom filter invalid value size 0"))
	fd = bpf_map_create(BPF_MAP_TYPE_BLOOM_FILTER, NULL, 0, 0, 100, NULL);
	if (!ASSERT_LT(fd, 0, "bpf_map_create bloom filter invalid value size 0"))
		close(fd);

	/* Invalid max entries size */
	fd = bpf_create_map(BPF_MAP_TYPE_BLOOM_FILTER, 0, sizeof(value), 0, 0);
	if (!ASSERT_LT(fd, 0, "bpf_create_map bloom filter invalid max entries size"))
	fd = bpf_map_create(BPF_MAP_TYPE_BLOOM_FILTER, NULL, 0, sizeof(value), 0, NULL);
	if (!ASSERT_LT(fd, 0, "bpf_map_create bloom filter invalid max entries size"))
		close(fd);

	/* Bloom filter maps do not support BPF_F_NO_PREALLOC */
	fd = bpf_create_map(BPF_MAP_TYPE_BLOOM_FILTER, 0, sizeof(value), 100,
			    BPF_F_NO_PREALLOC);
	if (!ASSERT_LT(fd, 0, "bpf_create_map bloom filter invalid flags"))
	opts.map_flags = BPF_F_NO_PREALLOC;
	fd = bpf_map_create(BPF_MAP_TYPE_BLOOM_FILTER, NULL, 0, sizeof(value), 100, &opts);
	if (!ASSERT_LT(fd, 0, "bpf_map_create bloom filter invalid flags"))
		close(fd);

	fd = bpf_create_map(BPF_MAP_TYPE_BLOOM_FILTER, 0, sizeof(value), 100, 0);
	if (!ASSERT_GE(fd, 0, "bpf_create_map bloom filter"))
	fd = bpf_map_create(BPF_MAP_TYPE_BLOOM_FILTER, NULL, 0, sizeof(value), 100, NULL);
	if (!ASSERT_GE(fd, 0, "bpf_map_create bloom filter"))
		return;

	/* Test invalid flags */
@@ -56,13 +57,14 @@ static void test_fail_cases(void)

static void test_success_cases(void)
{
	LIBBPF_OPTS(bpf_map_create_opts, opts);
	char value[11];
	int fd, err;

	/* Create a map */
	fd = bpf_create_map(BPF_MAP_TYPE_BLOOM_FILTER, 0, sizeof(value), 100,
			    BPF_F_ZERO_SEED | BPF_F_NUMA_NODE);
	if (!ASSERT_GE(fd, 0, "bpf_create_map bloom filter success case"))
	opts.map_flags = BPF_F_ZERO_SEED | BPF_F_NUMA_NODE;
	fd = bpf_map_create(BPF_MAP_TYPE_BLOOM_FILTER, NULL, 0, sizeof(value), 100, &opts);
	if (!ASSERT_GE(fd, 0, "bpf_map_create bloom filter success case"))
		return;

	/* Add a value to the bloom filter */
@@ -100,9 +102,9 @@ static void test_inner_map(struct bloom_filter_map *skel, const __u32 *rand_vals
	struct bpf_link *link;

	/* Create a bloom filter map that will be used as the inner map */
	inner_map_fd = bpf_create_map(BPF_MAP_TYPE_BLOOM_FILTER, 0, sizeof(*rand_vals),
				      nr_rand_vals, 0);
	if (!ASSERT_GE(inner_map_fd, 0, "bpf_create_map bloom filter inner map"))
	inner_map_fd = bpf_map_create(BPF_MAP_TYPE_BLOOM_FILTER, NULL, 0, sizeof(*rand_vals),
				      nr_rand_vals, NULL);
	if (!ASSERT_GE(inner_map_fd, 0, "bpf_map_create bloom filter inner map"))
		return;

	for (i = 0; i < nr_rand_vals; i++) {
Loading