From: Cong Wang <cong.w...@bytedance.com>

Add some test cases in test_maps.c for timeout map, which focus
on testing timeout.

Cc: Alexei Starovoitov <a...@kernel.org>
Cc: Daniel Borkmann <dan...@iogearbox.net>
Cc: Dongdong Wang <wangdongdon...@bytedance.com>
Signed-off-by: Cong Wang <cong.w...@bytedance.com>
---
 tools/testing/selftests/bpf/test_maps.c | 46 +++++++++++++++++++++++++
 1 file changed, 46 insertions(+)

diff --git a/tools/testing/selftests/bpf/test_maps.c 
b/tools/testing/selftests/bpf/test_maps.c
index 0ad3e6305ff0..19c7c0f64a5d 100644
--- a/tools/testing/selftests/bpf/test_maps.c
+++ b/tools/testing/selftests/bpf/test_maps.c
@@ -639,6 +639,49 @@ static void test_stackmap(unsigned int task, void *data)
        close(fd);
 }
 
+static void test_timeout_map(unsigned int task, void *data)
+{
+       int val1 = 1, val2 = 2, val3 = 3;
+       int key1 = 1, key2 = 2, key3 = 3;
+       int fd;
+
+       fd = bpf_create_map(BPF_MAP_TYPE_TIMEOUT_HASH, sizeof(int), sizeof(int),
+                           3, map_flags);
+       if (fd < 0) {
+               printf("Failed to create timeout map '%s'!\n", strerror(errno));
+               exit(1);
+       }
+
+       /* Timeout after 1 secs */
+       assert(bpf_map_update_elem(fd, &key1, &val1, (u64)1000<<32) == 0);
+       /* Timeout after 2 secs */
+       assert(bpf_map_update_elem(fd, &key2, &val2, (u64)2000<<32) == 0);
+       /* Timeout after 10 secs */
+       assert(bpf_map_update_elem(fd, &key3, &val3, (u64)10000<<32) == 0);
+
+       sleep(1);
+       assert(bpf_map_lookup_elem(fd, &key1, &val1) != 0);
+       val2 = 0;
+       assert(bpf_map_lookup_elem(fd, &key2, &val2) == 0 && val2 == 2);
+
+       sleep(1);
+       assert(bpf_map_lookup_elem(fd, &key1, &val1) != 0);
+       assert(bpf_map_lookup_elem(fd, &key2, &val2) != 0);
+
+       /* Modify timeout to expire it earlier */
+       val3 = 0;
+       assert(bpf_map_lookup_elem(fd, &key3, &val3) == 0 && val3 == 3);
+       assert(bpf_map_update_elem(fd, &key3, &val3, (u64)1000<<32) == 0);
+       sleep(1);
+       assert(bpf_map_lookup_elem(fd, &key3, &val3) != 0);
+
+       /* Add one elem expired immediately and try to delete this expired */
+       assert(bpf_map_update_elem(fd, &key3, &val3, 0) == 0);
+       assert(bpf_map_delete_elem(fd, &key3) == -1 && errno == ENOENT);
+
+       close(fd);
+}
+
 #include <sys/ioctl.h>
 #include <arpa/inet.h>
 #include <sys/select.h>
@@ -1305,6 +1348,7 @@ static void test_map_stress(void)
 
        run_parallel(100, test_arraymap, NULL);
        run_parallel(100, test_arraymap_percpu, NULL);
+       run_parallel(100, test_timeout_map, NULL);
 }
 
 #define TASKS 1024
@@ -1752,6 +1796,8 @@ static void run_all_tests(void)
        test_stackmap(0, NULL);
 
        test_map_in_map();
+
+       test_timeout_map(0, NULL);
 }
 
 #define DEFINE_TEST(name) extern void test_##name(void);
-- 
2.25.1

Reply via email to