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 3/3] tools: add a test case for bpf timeout map
Date: Thu, 10 Dec 2020 16:06:49 -0800 [thread overview]
Message-ID: <20201211000649.236635-4-xiyou.wangcong@gmail.com> (raw)
In-Reply-To: <20201211000649.236635-1-xiyou.wangcong@gmail.com>
From: Cong Wang <cong.wang@bytedance.com>
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-11 0:08 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-12-11 0:06 [Patch bpf-next 0/3] bpf: introduce timeout map Cong Wang
2020-12-11 0:06 ` [Patch bpf-next 1/3] bpf: use index instead of hash for map_locked[] Cong Wang
2020-12-11 0:06 ` [Patch bpf-next 2/3] bpf: introduce timeout map Cong Wang
2020-12-11 0:06 ` Cong Wang [this message]
2020-12-11 19:55 ` [Patch bpf-next 0/3] " Andrii Nakryiko
2020-12-12 22:25 ` Cong Wang
2020-12-12 23:18 ` Cong Wang
2020-12-14 1:33 ` Andrey Ignatov
2020-12-14 18:40 ` 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=20201211000649.236635-4-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).