mirror of
https://github.com/AuxXxilium/linux_dsm_epyc7002.git
synced 2024-11-24 14:00:58 +07:00
samples/bpf: unit test for BPF_MAP_TYPE_PERCPU_HASH
A sanity test for BPF_MAP_TYPE_PERCPU_HASH. Signed-off-by: Martin KaFai Lau <kafai@fb.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: David S. Miller <davem@davemloft.net>
This commit is contained in:
parent
15a07b3381
commit
e155967179
@ -89,6 +89,100 @@ static void test_hashmap_sanity(int i, void *data)
|
||||
close(map_fd);
|
||||
}
|
||||
|
||||
/* sanity tests for percpu map API */
|
||||
static void test_percpu_hashmap_sanity(int task, void *data)
|
||||
{
|
||||
long long key, next_key;
|
||||
int expected_key_mask = 0;
|
||||
unsigned int nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
|
||||
long long value[nr_cpus];
|
||||
int map_fd, i;
|
||||
|
||||
map_fd = bpf_create_map(BPF_MAP_TYPE_PERCPU_HASH, sizeof(key),
|
||||
sizeof(value[0]), 2);
|
||||
if (map_fd < 0) {
|
||||
printf("failed to create hashmap '%s'\n", strerror(errno));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
for (i = 0; i < nr_cpus; i++)
|
||||
value[i] = i + 100;
|
||||
key = 1;
|
||||
/* insert key=1 element */
|
||||
assert(!(expected_key_mask & key));
|
||||
assert(bpf_update_elem(map_fd, &key, value, BPF_ANY) == 0);
|
||||
expected_key_mask |= key;
|
||||
|
||||
/* BPF_NOEXIST means: add new element if it doesn't exist */
|
||||
assert(bpf_update_elem(map_fd, &key, value, BPF_NOEXIST) == -1 &&
|
||||
/* key=1 already exists */
|
||||
errno == EEXIST);
|
||||
|
||||
/* -1 is an invalid flag */
|
||||
assert(bpf_update_elem(map_fd, &key, value, -1) == -1 &&
|
||||
errno == EINVAL);
|
||||
|
||||
/* check that key=1 can be found. value could be 0 if the lookup
|
||||
* was run from a different cpu.
|
||||
*/
|
||||
value[0] = 1;
|
||||
assert(bpf_lookup_elem(map_fd, &key, value) == 0 && value[0] == 100);
|
||||
|
||||
key = 2;
|
||||
/* check that key=2 is not found */
|
||||
assert(bpf_lookup_elem(map_fd, &key, value) == -1 && errno == ENOENT);
|
||||
|
||||
/* BPF_EXIST means: update existing element */
|
||||
assert(bpf_update_elem(map_fd, &key, value, BPF_EXIST) == -1 &&
|
||||
/* key=2 is not there */
|
||||
errno == ENOENT);
|
||||
|
||||
/* insert key=2 element */
|
||||
assert(!(expected_key_mask & key));
|
||||
assert(bpf_update_elem(map_fd, &key, value, BPF_NOEXIST) == 0);
|
||||
expected_key_mask |= key;
|
||||
|
||||
/* key=1 and key=2 were inserted, check that key=0 cannot be inserted
|
||||
* due to max_entries limit
|
||||
*/
|
||||
key = 0;
|
||||
assert(bpf_update_elem(map_fd, &key, value, BPF_NOEXIST) == -1 &&
|
||||
errno == E2BIG);
|
||||
|
||||
/* check that key = 0 doesn't exist */
|
||||
assert(bpf_delete_elem(map_fd, &key) == -1 && errno == ENOENT);
|
||||
|
||||
/* iterate over two elements */
|
||||
while (!bpf_get_next_key(map_fd, &key, &next_key)) {
|
||||
assert((expected_key_mask & next_key) == next_key);
|
||||
expected_key_mask &= ~next_key;
|
||||
|
||||
assert(bpf_lookup_elem(map_fd, &next_key, value) == 0);
|
||||
for (i = 0; i < nr_cpus; i++)
|
||||
assert(value[i] == i + 100);
|
||||
|
||||
key = next_key;
|
||||
}
|
||||
assert(errno == ENOENT);
|
||||
|
||||
/* Update with BPF_EXIST */
|
||||
key = 1;
|
||||
assert(bpf_update_elem(map_fd, &key, value, BPF_EXIST) == 0);
|
||||
|
||||
/* delete both elements */
|
||||
key = 1;
|
||||
assert(bpf_delete_elem(map_fd, &key) == 0);
|
||||
key = 2;
|
||||
assert(bpf_delete_elem(map_fd, &key) == 0);
|
||||
assert(bpf_delete_elem(map_fd, &key) == -1 && errno == ENOENT);
|
||||
|
||||
key = 0;
|
||||
/* check that map is empty */
|
||||
assert(bpf_get_next_key(map_fd, &key, &next_key) == -1 &&
|
||||
errno == ENOENT);
|
||||
close(map_fd);
|
||||
}
|
||||
|
||||
static void test_arraymap_sanity(int i, void *data)
|
||||
{
|
||||
int key, next_key, map_fd;
|
||||
@ -209,6 +303,7 @@ static void run_parallel(int tasks, void (*fn)(int i, void *data), void *data)
|
||||
static void test_map_stress(void)
|
||||
{
|
||||
run_parallel(100, test_hashmap_sanity, NULL);
|
||||
run_parallel(100, test_percpu_hashmap_sanity, NULL);
|
||||
run_parallel(100, test_arraymap_sanity, NULL);
|
||||
}
|
||||
|
||||
@ -282,6 +377,7 @@ static void test_map_parallel(void)
|
||||
int main(void)
|
||||
{
|
||||
test_hashmap_sanity(0, NULL);
|
||||
test_percpu_hashmap_sanity(0, NULL);
|
||||
test_arraymap_sanity(0, NULL);
|
||||
test_map_large();
|
||||
test_map_parallel();
|
||||
|
Loading…
Reference in New Issue
Block a user