Extend the existing selftests for maps with checks that the number of map entries returned by bpf_map_get_num_entries is as expected. Co-developed-by: Nick Zavaritsky <mejedi@xxxxxxxxx> Signed-off-by: Nick Zavaritsky <mejedi@xxxxxxxxx> Signed-off-by: Charalampos Stylianopoulos <charalampos.stylianopoulos@xxxxxxxxx> --- .../bpf/map_tests/lpm_trie_map_basic_ops.c | 5 +++ tools/testing/selftests/bpf/test_maps.c | 35 +++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/tools/testing/selftests/bpf/map_tests/lpm_trie_map_basic_ops.c b/tools/testing/selftests/bpf/map_tests/lpm_trie_map_basic_ops.c index d32e4edac930..40265b497791 100644 --- a/tools/testing/selftests/bpf/map_tests/lpm_trie_map_basic_ops.c +++ b/tools/testing/selftests/bpf/map_tests/lpm_trie_map_basic_ops.c @@ -434,6 +434,11 @@ static void test_lpm_ipaddr(void) inet_pton(AF_INET6, "2a00:ffff::", key_ipv6->data); assert(bpf_map_lookup_elem(map_fd_ipv6, key_ipv6, &value) == -ENOENT); + unsigned int entries; + /* Check that the reported number of entries in the map is as expected. */ + assert(bpf_map_get_num_entries(map_fd_ipv4, &entries) == 0 && entries == 5); + assert(bpf_map_get_num_entries(map_fd_ipv6, &entries) == 0 && entries == 1); + close(map_fd_ipv4); close(map_fd_ipv6); } diff --git a/tools/testing/selftests/bpf/test_maps.c b/tools/testing/selftests/bpf/test_maps.c index 8b40e9496af1..c61cf740a6b6 100644 --- a/tools/testing/selftests/bpf/test_maps.c +++ b/tools/testing/selftests/bpf/test_maps.c @@ -109,6 +109,10 @@ static void test_hashmap(unsigned int task, void *data) assert(bpf_map_get_next_key(fd, &next_key, &next_key) < 0 && errno == ENOENT); + unsigned int entries; + /* Check that the number of entries in the map is 2. */ + assert(bpf_map_get_num_entries(fd, &entries) == 0 && entries == 2); + /* Delete both elements. */ key = 1; assert(bpf_map_delete_elem(fd, &key) == 0); @@ -122,6 +126,7 @@ static void test_hashmap(unsigned int task, void *data) errno == ENOENT); assert(bpf_map_get_next_key(fd, &key, &next_key) < 0 && errno == ENOENT); + assert(bpf_map_get_num_entries(fd, &entries) == 0 && entries == 0); close(fd); } @@ -243,6 +248,11 @@ static void test_hashmap_percpu(unsigned int task, void *data) key = 1; assert(bpf_map_update_elem(fd, &key, value, BPF_EXIST) == 0); + + unsigned int entries; + /* Check that the number of entries in the map is 2. */ + assert(bpf_map_get_num_entries(fd, &entries) == 0 && entries == 2); + /* Delete both elements. */ key = 1; assert(bpf_map_delete_elem(fd, &key) == 0); @@ -256,6 +266,7 @@ static void test_hashmap_percpu(unsigned int task, void *data) errno == ENOENT); assert(bpf_map_get_next_key(fd, &key, &next_key) < 0 && errno == ENOENT); + assert(bpf_map_get_num_entries(fd, &entries) == 0 && entries == 0); close(fd); } @@ -529,6 +540,10 @@ static void test_devmap_hash(unsigned int task, void *data) exit(1); } + unsigned int entries; + /* Check that the number of entries in the map is 0. */ + assert(bpf_map_get_num_entries(fd, &entries) == 0 && entries == 0); + close(fd); } @@ -557,10 +572,17 @@ static void test_queuemap(unsigned int task, void *data) exit(1); } + unsigned int entries; + /* Check that the number of entries in the map is 0. */ + assert(bpf_map_get_num_entries(fd, &entries) == 0 && entries == 0); + /* Push MAP_SIZE elements */ for (i = 0; i < MAP_SIZE; i++) assert(bpf_map_update_elem(fd, NULL, &vals[i], 0) == 0); + /* Check that the number of entries in the map is MAP_SIZE. */ + assert(bpf_map_get_num_entries(fd, &entries) == 0 && entries == MAP_SIZE); + /* Check that element cannot be pushed due to max_entries limit */ assert(bpf_map_update_elem(fd, NULL, &val, 0) < 0 && errno == E2BIG); @@ -581,6 +603,9 @@ static void test_queuemap(unsigned int task, void *data) assert(bpf_map_lookup_and_delete_elem(fd, NULL, &val) < 0 && errno == ENOENT); + /* Check that the number of entries in the map is 0. */ + assert(bpf_map_get_num_entries(fd, &entries) == 0 && entries == 0); + /* Check that non supported functions set errno to EINVAL */ assert(bpf_map_delete_elem(fd, NULL) < 0 && errno == EINVAL); assert(bpf_map_get_next_key(fd, NULL, NULL) < 0 && errno == EINVAL); @@ -613,10 +638,17 @@ static void test_stackmap(unsigned int task, void *data) exit(1); } + unsigned int entries; + /* Check that the number of entries in the map is 0. */ + assert(bpf_map_get_num_entries(fd, &entries) == 0 && entries == 0); + /* Push MAP_SIZE elements */ for (i = 0; i < MAP_SIZE; i++) assert(bpf_map_update_elem(fd, NULL, &vals[i], 0) == 0); + /* Check that the number of entries in the map is MAP_SIZE. */ + assert(bpf_map_get_num_entries(fd, &entries) == 0 && entries == MAP_SIZE); + /* Check that element cannot be pushed due to max_entries limit */ assert(bpf_map_update_elem(fd, NULL, &val, 0) < 0 && errno == E2BIG); @@ -637,6 +669,9 @@ static void test_stackmap(unsigned int task, void *data) assert(bpf_map_lookup_and_delete_elem(fd, NULL, &val) < 0 && errno == ENOENT); + /* Check that the number of entries in the map is 0. */ + assert(bpf_map_get_num_entries(fd, &entries) == 0 && entries == 0); + /* Check that non supported functions set errno to EINVAL */ assert(bpf_map_delete_elem(fd, NULL) < 0 && errno == EINVAL); assert(bpf_map_get_next_key(fd, NULL, NULL) < 0 && errno == EINVAL); -- 2.43.0