[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <d922ded45e0d3a76e918e1eed0426d3743d20f0d.1576673843.git.paul.chaignon@orange.com>
Date: Wed, 18 Dec 2019 15:23:33 +0100
From: Paul Chaignon <paul.chaignon@...nge.com>
To: bpf@...r.kernel.org
Cc: paul.chaignon@...il.com, netdev@...r.kernel.org,
Alexei Starovoitov <ast@...nel.org>,
Daniel Borkmann <daniel@...earbox.net>,
Martin KaFai Lau <kafai@...com>,
Song Liu <songliubraving@...com>, Yonghong Song <yhs@...com>,
Andrii Nakryiko <andriin@...com>
Subject: [PATCH bpf-next 2/3] selftests/bpf: Tests for single-cpu updates of
per-cpu maps
This patch adds test cases for single-cpu updates of per-cpu maps. It
updates values for the first and last CPUs and checks that only their
values were updated.
Signed-off-by: Paul Chaignon <paul.chaignon@...nge.com>
---
tools/testing/selftests/bpf/test_maps.c | 34 ++++++++++++++++++++++++-
1 file changed, 33 insertions(+), 1 deletion(-)
diff --git a/tools/testing/selftests/bpf/test_maps.c b/tools/testing/selftests/bpf/test_maps.c
index 02eae1e864c2..e3d3b15e1b91 100644
--- a/tools/testing/selftests/bpf/test_maps.c
+++ b/tools/testing/selftests/bpf/test_maps.c
@@ -150,6 +150,7 @@ static void test_hashmap_percpu(unsigned int task, void *data)
BPF_DECLARE_PERCPU(long, value);
long long key, next_key, first_key;
int expected_key_mask = 0;
+ long new_value = 42;
int fd, i;
fd = bpf_create_map(BPF_MAP_TYPE_PERCPU_HASH, sizeof(key),
@@ -232,6 +233,23 @@ static void test_hashmap_percpu(unsigned int task, void *data)
key = 1;
assert(bpf_map_update_elem(fd, &key, value, BPF_EXIST) == 0);
+ /* Update value for a single CPU. */
+ assert(bpf_map_update_elem(fd, &key, &new_value,
+ BPF_NOEXIST | BPF_CPU) == -1 &&
+ errno == EEXIST);
+ assert(bpf_map_update_elem(fd, &key, &new_value,
+ ((__u64)nr_cpus) << 32 | BPF_CPU) == -1 &&
+ errno == EINVAL);
+ assert(bpf_map_update_elem(fd, &key, &new_value,
+ BPF_EXIST | BPF_CPU) == 0);
+ assert(bpf_map_update_elem(fd, &key, &new_value,
+ (nr_cpus - 1ULL) << 32 | BPF_CPU) == 0);
+ assert(bpf_map_lookup_elem(fd, &key, value) == 0);
+ assert(bpf_percpu(value, 0) == new_value);
+ for (i = 1; i < nr_cpus - 1; i++)
+ assert(bpf_percpu(value, i) == i + 100);
+ assert(bpf_percpu(value, nr_cpus - 1) == new_value);
+
/* Delete both elements. */
key = 1;
assert(bpf_map_delete_elem(fd, &key) == 0);
@@ -402,6 +420,7 @@ static void test_arraymap_percpu(unsigned int task, void *data)
unsigned int nr_cpus = bpf_num_possible_cpus();
BPF_DECLARE_PERCPU(long, values);
int key, next_key, fd, i;
+ long new_value = 42;
fd = bpf_create_map(BPF_MAP_TYPE_PERCPU_ARRAY, sizeof(key),
sizeof(bpf_percpu(values, 0)), 2, 0);
@@ -449,8 +468,21 @@ static void test_arraymap_percpu(unsigned int task, void *data)
assert(bpf_map_get_next_key(fd, &next_key, &next_key) == -1 &&
errno == ENOENT);
- /* Delete shouldn't succeed. */
+ /* Update value for a single CPU */
key = 1;
+ assert(bpf_map_update_elem(fd, &key, &new_value,
+ ((__u64)nr_cpus) << 32 | BPF_CPU) == -1 &&
+ errno == EINVAL);
+ assert(bpf_map_update_elem(fd, &key, &new_value, BPF_CPU) == 0);
+ assert(bpf_map_update_elem(fd, &key, &new_value,
+ (nr_cpus - 1ULL) << 32 | BPF_CPU) == 0);
+ assert(bpf_map_lookup_elem(fd, &key, values) == 0);
+ assert(bpf_percpu(values, 0) == new_value);
+ for (i = 1; i < nr_cpus - 1; i++)
+ assert(bpf_percpu(values, i) == i + 100);
+ assert(bpf_percpu(values, nr_cpus - 1) == new_value);
+
+ /* Delete shouldn't succeed. */
assert(bpf_map_delete_elem(fd, &key) == -1 && errno == EINVAL);
close(fd);
--
2.24.0
Powered by blists - more mailing lists