[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20190829064513.2751375-1-yhs@fb.com>
Date: Wed, 28 Aug 2019 23:45:13 -0700
From: Yonghong Song <yhs@...com>
To: <bpf@...r.kernel.org>, <netdev@...r.kernel.org>
CC: Alexei Starovoitov <ast@...com>,
Brian Vazquez <brianvv@...gle.com>,
Daniel Borkmann <daniel@...earbox.net>, <kernel-team@...com>,
Yonghong Song <yhs@...com>
Subject: [PATCH bpf-next 10/13] tools/bpf: add test for bpf_map_lookup_and_delete_batch()
Test bpf_map_lookup_and_delete_batch() functionality.
$ ./test_maps
...
test_map_lookup_and_delete_batch:PASS
...
Signed-off-by: Yonghong Song <yhs@...com>
---
.../map_tests/map_lookup_and_delete_batch.c | 164 ++++++++++++++++++
1 file changed, 164 insertions(+)
create mode 100644 tools/testing/selftests/bpf/map_tests/map_lookup_and_delete_batch.c
diff --git a/tools/testing/selftests/bpf/map_tests/map_lookup_and_delete_batch.c b/tools/testing/selftests/bpf/map_tests/map_lookup_and_delete_batch.c
new file mode 100644
index 000000000000..b151513a0ab2
--- /dev/null
+++ b/tools/testing/selftests/bpf/map_tests/map_lookup_and_delete_batch.c
@@ -0,0 +1,164 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2019 Facebook */
+#include <stdio.h>
+#include <errno.h>
+#include <string.h>
+
+#include <bpf/bpf.h>
+#include <bpf/libbpf.h>
+
+#include <test_maps.h>
+
+static void map_batch_update(int map_fd, __u32 max_entries, int *keys,
+ int *values)
+{
+ int i, err;
+
+ for (i = 0; i < max_entries; i++) {
+ keys[i] = i + 1;
+ values[i] = i + 2;
+ }
+
+ err = bpf_map_update_batch(map_fd, keys, values, &max_entries, 0, 0);
+ CHECK(err, "bpf_map_update_batch()", "error:%s\n", strerror(errno));
+}
+
+static void map_batch_verify(int *visited, __u32 max_entries,
+ int *keys, int *values)
+{
+ int i;
+
+ memset(visited, 0, max_entries * sizeof(*visited));
+ for (i = 0; i < max_entries; i++) {
+ CHECK(keys[i] + 1 != values[i], "key/value checking",
+ "error: i %d key %d value %d\n", i, keys[i], values[i]);
+ visited[i] = 1;
+ }
+ for (i = 0; i < max_entries; i++) {
+ CHECK(visited[i] != 1, "visited checking",
+ "error: keys array at index %d missing\n", i);
+ }
+}
+
+void test_map_lookup_and_delete_batch(void)
+{
+ struct bpf_create_map_attr xattr = {
+ .name = "hash_map",
+ .map_type = BPF_MAP_TYPE_HASH,
+ .key_size = sizeof(int),
+ .value_size = sizeof(int),
+ };
+ int map_fd, *keys, *values, *visited, key;
+ const __u32 max_entries = 10;
+ void *p_skey, *p_next_skey;
+ __u32 count, total;
+ int err, i, step;
+
+ xattr.max_entries = max_entries;
+ map_fd = bpf_create_map_xattr(&xattr);
+ CHECK(map_fd == -1,
+ "bpf_create_map_xattr()", "error:%s\n", strerror(errno));
+
+ keys = malloc(max_entries * sizeof(int));
+ values = malloc(max_entries * sizeof(int));
+ visited = malloc(max_entries * sizeof(int));
+ CHECK(!keys || !values || !visited, "malloc()", "error:%s\n", strerror(errno));
+
+ /* test 1: lookup/delete an empty hash table, success */
+ count = max_entries;
+ p_next_skey = &key;
+ err = bpf_map_lookup_and_delete_batch(map_fd, NULL, &p_next_skey, keys, values,
+ &count, 0, 0);
+ CHECK(err, "empty map", "error: %s\n", strerror(errno));
+ CHECK(p_next_skey || count, "empty map",
+ "p_next_skey = %p, count = %u\n", p_next_skey, count);
+
+ /* populate elements to the map */
+ map_batch_update(map_fd, max_entries, keys, values);
+
+ /* test 2: lookup/delete with count = 0, success */
+ count = 0;
+ err = bpf_map_lookup_and_delete_batch(map_fd, NULL, NULL, keys, values,
+ &count, 0, 0);
+ CHECK(err, "count = 0", "error: %s\n", strerror(errno));
+
+ /* test 3: lookup/delete with count = max_entries, skey && !nskey,
+ * failure.
+ */
+ count = max_entries;
+ key = 1;
+ err = bpf_map_lookup_and_delete_batch(map_fd, &key, NULL, keys, values,
+ &count, 0, 0);
+ CHECK(!err, "skey && !nskey", "unexpected success\n");
+
+ /* test 4: lookup/delete with count = max_entries, success */
+ memset(keys, 0, max_entries * sizeof(*keys));
+ memset(values, 0, max_entries * sizeof(*values));
+ count = max_entries;
+ p_next_skey = &key;
+ err = bpf_map_lookup_and_delete_batch(map_fd, NULL, &p_next_skey, keys,
+ values, &count, 0, 0);
+ CHECK(err, "count = max_entries", "error: %s\n", strerror(errno));
+ CHECK(count != max_entries || p_next_skey != NULL, "count = max_entries",
+ "count = %u, max_entries = %u, p_next_skey = %p\n",
+ count, max_entries, p_next_skey);
+ map_batch_verify(visited, max_entries, keys, values);
+
+ /* bpf_map_get_next_key() should return -ENOENT for an empty map. */
+ err = bpf_map_get_next_key(map_fd, NULL, &key);
+ CHECK(!err, "bpf_map_get_next_key()", "error: %s\n", strerror(errno));
+
+ /* test 5: lookup/delete in a loop with various steps. */
+ for (step = 1; step < max_entries; step++) {
+ map_batch_update(map_fd, max_entries, keys, values);
+ memset(keys, 0, max_entries * sizeof(*keys));
+ memset(values, 0, max_entries * sizeof(*values));
+ p_skey = NULL;
+ p_next_skey = &key;
+ total = 0;
+ i = 0;
+ /* iteratively lookup/delete elements with 'step' elements each */
+ count = step;
+ while (true) {
+ err = bpf_map_lookup_and_delete_batch(map_fd, p_skey,
+ &p_next_skey,
+ keys + i * step,
+ values + i * step,
+ &count, 0, 0);
+ CHECK(err, "lookup/delete with steps", "error: %s\n",
+ strerror(errno));
+
+ total += count;
+ if (!p_next_skey)
+ break;
+
+ CHECK(count != step, "lookup/delete with steps",
+ "i = %d, count = %u, step = %d\n",
+ i, count, step);
+
+ if (!p_skey)
+ p_skey = p_next_skey;
+ i++;
+ }
+
+ CHECK(total != max_entries, "lookup/delete with steps",
+ "total = %u, max_entries = %u\n", total, max_entries);
+
+ map_batch_verify(visited, max_entries, keys, values);
+ err = bpf_map_get_next_key(map_fd, NULL, &key);
+ CHECK(!err, "bpf_map_get_next_key()", "error: %s\n", strerror(errno));
+ }
+
+ /* test 6: lookup/delete with keys in keys[]. */
+ map_batch_update(map_fd, max_entries, keys, values);
+ memset(values, 0, max_entries * sizeof(*values));
+ count = max_entries;
+ err = bpf_map_lookup_and_delete_batch(map_fd, NULL, NULL, keys, values,
+ &count, 0, 0);
+ CHECK(err, "lookup/delete key in keys[]", "error: %s\n", strerror(errno));
+ map_batch_verify(visited, max_entries, keys, values);
+ err = bpf_map_get_next_key(map_fd, NULL, &key);
+ CHECK(!err, "bpf_map_get_next_key()", "error: %s\n", strerror(errno));
+
+ printf("%s:PASS\n", __func__);
+}
--
2.17.1
Powered by blists - more mailing lists