From: Cong Wang <xiyou.wangcong@gmail.com>
To: netdev@vger.kernel.org
Cc: bpf@vger.kernel.org, Cong Wang <cong.wang@bytedance.com>,
Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Dongdong Wang <wangdongdong.6@bytedance.com>
Subject: [Patch bpf-next v2 4/5] selftests/bpf: add a test case for bpf timeout map
Date: Mon, 14 Dec 2020 12:11:17 -0800 [thread overview]
Message-ID: <20201214201118.148126-5-xiyou.wangcong@gmail.com> (raw)
In-Reply-To: <20201214201118.148126-1-xiyou.wangcong@gmail.com>
From: Cong Wang <cong.wang@bytedance.com>
Add a test case in test_maps.c for timeout map, which focuses
on testing timeout.
Cc: Alexei Starovoitov <ast@kernel.org>
Cc: Daniel Borkmann <daniel@iogearbox.net>
Cc: Dongdong Wang <wangdongdong.6@bytedance.com>
Signed-off-by: Cong Wang <cong.wang@bytedance.com>
---
tools/testing/selftests/bpf/test_maps.c | 41 +++++++++++++++++++++++++
1 file changed, 41 insertions(+)
diff --git a/tools/testing/selftests/bpf/test_maps.c b/tools/testing/selftests/bpf/test_maps.c
index 0ad3e6305ff0..9fc7b5424fdf 100644
--- a/tools/testing/selftests/bpf/test_maps.c
+++ b/tools/testing/selftests/bpf/test_maps.c
@@ -1723,6 +1723,45 @@ static void test_reuseport_array(void)
close(map_fd);
}
+static void test_timeout_map(void)
+{
+ 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)1<<32) == 0);
+ /* Timeout after 2 secs */
+ assert(bpf_map_update_elem(fd, &key2, &val2, (u64)2<<32) == 0);
+ /* Timeout after 10 secs */
+ assert(bpf_map_update_elem(fd, &key3, &val3, (u64)10<<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)1<<32) == 0);
+ sleep(1);
+ assert(bpf_map_lookup_elem(fd, &key3, &val3) != 0);
+
+ close(fd);
+}
+
static void run_all_tests(void)
{
test_hashmap(0, NULL);
@@ -1752,6 +1791,8 @@ static void run_all_tests(void)
test_stackmap(0, NULL);
test_map_in_map();
+
+ test_timeout_map();
}
#define DEFINE_TEST(name) extern void test_##name(void);
--
2.25.1
next prev parent reply other threads:[~2020-12-14 20:15 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-12-14 20:11 [Patch bpf-next v2 0/5] bpf: introduce timeout map Cong Wang
2020-12-14 20:11 ` [Patch bpf-next v2 1/5] bpf: use index instead of hash for map_locked[] Cong Wang
2020-12-14 20:11 ` [Patch bpf-next v2 2/5] bpf: introduce timeout map Cong Wang
2020-12-15 19:27 ` Andrii Nakryiko
2020-12-15 20:06 ` Cong Wang
2020-12-15 22:03 ` Andrii Nakryiko
2020-12-15 23:23 ` Daniel Borkmann
2020-12-16 0:22 ` Cong Wang
2020-12-16 1:14 ` Alexei Starovoitov
2020-12-16 2:10 ` Cong Wang
2020-12-16 2:35 ` Alexei Starovoitov
2020-12-16 10:38 ` David Laight
2020-12-17 5:06 ` Cong Wang
2020-12-17 22:39 ` Daniel Borkmann
2020-12-16 0:15 ` Cong Wang
2020-12-16 18:35 ` Andrii Nakryiko
2020-12-17 6:29 ` Cong Wang
2020-12-17 21:14 ` Cong Wang
2020-12-18 19:14 ` Andrii Nakryiko
2020-12-18 19:13 ` Andrii Nakryiko
2020-12-14 20:11 ` [Patch bpf-next v2 3/5] selftests/bpf: update elem_size check in map ptr test Cong Wang
2020-12-14 20:40 ` Andrey Ignatov
2020-12-14 20:11 ` Cong Wang [this message]
2020-12-14 20:11 ` [Patch bpf-next v2 5/5] selftests/bpf: add timeout map check in map_ptr tests Cong Wang
2020-12-14 20:41 ` Andrey Ignatov
2020-12-15 19:28 ` [Patch bpf-next v2 0/5] bpf: introduce timeout map Andrii Nakryiko
2020-12-15 20:12 ` Cong Wang
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20201214201118.148126-5-xiyou.wangcong@gmail.com \
--to=xiyou.wangcong@gmail.com \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=cong.wang@bytedance.com \
--cc=daniel@iogearbox.net \
--cc=netdev@vger.kernel.org \
--cc=wangdongdong.6@bytedance.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).