Skip to content

Commit 5ecf51f

Browse files
borkmanndavem330
authored andcommitted
bpf, tests: add a test for htab lookup + update traversal
Add a test case to track behaviour when traversing and updating the htab map. We recently used such traversal, so it's quite useful to keep it as an example in selftests. Signed-off-by: Daniel Borkmann <[email protected]> Acked-by: Alexei Starovoitov <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 36e24c0 commit 5ecf51f

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

tools/testing/selftests/bpf/test_maps.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,54 @@ static void test_hashmap_percpu(int task, void *data)
239239
close(fd);
240240
}
241241

242+
static void test_hashmap_walk(int task, void *data)
243+
{
244+
int fd, i, max_entries = 100000;
245+
long long key, value, next_key;
246+
bool next_key_valid = true;
247+
248+
fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value),
249+
max_entries, map_flags);
250+
if (fd < 0) {
251+
printf("Failed to create hashmap '%s'!\n", strerror(errno));
252+
exit(1);
253+
}
254+
255+
for (i = 0; i < max_entries; i++) {
256+
key = i; value = key;
257+
assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == 0);
258+
}
259+
260+
for (i = 0; bpf_map_get_next_key(fd, !i ? NULL : &key,
261+
&next_key) == 0; i++) {
262+
key = next_key;
263+
assert(bpf_map_lookup_elem(fd, &key, &value) == 0);
264+
}
265+
266+
assert(i == max_entries);
267+
268+
assert(bpf_map_get_next_key(fd, NULL, &key) == 0);
269+
for (i = 0; next_key_valid; i++) {
270+
next_key_valid = bpf_map_get_next_key(fd, &key, &next_key) == 0;
271+
assert(bpf_map_lookup_elem(fd, &key, &value) == 0);
272+
value++;
273+
assert(bpf_map_update_elem(fd, &key, &value, BPF_EXIST) == 0);
274+
key = next_key;
275+
}
276+
277+
assert(i == max_entries);
278+
279+
for (i = 0; bpf_map_get_next_key(fd, !i ? NULL : &key,
280+
&next_key) == 0; i++) {
281+
key = next_key;
282+
assert(bpf_map_lookup_elem(fd, &key, &value) == 0);
283+
assert(value - 1 == key);
284+
}
285+
286+
assert(i == max_entries);
287+
close(fd);
288+
}
289+
242290
static void test_arraymap(int task, void *data)
243291
{
244292
int key, next_key, fd;
@@ -464,6 +512,7 @@ static void test_map_stress(void)
464512
run_parallel(100, test_hashmap, NULL);
465513
run_parallel(100, test_hashmap_percpu, NULL);
466514
run_parallel(100, test_hashmap_sizes, NULL);
515+
run_parallel(100, test_hashmap_walk, NULL);
467516

468517
run_parallel(100, test_arraymap, NULL);
469518
run_parallel(100, test_arraymap_percpu, NULL);
@@ -549,6 +598,7 @@ static void run_all_tests(void)
549598
{
550599
test_hashmap(0, NULL);
551600
test_hashmap_percpu(0, NULL);
601+
test_hashmap_walk(0, NULL);
552602

553603
test_arraymap(0, NULL);
554604
test_arraymap_percpu(0, NULL);

0 commit comments

Comments
 (0)