Commit 37521bff authored by Alexei Starovoitov's avatar Alexei Starovoitov Committed by Daniel Borkmann
Browse files

selftests/bpf: Improve test coverage of test_maps



Make test_maps more stressful with more parallelism in
update/delete/lookup/walk including different value sizes.

Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
Acked-by: default avatarKumar Kartikeya Dwivedi <memxor@gmail.com>
Acked-by: default avatarAndrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20220902211058.60789-4-alexei.starovoitov@gmail.com
parent fba1a1c6
Loading
Loading
Loading
Loading
+24 −14
Original line number Diff line number Diff line
@@ -264,10 +264,11 @@ static void test_hashmap_percpu(unsigned int task, void *data)
	close(fd);
}

#define VALUE_SIZE 3
static int helper_fill_hashmap(int max_entries)
{
	int i, fd, ret;
	long long key, value;
	long long key, value[VALUE_SIZE] = {};

	fd = bpf_map_create(BPF_MAP_TYPE_HASH, NULL, sizeof(key), sizeof(value),
			    max_entries, &map_opts);
@@ -276,8 +277,8 @@ static int helper_fill_hashmap(int max_entries)
	      "err: %s, flags: 0x%x\n", strerror(errno), map_opts.map_flags);

	for (i = 0; i < max_entries; i++) {
		key = i; value = key;
		ret = bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST);
		key = i; value[0] = key;
		ret = bpf_map_update_elem(fd, &key, value, BPF_NOEXIST);
		CHECK(ret != 0,
		      "can't update hashmap",
		      "err: %s\n", strerror(ret));
@@ -288,8 +289,8 @@ static int helper_fill_hashmap(int max_entries)

static void test_hashmap_walk(unsigned int task, void *data)
{
	int fd, i, max_entries = 1000;
	long long key, value, next_key;
	int fd, i, max_entries = 10000;
	long long key, value[VALUE_SIZE], next_key;
	bool next_key_valid = true;

	fd = helper_fill_hashmap(max_entries);
@@ -297,7 +298,7 @@ static void test_hashmap_walk(unsigned int task, void *data)
	for (i = 0; bpf_map_get_next_key(fd, !i ? NULL : &key,
					 &next_key) == 0; i++) {
		key = next_key;
		assert(bpf_map_lookup_elem(fd, &key, &value) == 0);
		assert(bpf_map_lookup_elem(fd, &key, value) == 0);
	}

	assert(i == max_entries);
@@ -305,9 +306,9 @@ static void test_hashmap_walk(unsigned int task, void *data)
	assert(bpf_map_get_next_key(fd, NULL, &key) == 0);
	for (i = 0; next_key_valid; i++) {
		next_key_valid = bpf_map_get_next_key(fd, &key, &next_key) == 0;
		assert(bpf_map_lookup_elem(fd, &key, &value) == 0);
		value++;
		assert(bpf_map_update_elem(fd, &key, &value, BPF_EXIST) == 0);
		assert(bpf_map_lookup_elem(fd, &key, value) == 0);
		value[0]++;
		assert(bpf_map_update_elem(fd, &key, value, BPF_EXIST) == 0);
		key = next_key;
	}

@@ -316,8 +317,8 @@ static void test_hashmap_walk(unsigned int task, void *data)
	for (i = 0; bpf_map_get_next_key(fd, !i ? NULL : &key,
					 &next_key) == 0; i++) {
		key = next_key;
		assert(bpf_map_lookup_elem(fd, &key, &value) == 0);
		assert(value - 1 == key);
		assert(bpf_map_lookup_elem(fd, &key, value) == 0);
		assert(value[0] - 1 == key);
	}

	assert(i == max_entries);
@@ -1371,16 +1372,16 @@ static void __run_parallel(unsigned int tasks,

static void test_map_stress(void)
{
	run_parallel(100, test_hashmap_walk, NULL);
	run_parallel(100, test_hashmap, NULL);
	run_parallel(100, test_hashmap_percpu, NULL);
	run_parallel(100, test_hashmap_sizes, NULL);
	run_parallel(100, test_hashmap_walk, NULL);

	run_parallel(100, test_arraymap, NULL);
	run_parallel(100, test_arraymap_percpu, NULL);
}

#define TASKS 1024
#define TASKS 100

#define DO_UPDATE 1
#define DO_DELETE 0
@@ -1432,6 +1433,8 @@ static void test_update_delete(unsigned int fn, void *data)
	int fd = ((int *)data)[0];
	int i, key, value, err;

	if (fn & 1)
		test_hashmap_walk(fn, NULL);
	for (i = fn; i < MAP_SIZE; i += TASKS) {
		key = value = i;

@@ -1455,7 +1458,7 @@ static void test_update_delete(unsigned int fn, void *data)

static void test_map_parallel(void)
{
	int i, fd, key = 0, value = 0;
	int i, fd, key = 0, value = 0, j = 0;
	int data[2];

	fd = bpf_map_create(BPF_MAP_TYPE_HASH, NULL, sizeof(key), sizeof(value),
@@ -1466,6 +1469,7 @@ static void test_map_parallel(void)
		exit(1);
	}

again:
	/* Use the same fd in children to add elements to this map:
	 * child_0 adds key=0, key=1024, key=2048, ...
	 * child_1 adds key=1, key=1025, key=2049, ...
@@ -1502,6 +1506,12 @@ static void test_map_parallel(void)
	key = -1;
	assert(bpf_map_get_next_key(fd, NULL, &key) < 0 && errno == ENOENT);
	assert(bpf_map_get_next_key(fd, &key, &key) < 0 && errno == ENOENT);

	key = 0;
	bpf_map_delete_elem(fd, &key);
	if (j++ < 5)
		goto again;
	close(fd);
}

static void test_map_rdonly(void)