Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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 <daniel@iogearbox.net>
Acked-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: David S. Miller <davem@davemloft.net>

authored by

Daniel Borkmann and committed by
David S. Miller
5ecf51fd 36e24c00

+50
+50
tools/testing/selftests/bpf/test_maps.c
··· 239 239 close(fd); 240 240 } 241 241 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 + 242 290 static void test_arraymap(int task, void *data) 243 291 { 244 292 int key, next_key, fd; ··· 512 464 run_parallel(100, test_hashmap, NULL); 513 465 run_parallel(100, test_hashmap_percpu, NULL); 514 466 run_parallel(100, test_hashmap_sizes, NULL); 467 + run_parallel(100, test_hashmap_walk, NULL); 515 468 516 469 run_parallel(100, test_arraymap, NULL); 517 470 run_parallel(100, test_arraymap_percpu, NULL); ··· 598 549 { 599 550 test_hashmap(0, NULL); 600 551 test_hashmap_percpu(0, NULL); 552 + test_hashmap_walk(0, NULL); 601 553 602 554 test_arraymap(0, NULL); 603 555 test_arraymap_percpu(0, NULL);