From: Paul Chaignon <paul.chaignon@orange.com>
To: bpf@vger.kernel.org
Cc: paul.chaignon@gmail.com, netdev@vger.kernel.org,
Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Martin KaFai Lau <kafai@fb.com>, Song Liu <songliubraving@fb.com>,
Yonghong Song <yhs@fb.com>, Andrii Nakryiko <andriin@fb.com>
Subject: [PATCH bpf-next 2/3] selftests/bpf: Tests for single-cpu updates of per-cpu maps
Date: Wed, 18 Dec 2019 15:23:33 +0100 [thread overview]
Message-ID: <d922ded45e0d3a76e918e1eed0426d3743d20f0d.1576673843.git.paul.chaignon@orange.com> (raw)
In-Reply-To: <cover.1576673841.git.paul.chaignon@orange.com>
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@orange.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
next prev parent reply other threads:[~2019-12-18 14:23 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-12-18 14:22 [PATCH bpf-next 0/3] Single-cpu updates for per-cpu maps Paul Chaignon
2019-12-18 14:23 ` [PATCH bpf-next 1/3] bpf: " Paul Chaignon
2019-12-18 18:00 ` Alexei Starovoitov
2019-12-19 9:14 ` Paul Chaignon
2019-12-18 19:10 ` Yonghong Song
2019-12-19 0:49 ` Andrii Nakryiko
2019-12-18 14:23 ` Paul Chaignon [this message]
2019-12-18 14:23 ` [PATCH bpf-next 3/3] bpftool: Support single-cpu " Paul Chaignon
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=d922ded45e0d3a76e918e1eed0426d3743d20f0d.1576673843.git.paul.chaignon@orange.com \
--to=paul.chaignon@orange.com \
--cc=andriin@fb.com \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=kafai@fb.com \
--cc=netdev@vger.kernel.org \
--cc=paul.chaignon@gmail.com \
--cc=songliubraving@fb.com \
--cc=yhs@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).