linux-api.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Lorenz Bauer <lmb@cloudflare.com>
To: ast@kernel.org, daniel@iogearbox.net
Cc: netdev@vger.kernel.org, linux-api@vger.kernel.org,
	songliubraving@fb.com, Lorenz Bauer <lmb@cloudflare.com>
Subject: [PATCH v3 4/4] tools: add selftest for BPF_F_ZERO_SEED
Date: Fri, 16 Nov 2018 11:41:11 +0000	[thread overview]
Message-ID: <20181116114111.31177-5-lmb@cloudflare.com> (raw)
In-Reply-To: <20181116114111.31177-1-lmb@cloudflare.com>

Check that iterating two separate hash maps produces the same
order of keys if BPF_F_ZERO_SEED is used.

Signed-off-by: Lorenz Bauer <lmb@cloudflare.com>
---
 tools/testing/selftests/bpf/test_maps.c | 68 +++++++++++++++++++++----
 1 file changed, 57 insertions(+), 11 deletions(-)

diff --git a/tools/testing/selftests/bpf/test_maps.c b/tools/testing/selftests/bpf/test_maps.c
index 4db2116e52be..9f0a5b16a246 100644
--- a/tools/testing/selftests/bpf/test_maps.c
+++ b/tools/testing/selftests/bpf/test_maps.c
@@ -258,23 +258,35 @@ static void test_hashmap_percpu(int task, void *data)
 	close(fd);
 }
 
+static int helper_fill_hashmap(int max_entries)
+{
+	int i, fd, ret;
+	long long key, value;
+
+	fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value),
+			    max_entries, map_flags);
+	CHECK(fd < 0,
+	      "failed to create hashmap",
+	      "err: %s, flags: 0x%x\n", strerror(errno), map_flags);
+
+	for (i = 0; i < max_entries; i++) {
+		key = i; value = key;
+		ret = bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST);
+		CHECK(ret != 0,
+		      "can't update hashmap",
+		      "err: %s\n", strerror(ret));
+	}
+
+	return fd;
+}
+
 static void test_hashmap_walk(int task, void *data)
 {
 	int fd, i, max_entries = 1000;
 	long long key, value, next_key;
 	bool next_key_valid = true;
 
-	fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value),
-			    max_entries, map_flags);
-	if (fd < 0) {
-		printf("Failed to create hashmap '%s'!\n", strerror(errno));
-		exit(1);
-	}
-
-	for (i = 0; i < max_entries; i++) {
-		key = i; value = key;
-		assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == 0);
-	}
+	fd = helper_fill_hashmap(max_entries);
 
 	for (i = 0; bpf_map_get_next_key(fd, !i ? NULL : &key,
 					 &next_key) == 0; i++) {
@@ -306,6 +318,39 @@ static void test_hashmap_walk(int task, void *data)
 	close(fd);
 }
 
+static void test_hashmap_zero_seed(void)
+{
+	int i, first, second, old_flags;
+	long long key, next_first, next_second;
+
+	old_flags = map_flags;
+	map_flags |= BPF_F_ZERO_SEED;
+
+	first = helper_fill_hashmap(3);
+	second = helper_fill_hashmap(3);
+
+	for (i = 0; ; i++) {
+		void *key_ptr = !i ? NULL : &key;
+
+		if (bpf_map_get_next_key(first, key_ptr, &next_first) != 0)
+			break;
+
+		CHECK(bpf_map_get_next_key(second, key_ptr, &next_second) != 0,
+		      "next_key for second map must succeed",
+		      "key_ptr: %p", key_ptr);
+		CHECK(next_first != next_second,
+		      "keys must match",
+		      "i: %d first: %lld second: %lld\n", i,
+		      next_first, next_second);
+
+		key = next_first;
+	}
+
+	map_flags = old_flags;
+	close(first);
+	close(second);
+}
+
 static void test_arraymap(int task, void *data)
 {
 	int key, next_key, fd;
@@ -1534,6 +1579,7 @@ static void run_all_tests(void)
 	test_hashmap(0, NULL);
 	test_hashmap_percpu(0, NULL);
 	test_hashmap_walk(0, NULL);
+	test_hashmap_zero_seed();
 
 	test_arraymap(0, NULL);
 	test_arraymap_percpu(0, NULL);
-- 
2.17.1

  parent reply	other threads:[~2018-11-16 11:41 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-10-01 10:45 [PATCH 0/3] bpf: allow zero-initialising hash map seed Lorenz Bauer
2018-10-01 10:45 ` [PATCH 1/3] bpf: allow zero-initializing " Lorenz Bauer
2018-10-02 19:59   ` Jann Horn
2018-10-05  7:42     ` Lorenz Bauer
2018-10-05 14:12       ` Jann Horn
2018-10-05 14:21         ` Lorenz Bauer
2018-10-05 14:27           ` Jann Horn
2018-10-05 21:07             ` Alexei Starovoitov
2018-10-08  9:48               ` Lorenz Bauer
2018-10-01 10:45 ` [PATCH 2/3] tools: sync linux/bpf.h Lorenz Bauer
2018-10-01 10:45 ` [PATCH 3/3] tools: add selftest for BPF_F_ZERO_SEED Lorenz Bauer
2018-10-01 19:12 ` [PATCH 0/3] bpf: allow zero-initialising hash map seed Daniel Borkmann
2018-10-05 14:27   ` Lorenz Bauer
2018-10-05 14:29     ` Jann Horn
2018-10-08 10:32 ` [PATCH v2 " Lorenz Bauer
2018-10-08 10:32   ` [PATCH v2 1/3] bpf: allow zero-initializing " Lorenz Bauer
2018-10-08 23:07     ` Song Liu
2018-10-25 15:12       ` Lorenz Bauer
2018-11-07  0:39         ` Song Liu
2018-10-08 10:32   ` [PATCH v2 2/3] tools: sync linux/bpf.h Lorenz Bauer
2018-10-08 23:12     ` Song Liu
2018-10-25 15:07       ` Lorenz Bauer
2018-10-08 10:32   ` [PATCH v2 3/3] tools: add selftest for BPF_F_ZERO_SEED Lorenz Bauer
2018-10-08 23:15     ` Song Liu
2018-11-16 11:41 ` [PATCH v3 0/4] bpf: allow zero-initialising hash map seed Lorenz Bauer
2018-11-16 11:41   ` [PATCH v3 1/4] bpf: allow zero-initializing " Lorenz Bauer
2018-11-16 11:41   ` [PATCH v3 2/4] bpf: move BPF_F_QUERY_EFFECTIVE after map flags Lorenz Bauer
2018-11-16 11:41   ` [PATCH v3 3/4] tools: sync linux/bpf.h Lorenz Bauer
2018-11-16 11:41   ` Lorenz Bauer [this message]
2018-11-16 17:33   ` [PATCH v3 0/4] bpf: allow zero-initialising hash map seed Song Liu
2018-11-16 17:34   ` Song Liu
2018-11-19 23:56   ` Daniel Borkmann

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=20181116114111.31177-5-lmb@cloudflare.com \
    --to=lmb@cloudflare.com \
    --cc=ast@kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=linux-api@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=songliubraving@fb.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).