Skip to content

Commit

Permalink
selftests/bpf: Improve test coverage of test_maps
Browse files Browse the repository at this point in the history
Make test_maps more stressful with more parallelism in
update/delete/lookup/walk including different value sizes.

Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
Acked-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20220902211058.60789-4-alexei.starovoitov@gmail.com
  • Loading branch information
Alexei Starovoitov authored and Daniel Borkmann committed Sep 5, 2022
1 parent fba1a1c commit 37521bf
Showing 1 changed file with 24 additions and 14 deletions.
38 changes: 24 additions & 14 deletions tools/testing/selftests/bpf/test_maps.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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));
Expand All @@ -288,26 +289,26 @@ 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);

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);

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;
}

Expand All @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;

Expand All @@ -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),
Expand All @@ -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, ...
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 37521bf

Please sign in to comment.