forked from Minki/linux
bpf: add initial suite for selftests
Add a start of a test suite for kernel selftests. This moves test_verifier and test_maps over to tools/testing/selftests/bpf/ along with various code improvements and also adds a script for invoking test_bpf module. The test suite can simply be run via selftest framework, f.e.: # cd tools/testing/selftests/bpf/ # make # make run_tests Both test_verifier and test_maps were kind of misplaced in samples/bpf/ directory and we were looking into adding them to selftests for a while now, so it can be picked up by kbuild bot et al and hopefully also get more exposure and thus new test case additions. Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Acked-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: David S. Miller <davem@davemloft.net>
This commit is contained in:
parent
1a776b9ce8
commit
5aa5bd14c5
@ -2521,6 +2521,8 @@ L: netdev@vger.kernel.org
|
||||
L: linux-kernel@vger.kernel.org
|
||||
S: Supported
|
||||
F: kernel/bpf/
|
||||
F: tools/testing/selftests/bpf/
|
||||
F: lib/test_bpf.c
|
||||
|
||||
BROADCOM B44 10/100 ETHERNET DRIVER
|
||||
M: Michael Chan <michael.chan@broadcom.com>
|
||||
@ -8413,7 +8415,6 @@ F: include/uapi/linux/net_namespace.h
|
||||
F: tools/net/
|
||||
F: tools/testing/selftests/net/
|
||||
F: lib/random32.c
|
||||
F: lib/test_bpf.c
|
||||
|
||||
NETWORKING [IPv4/IPv6]
|
||||
M: "David S. Miller" <davem@davemloft.net>
|
||||
|
@ -2,7 +2,6 @@
|
||||
obj- := dummy.o
|
||||
|
||||
# List of programs to build
|
||||
hostprogs-y := test_verifier test_maps
|
||||
hostprogs-y += sock_example
|
||||
hostprogs-y += fds_example
|
||||
hostprogs-y += sockex1
|
||||
@ -28,8 +27,6 @@ hostprogs-y += test_current_task_under_cgroup
|
||||
hostprogs-y += trace_event
|
||||
hostprogs-y += sampleip
|
||||
|
||||
test_verifier-objs := test_verifier.o libbpf.o
|
||||
test_maps-objs := test_maps.o libbpf.o
|
||||
sock_example-objs := sock_example.o libbpf.o
|
||||
fds_example-objs := bpf_load.o libbpf.o fds_example.o
|
||||
sockex1-objs := bpf_load.o libbpf.o sockex1_user.o
|
||||
|
@ -1,503 +0,0 @@
|
||||
/*
|
||||
* Testsuite for eBPF maps
|
||||
*
|
||||
* Copyright (c) 2014 PLUMgrid, http://plumgrid.com
|
||||
* Copyright (c) 2016 Facebook
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of version 2 of the GNU General Public
|
||||
* License as published by the Free Software Foundation.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <linux/bpf.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
#include <sys/wait.h>
|
||||
#include <stdlib.h>
|
||||
#include "libbpf.h"
|
||||
|
||||
static int map_flags;
|
||||
|
||||
/* sanity tests for map API */
|
||||
static void test_hashmap_sanity(int i, void *data)
|
||||
{
|
||||
long long key, next_key, value;
|
||||
int map_fd;
|
||||
|
||||
map_fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value),
|
||||
2, map_flags);
|
||||
if (map_fd < 0) {
|
||||
printf("failed to create hashmap '%s'\n", strerror(errno));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
key = 1;
|
||||
value = 1234;
|
||||
/* insert key=1 element */
|
||||
assert(bpf_update_elem(map_fd, &key, &value, BPF_ANY) == 0);
|
||||
|
||||
value = 0;
|
||||
/* 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);
|
||||
|
||||
assert(bpf_update_elem(map_fd, &key, &value, -1) == -1 && errno == EINVAL);
|
||||
|
||||
/* check that key=1 can be found */
|
||||
assert(bpf_lookup_elem(map_fd, &key, &value) == 0 && value == 1234);
|
||||
|
||||
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(bpf_update_elem(map_fd, &key, &value, BPF_NOEXIST) == 0);
|
||||
|
||||
/* 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);
|
||||
|
||||
/* update existing element, thought the map is full */
|
||||
key = 1;
|
||||
assert(bpf_update_elem(map_fd, &key, &value, BPF_EXIST) == 0);
|
||||
key = 2;
|
||||
assert(bpf_update_elem(map_fd, &key, &value, BPF_ANY) == 0);
|
||||
key = 1;
|
||||
assert(bpf_update_elem(map_fd, &key, &value, BPF_ANY) == 0);
|
||||
|
||||
/* check that key = 0 doesn't exist */
|
||||
key = 0;
|
||||
assert(bpf_delete_elem(map_fd, &key) == -1 && errno == ENOENT);
|
||||
|
||||
/* iterate over two elements */
|
||||
assert(bpf_get_next_key(map_fd, &key, &next_key) == 0 &&
|
||||
(next_key == 1 || next_key == 2));
|
||||
assert(bpf_get_next_key(map_fd, &next_key, &next_key) == 0 &&
|
||||
(next_key == 1 || next_key == 2));
|
||||
assert(bpf_get_next_key(map_fd, &next_key, &next_key) == -1 &&
|
||||
errno == ENOENT);
|
||||
|
||||
/* 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);
|
||||
}
|
||||
|
||||
/* 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, map_flags);
|
||||
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;
|
||||
long long value;
|
||||
|
||||
map_fd = bpf_create_map(BPF_MAP_TYPE_ARRAY, sizeof(key), sizeof(value),
|
||||
2, 0);
|
||||
if (map_fd < 0) {
|
||||
printf("failed to create arraymap '%s'\n", strerror(errno));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
key = 1;
|
||||
value = 1234;
|
||||
/* insert key=1 element */
|
||||
assert(bpf_update_elem(map_fd, &key, &value, BPF_ANY) == 0);
|
||||
|
||||
value = 0;
|
||||
assert(bpf_update_elem(map_fd, &key, &value, BPF_NOEXIST) == -1 &&
|
||||
errno == EEXIST);
|
||||
|
||||
/* check that key=1 can be found */
|
||||
assert(bpf_lookup_elem(map_fd, &key, &value) == 0 && value == 1234);
|
||||
|
||||
key = 0;
|
||||
/* check that key=0 is also found and zero initialized */
|
||||
assert(bpf_lookup_elem(map_fd, &key, &value) == 0 && value == 0);
|
||||
|
||||
|
||||
/* key=0 and key=1 were inserted, check that key=2 cannot be inserted
|
||||
* due to max_entries limit
|
||||
*/
|
||||
key = 2;
|
||||
assert(bpf_update_elem(map_fd, &key, &value, BPF_EXIST) == -1 &&
|
||||
errno == E2BIG);
|
||||
|
||||
/* check that key = 2 doesn't exist */
|
||||
assert(bpf_lookup_elem(map_fd, &key, &value) == -1 && errno == ENOENT);
|
||||
|
||||
/* iterate over two elements */
|
||||
assert(bpf_get_next_key(map_fd, &key, &next_key) == 0 &&
|
||||
next_key == 0);
|
||||
assert(bpf_get_next_key(map_fd, &next_key, &next_key) == 0 &&
|
||||
next_key == 1);
|
||||
assert(bpf_get_next_key(map_fd, &next_key, &next_key) == -1 &&
|
||||
errno == ENOENT);
|
||||
|
||||
/* delete shouldn't succeed */
|
||||
key = 1;
|
||||
assert(bpf_delete_elem(map_fd, &key) == -1 && errno == EINVAL);
|
||||
|
||||
close(map_fd);
|
||||
}
|
||||
|
||||
static void test_percpu_arraymap_many_keys(void)
|
||||
{
|
||||
unsigned nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
|
||||
unsigned nr_keys = 20000;
|
||||
long values[nr_cpus];
|
||||
int key, map_fd, i;
|
||||
|
||||
map_fd = bpf_create_map(BPF_MAP_TYPE_PERCPU_ARRAY, sizeof(key),
|
||||
sizeof(values[0]), nr_keys, 0);
|
||||
if (map_fd < 0) {
|
||||
printf("failed to create per-cpu arraymap '%s'\n",
|
||||
strerror(errno));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
for (i = 0; i < nr_cpus; i++)
|
||||
values[i] = i + 10;
|
||||
|
||||
for (key = 0; key < nr_keys; key++)
|
||||
assert(bpf_update_elem(map_fd, &key, values, BPF_ANY) == 0);
|
||||
|
||||
for (key = 0; key < nr_keys; key++) {
|
||||
for (i = 0; i < nr_cpus; i++)
|
||||
values[i] = 0;
|
||||
assert(bpf_lookup_elem(map_fd, &key, values) == 0);
|
||||
for (i = 0; i < nr_cpus; i++)
|
||||
assert(values[i] == i + 10);
|
||||
}
|
||||
|
||||
close(map_fd);
|
||||
}
|
||||
|
||||
static void test_percpu_arraymap_sanity(int i, void *data)
|
||||
{
|
||||
unsigned nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
|
||||
long values[nr_cpus];
|
||||
int key, next_key, map_fd;
|
||||
|
||||
map_fd = bpf_create_map(BPF_MAP_TYPE_PERCPU_ARRAY, sizeof(key),
|
||||
sizeof(values[0]), 2, 0);
|
||||
if (map_fd < 0) {
|
||||
printf("failed to create arraymap '%s'\n", strerror(errno));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
for (i = 0; i < nr_cpus; i++)
|
||||
values[i] = i + 100;
|
||||
|
||||
key = 1;
|
||||
/* insert key=1 element */
|
||||
assert(bpf_update_elem(map_fd, &key, values, BPF_ANY) == 0);
|
||||
|
||||
values[0] = 0;
|
||||
assert(bpf_update_elem(map_fd, &key, values, BPF_NOEXIST) == -1 &&
|
||||
errno == EEXIST);
|
||||
|
||||
/* check that key=1 can be found */
|
||||
assert(bpf_lookup_elem(map_fd, &key, values) == 0 && values[0] == 100);
|
||||
|
||||
key = 0;
|
||||
/* check that key=0 is also found and zero initialized */
|
||||
assert(bpf_lookup_elem(map_fd, &key, values) == 0 &&
|
||||
values[0] == 0 && values[nr_cpus - 1] == 0);
|
||||
|
||||
|
||||
/* check that key=2 cannot be inserted due to max_entries limit */
|
||||
key = 2;
|
||||
assert(bpf_update_elem(map_fd, &key, values, BPF_EXIST) == -1 &&
|
||||
errno == E2BIG);
|
||||
|
||||
/* check that key = 2 doesn't exist */
|
||||
assert(bpf_lookup_elem(map_fd, &key, values) == -1 && errno == ENOENT);
|
||||
|
||||
/* iterate over two elements */
|
||||
assert(bpf_get_next_key(map_fd, &key, &next_key) == 0 &&
|
||||
next_key == 0);
|
||||
assert(bpf_get_next_key(map_fd, &next_key, &next_key) == 0 &&
|
||||
next_key == 1);
|
||||
assert(bpf_get_next_key(map_fd, &next_key, &next_key) == -1 &&
|
||||
errno == ENOENT);
|
||||
|
||||
/* delete shouldn't succeed */
|
||||
key = 1;
|
||||
assert(bpf_delete_elem(map_fd, &key) == -1 && errno == EINVAL);
|
||||
|
||||
close(map_fd);
|
||||
}
|
||||
|
||||
#define MAP_SIZE (32 * 1024)
|
||||
static void test_map_large(void)
|
||||
{
|
||||
struct bigkey {
|
||||
int a;
|
||||
char b[116];
|
||||
long long c;
|
||||
} key;
|
||||
int map_fd, i, value;
|
||||
|
||||
/* allocate 4Mbyte of memory */
|
||||
map_fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value),
|
||||
MAP_SIZE, map_flags);
|
||||
if (map_fd < 0) {
|
||||
printf("failed to create large map '%s'\n", strerror(errno));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
for (i = 0; i < MAP_SIZE; i++) {
|
||||
key = (struct bigkey) {.c = i};
|
||||
value = i;
|
||||
assert(bpf_update_elem(map_fd, &key, &value, BPF_NOEXIST) == 0);
|
||||
}
|
||||
key.c = -1;
|
||||
assert(bpf_update_elem(map_fd, &key, &value, BPF_NOEXIST) == -1 &&
|
||||
errno == E2BIG);
|
||||
|
||||
/* iterate through all elements */
|
||||
for (i = 0; i < MAP_SIZE; i++)
|
||||
assert(bpf_get_next_key(map_fd, &key, &key) == 0);
|
||||
assert(bpf_get_next_key(map_fd, &key, &key) == -1 && errno == ENOENT);
|
||||
|
||||
key.c = 0;
|
||||
assert(bpf_lookup_elem(map_fd, &key, &value) == 0 && value == 0);
|
||||
key.a = 1;
|
||||
assert(bpf_lookup_elem(map_fd, &key, &value) == -1 && errno == ENOENT);
|
||||
|
||||
close(map_fd);
|
||||
}
|
||||
|
||||
/* fork N children and wait for them to complete */
|
||||
static void run_parallel(int tasks, void (*fn)(int i, void *data), void *data)
|
||||
{
|
||||
pid_t pid[tasks];
|
||||
int i;
|
||||
|
||||
for (i = 0; i < tasks; i++) {
|
||||
pid[i] = fork();
|
||||
if (pid[i] == 0) {
|
||||
fn(i, data);
|
||||
exit(0);
|
||||
} else if (pid[i] == -1) {
|
||||
printf("couldn't spawn #%d process\n", i);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
for (i = 0; i < tasks; i++) {
|
||||
int status;
|
||||
|
||||
assert(waitpid(pid[i], &status, 0) == pid[i]);
|
||||
assert(status == 0);
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
run_parallel(100, test_percpu_arraymap_sanity, NULL);
|
||||
}
|
||||
|
||||
#define TASKS 1024
|
||||
#define DO_UPDATE 1
|
||||
#define DO_DELETE 0
|
||||
static void do_work(int fn, void *data)
|
||||
{
|
||||
int map_fd = ((int *)data)[0];
|
||||
int do_update = ((int *)data)[1];
|
||||
int i;
|
||||
int key, value;
|
||||
|
||||
for (i = fn; i < MAP_SIZE; i += TASKS) {
|
||||
key = value = i;
|
||||
if (do_update) {
|
||||
assert(bpf_update_elem(map_fd, &key, &value, BPF_NOEXIST) == 0);
|
||||
assert(bpf_update_elem(map_fd, &key, &value, BPF_EXIST) == 0);
|
||||
} else {
|
||||
assert(bpf_delete_elem(map_fd, &key) == 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void test_map_parallel(void)
|
||||
{
|
||||
int i, map_fd, key = 0, value = 0;
|
||||
int data[2];
|
||||
|
||||
map_fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value),
|
||||
MAP_SIZE, map_flags);
|
||||
if (map_fd < 0) {
|
||||
printf("failed to create map for parallel test '%s'\n",
|
||||
strerror(errno));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
data[0] = map_fd;
|
||||
data[1] = DO_UPDATE;
|
||||
/* use the same map_fd in children to add elements to this map
|
||||
* child_0 adds key=0, key=1024, key=2048, ...
|
||||
* child_1 adds key=1, key=1025, key=2049, ...
|
||||
* child_1023 adds key=1023, ...
|
||||
*/
|
||||
run_parallel(TASKS, do_work, data);
|
||||
|
||||
/* check that key=0 is already there */
|
||||
assert(bpf_update_elem(map_fd, &key, &value, BPF_NOEXIST) == -1 &&
|
||||
errno == EEXIST);
|
||||
|
||||
/* check that all elements were inserted */
|
||||
key = -1;
|
||||
for (i = 0; i < MAP_SIZE; i++)
|
||||
assert(bpf_get_next_key(map_fd, &key, &key) == 0);
|
||||
assert(bpf_get_next_key(map_fd, &key, &key) == -1 && errno == ENOENT);
|
||||
|
||||
/* another check for all elements */
|
||||
for (i = 0; i < MAP_SIZE; i++) {
|
||||
key = MAP_SIZE - i - 1;
|
||||
assert(bpf_lookup_elem(map_fd, &key, &value) == 0 &&
|
||||
value == key);
|
||||
}
|
||||
|
||||
/* now let's delete all elemenets in parallel */
|
||||
data[1] = DO_DELETE;
|
||||
run_parallel(TASKS, do_work, data);
|
||||
|
||||
/* nothing should be left */
|
||||
key = -1;
|
||||
assert(bpf_get_next_key(map_fd, &key, &key) == -1 && errno == ENOENT);
|
||||
}
|
||||
|
||||
static void run_all_tests(void)
|
||||
{
|
||||
test_hashmap_sanity(0, NULL);
|
||||
test_percpu_hashmap_sanity(0, NULL);
|
||||
test_arraymap_sanity(0, NULL);
|
||||
test_percpu_arraymap_sanity(0, NULL);
|
||||
test_percpu_arraymap_many_keys();
|
||||
|
||||
test_map_large();
|
||||
test_map_parallel();
|
||||
test_map_stress();
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
map_flags = 0;
|
||||
run_all_tests();
|
||||
map_flags = BPF_F_NO_PREALLOC;
|
||||
run_all_tests();
|
||||
printf("test_maps: OK\n");
|
||||
return 0;
|
||||
}
|
@ -218,6 +218,30 @@
|
||||
.off = OFF, \
|
||||
.imm = IMM })
|
||||
|
||||
/* BPF_LD_IMM64 macro encodes single 'load 64-bit immediate' insn */
|
||||
|
||||
#define BPF_LD_IMM64(DST, IMM) \
|
||||
BPF_LD_IMM64_RAW(DST, 0, IMM)
|
||||
|
||||
#define BPF_LD_IMM64_RAW(DST, SRC, IMM) \
|
||||
((struct bpf_insn) { \
|
||||
.code = BPF_LD | BPF_DW | BPF_IMM, \
|
||||
.dst_reg = DST, \
|
||||
.src_reg = SRC, \
|
||||
.off = 0, \
|
||||
.imm = (__u32) (IMM) }), \
|
||||
((struct bpf_insn) { \
|
||||
.code = 0, /* zero is reserved opcode */ \
|
||||
.dst_reg = 0, \
|
||||
.src_reg = 0, \
|
||||
.off = 0, \
|
||||
.imm = ((__u64) (IMM)) >> 32 })
|
||||
|
||||
/* pseudo BPF_LD_IMM64 insn used to refer to process-local map_fd */
|
||||
|
||||
#define BPF_LD_MAP_FD(DST, MAP_FD) \
|
||||
BPF_LD_IMM64_RAW(DST, BPF_PSEUDO_MAP_FD, MAP_FD)
|
||||
|
||||
/* Program exit */
|
||||
|
||||
#define BPF_EXIT_INSN() \
|
||||
|
@ -1,4 +1,5 @@
|
||||
TARGETS = breakpoints
|
||||
TARGETS = bpf
|
||||
TARGETS += breakpoints
|
||||
TARGETS += capabilities
|
||||
TARGETS += cpu-hotplug
|
||||
TARGETS += efivarfs
|
||||
|
2
tools/testing/selftests/bpf/.gitignore
vendored
Normal file
2
tools/testing/selftests/bpf/.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
test_verifier
|
||||
test_maps
|
13
tools/testing/selftests/bpf/Makefile
Normal file
13
tools/testing/selftests/bpf/Makefile
Normal file
@ -0,0 +1,13 @@
|
||||
CFLAGS += -Wall -O2
|
||||
|
||||
test_objs = test_verifier test_maps
|
||||
|
||||
TEST_PROGS := test_verifier test_maps test_kmod.sh
|
||||
TEST_FILES := $(test_objs)
|
||||
|
||||
all: $(test_objs)
|
||||
|
||||
include ../lib.mk
|
||||
|
||||
clean:
|
||||
$(RM) $(test_objs)
|
108
tools/testing/selftests/bpf/bpf_sys.h
Normal file
108
tools/testing/selftests/bpf/bpf_sys.h
Normal file
@ -0,0 +1,108 @@
|
||||
#ifndef __BPF_SYS__
|
||||
#define __BPF_SYS__
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <sys/syscall.h>
|
||||
|
||||
#include <linux/bpf.h>
|
||||
|
||||
static inline __u64 bpf_ptr_to_u64(const void *ptr)
|
||||
{
|
||||
return (__u64)(unsigned long) ptr;
|
||||
}
|
||||
|
||||
static inline int bpf(int cmd, union bpf_attr *attr, unsigned int size)
|
||||
{
|
||||
#ifdef __NR_bpf
|
||||
return syscall(__NR_bpf, cmd, attr, size);
|
||||
#else
|
||||
fprintf(stderr, "No bpf syscall, kernel headers too old?\n");
|
||||
errno = ENOSYS;
|
||||
return -1;
|
||||
#endif
|
||||
}
|
||||
|
||||
static inline int bpf_map_lookup(int fd, const void *key, void *value)
|
||||
{
|
||||
union bpf_attr attr = {};
|
||||
|
||||
attr.map_fd = fd;
|
||||
attr.key = bpf_ptr_to_u64(key);
|
||||
attr.value = bpf_ptr_to_u64(value);
|
||||
|
||||
return bpf(BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr));
|
||||
}
|
||||
|
||||
static inline int bpf_map_update(int fd, const void *key, const void *value,
|
||||
uint64_t flags)
|
||||
{
|
||||
union bpf_attr attr = {};
|
||||
|
||||
attr.map_fd = fd;
|
||||
attr.key = bpf_ptr_to_u64(key);
|
||||
attr.value = bpf_ptr_to_u64(value);
|
||||
attr.flags = flags;
|
||||
|
||||
return bpf(BPF_MAP_UPDATE_ELEM, &attr, sizeof(attr));
|
||||
}
|
||||
|
||||
static inline int bpf_map_delete(int fd, const void *key)
|
||||
{
|
||||
union bpf_attr attr = {};
|
||||
|
||||
attr.map_fd = fd;
|
||||
attr.key = bpf_ptr_to_u64(key);
|
||||
|
||||
return bpf(BPF_MAP_DELETE_ELEM, &attr, sizeof(attr));
|
||||
}
|
||||
|
||||
static inline int bpf_map_next_key(int fd, const void *key, void *next_key)
|
||||
{
|
||||
union bpf_attr attr = {};
|
||||
|
||||
attr.map_fd = fd;
|
||||
attr.key = bpf_ptr_to_u64(key);
|
||||
attr.next_key = bpf_ptr_to_u64(next_key);
|
||||
|
||||
return bpf(BPF_MAP_GET_NEXT_KEY, &attr, sizeof(attr));
|
||||
}
|
||||
|
||||
static inline int bpf_map_create(enum bpf_map_type type, uint32_t size_key,
|
||||
uint32_t size_value, uint32_t max_elem,
|
||||
uint32_t flags)
|
||||
{
|
||||
union bpf_attr attr = {};
|
||||
|
||||
attr.map_type = type;
|
||||
attr.key_size = size_key;
|
||||
attr.value_size = size_value;
|
||||
attr.max_entries = max_elem;
|
||||
attr.map_flags = flags;
|
||||
|
||||
return bpf(BPF_MAP_CREATE, &attr, sizeof(attr));
|
||||
}
|
||||
|
||||
static inline int bpf_prog_load(enum bpf_prog_type type,
|
||||
const struct bpf_insn *insns, size_t size_insns,
|
||||
const char *license, char *log, size_t size_log)
|
||||
{
|
||||
union bpf_attr attr = {};
|
||||
|
||||
attr.prog_type = type;
|
||||
attr.insns = bpf_ptr_to_u64(insns);
|
||||
attr.insn_cnt = size_insns / sizeof(struct bpf_insn);
|
||||
attr.license = bpf_ptr_to_u64(license);
|
||||
|
||||
if (size_log > 0) {
|
||||
attr.log_buf = bpf_ptr_to_u64(log);
|
||||
attr.log_size = size_log;
|
||||
attr.log_level = 1;
|
||||
log[0] = 0;
|
||||
}
|
||||
|
||||
return bpf(BPF_PROG_LOAD, &attr, sizeof(attr));
|
||||
}
|
||||
|
||||
#endif /* __BPF_SYS__ */
|
5
tools/testing/selftests/bpf/config
Normal file
5
tools/testing/selftests/bpf/config
Normal file
@ -0,0 +1,5 @@
|
||||
CONFIG_BPF=y
|
||||
CONFIG_BPF_SYSCALL=y
|
||||
CONFIG_NET_CLS_BPF=m
|
||||
CONFIG_BPF_EVENTS=y
|
||||
CONFIG_TEST_BPF=m
|
39
tools/testing/selftests/bpf/test_kmod.sh
Executable file
39
tools/testing/selftests/bpf/test_kmod.sh
Executable file
@ -0,0 +1,39 @@
|
||||
#!/bin/bash
|
||||
|
||||
SRC_TREE=../../../../
|
||||
|
||||
test_run()
|
||||
{
|
||||
sysctl -w net.core.bpf_jit_enable=$1 2>&1 > /dev/null
|
||||
sysctl -w net.core.bpf_jit_harden=$2 2>&1 > /dev/null
|
||||
|
||||
echo "[ JIT enabled:$1 hardened:$2 ]"
|
||||
dmesg -C
|
||||
insmod $SRC_TREE/lib/test_bpf.ko 2> /dev/null
|
||||
if [ $? -ne 0 ]; then
|
||||
rc=1
|
||||
fi
|
||||
rmmod test_bpf 2> /dev/null
|
||||
dmesg | grep FAIL
|
||||
}
|
||||
|
||||
test_save()
|
||||
{
|
||||
JE=`sysctl -n net.core.bpf_jit_enable`
|
||||
JH=`sysctl -n net.core.bpf_jit_harden`
|
||||
}
|
||||
|
||||
test_restore()
|
||||
{
|
||||
sysctl -w net.core.bpf_jit_enable=$JE 2>&1 > /dev/null
|
||||
sysctl -w net.core.bpf_jit_harden=$JH 2>&1 > /dev/null
|
||||
}
|
||||
|
||||
rc=0
|
||||
test_save
|
||||
test_run 0 0
|
||||
test_run 1 0
|
||||
test_run 1 1
|
||||
test_run 1 2
|
||||
test_restore
|
||||
exit $rc
|
525
tools/testing/selftests/bpf/test_maps.c
Normal file
525
tools/testing/selftests/bpf/test_maps.c
Normal file
@ -0,0 +1,525 @@
|
||||
/*
|
||||
* Testsuite for eBPF maps
|
||||
*
|
||||
* Copyright (c) 2014 PLUMgrid, http://plumgrid.com
|
||||
* Copyright (c) 2016 Facebook
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of version 2 of the GNU General Public
|
||||
* License as published by the Free Software Foundation.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <sys/wait.h>
|
||||
#include <sys/resource.h>
|
||||
|
||||
#include <linux/bpf.h>
|
||||
|
||||
#include "bpf_sys.h"
|
||||
|
||||
static int map_flags;
|
||||
|
||||
static void test_hashmap(int task, void *data)
|
||||
{
|
||||
long long key, next_key, value;
|
||||
int fd;
|
||||
|
||||
fd = bpf_map_create(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value),
|
||||
2, map_flags);
|
||||
if (fd < 0) {
|
||||
printf("Failed to create hashmap '%s'!\n", strerror(errno));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
key = 1;
|
||||
value = 1234;
|
||||
/* Insert key=1 element. */
|
||||
assert(bpf_map_update(fd, &key, &value, BPF_ANY) == 0);
|
||||
|
||||
value = 0;
|
||||
/* BPF_NOEXIST means add new element if it doesn't exist. */
|
||||
assert(bpf_map_update(fd, &key, &value, BPF_NOEXIST) == -1 &&
|
||||
/* key=1 already exists. */
|
||||
errno == EEXIST);
|
||||
|
||||
/* -1 is an invalid flag. */
|
||||
assert(bpf_map_update(fd, &key, &value, -1) == -1 && errno == EINVAL);
|
||||
|
||||
/* Check that key=1 can be found. */
|
||||
assert(bpf_map_lookup(fd, &key, &value) == 0 && value == 1234);
|
||||
|
||||
key = 2;
|
||||
/* Check that key=2 is not found. */
|
||||
assert(bpf_map_lookup(fd, &key, &value) == -1 && errno == ENOENT);
|
||||
|
||||
/* BPF_EXIST means update existing element. */
|
||||
assert(bpf_map_update(fd, &key, &value, BPF_EXIST) == -1 &&
|
||||
/* key=2 is not there. */
|
||||
errno == ENOENT);
|
||||
|
||||
/* Insert key=2 element. */
|
||||
assert(bpf_map_update(fd, &key, &value, BPF_NOEXIST) == 0);
|
||||
|
||||
/* key=1 and key=2 were inserted, check that key=0 cannot be
|
||||
* inserted due to max_entries limit.
|
||||
*/
|
||||
key = 0;
|
||||
assert(bpf_map_update(fd, &key, &value, BPF_NOEXIST) == -1 &&
|
||||
errno == E2BIG);
|
||||
|
||||
/* Update existing element, though the map is full. */
|
||||
key = 1;
|
||||
assert(bpf_map_update(fd, &key, &value, BPF_EXIST) == 0);
|
||||
key = 2;
|
||||
assert(bpf_map_update(fd, &key, &value, BPF_ANY) == 0);
|
||||
key = 1;
|
||||
assert(bpf_map_update(fd, &key, &value, BPF_ANY) == 0);
|
||||
|
||||
/* Check that key = 0 doesn't exist. */
|
||||
key = 0;
|
||||
assert(bpf_map_delete(fd, &key) == -1 && errno == ENOENT);
|
||||
|
||||
/* Iterate over two elements. */
|
||||
assert(bpf_map_next_key(fd, &key, &next_key) == 0 &&
|
||||
(next_key == 1 || next_key == 2));
|
||||
assert(bpf_map_next_key(fd, &next_key, &next_key) == 0 &&
|
||||
(next_key == 1 || next_key == 2));
|
||||
assert(bpf_map_next_key(fd, &next_key, &next_key) == -1 &&
|
||||
errno == ENOENT);
|
||||
|
||||
/* Delete both elements. */
|
||||
key = 1;
|
||||
assert(bpf_map_delete(fd, &key) == 0);
|
||||
key = 2;
|
||||
assert(bpf_map_delete(fd, &key) == 0);
|
||||
assert(bpf_map_delete(fd, &key) == -1 && errno == ENOENT);
|
||||
|
||||
key = 0;
|
||||
/* Check that map is empty. */
|
||||
assert(bpf_map_next_key(fd, &key, &next_key) == -1 &&
|
||||
errno == ENOENT);
|
||||
|
||||
close(fd);
|
||||
}
|
||||
|
||||
static void test_hashmap_percpu(int task, void *data)
|
||||
{
|
||||
unsigned int nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
|
||||
long long value[nr_cpus];
|
||||
long long key, next_key;
|
||||
int expected_key_mask = 0;
|
||||
int fd, i;
|
||||
|
||||
fd = bpf_map_create(BPF_MAP_TYPE_PERCPU_HASH, sizeof(key),
|
||||
sizeof(value[0]), 2, map_flags);
|
||||
if (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_map_update(fd, &key, value, BPF_ANY) == 0);
|
||||
expected_key_mask |= key;
|
||||
|
||||
/* BPF_NOEXIST means add new element if it doesn't exist. */
|
||||
assert(bpf_map_update(fd, &key, value, BPF_NOEXIST) == -1 &&
|
||||
/* key=1 already exists. */
|
||||
errno == EEXIST);
|
||||
|
||||
/* -1 is an invalid flag. */
|
||||
assert(bpf_map_update(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_map_lookup(fd, &key, value) == 0 && value[0] == 100);
|
||||
|
||||
key = 2;
|
||||
/* Check that key=2 is not found. */
|
||||
assert(bpf_map_lookup(fd, &key, value) == -1 && errno == ENOENT);
|
||||
|
||||
/* BPF_EXIST means update existing element. */
|
||||
assert(bpf_map_update(fd, &key, value, BPF_EXIST) == -1 &&
|
||||
/* key=2 is not there. */
|
||||
errno == ENOENT);
|
||||
|
||||
/* Insert key=2 element. */
|
||||
assert(!(expected_key_mask & key));
|
||||
assert(bpf_map_update(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_map_update(fd, &key, value, BPF_NOEXIST) == -1 &&
|
||||
errno == E2BIG);
|
||||
|
||||
/* Check that key = 0 doesn't exist. */
|
||||
assert(bpf_map_delete(fd, &key) == -1 && errno == ENOENT);
|
||||
|
||||
/* Iterate over two elements. */
|
||||
while (!bpf_map_next_key(fd, &key, &next_key)) {
|
||||
assert((expected_key_mask & next_key) == next_key);
|
||||
expected_key_mask &= ~next_key;
|
||||
|
||||
assert(bpf_map_lookup(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_map_update(fd, &key, value, BPF_EXIST) == 0);
|
||||
|
||||
/* Delete both elements. */
|
||||
key = 1;
|
||||
assert(bpf_map_delete(fd, &key) == 0);
|
||||
key = 2;
|
||||
assert(bpf_map_delete(fd, &key) == 0);
|
||||
assert(bpf_map_delete(fd, &key) == -1 && errno == ENOENT);
|
||||
|
||||
key = 0;
|
||||
/* Check that map is empty. */
|
||||
assert(bpf_map_next_key(fd, &key, &next_key) == -1 &&
|
||||
errno == ENOENT);
|
||||
|
||||
close(fd);
|
||||
}
|
||||
|
||||
static void test_arraymap(int task, void *data)
|
||||
{
|
||||
int key, next_key, fd;
|
||||
long long value;
|
||||
|
||||
fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, sizeof(key), sizeof(value),
|
||||
2, 0);
|
||||
if (fd < 0) {
|
||||
printf("Failed to create arraymap '%s'!\n", strerror(errno));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
key = 1;
|
||||
value = 1234;
|
||||
/* Insert key=1 element. */
|
||||
assert(bpf_map_update(fd, &key, &value, BPF_ANY) == 0);
|
||||
|
||||
value = 0;
|
||||
assert(bpf_map_update(fd, &key, &value, BPF_NOEXIST) == -1 &&
|
||||
errno == EEXIST);
|
||||
|
||||
/* Check that key=1 can be found. */
|
||||
assert(bpf_map_lookup(fd, &key, &value) == 0 && value == 1234);
|
||||
|
||||
key = 0;
|
||||
/* Check that key=0 is also found and zero initialized. */
|
||||
assert(bpf_map_lookup(fd, &key, &value) == 0 && value == 0);
|
||||
|
||||
/* key=0 and key=1 were inserted, check that key=2 cannot be inserted
|
||||
* due to max_entries limit.
|
||||
*/
|
||||
key = 2;
|
||||
assert(bpf_map_update(fd, &key, &value, BPF_EXIST) == -1 &&
|
||||
errno == E2BIG);
|
||||
|
||||
/* Check that key = 2 doesn't exist. */
|
||||
assert(bpf_map_lookup(fd, &key, &value) == -1 && errno == ENOENT);
|
||||
|
||||
/* Iterate over two elements. */
|
||||
assert(bpf_map_next_key(fd, &key, &next_key) == 0 &&
|
||||
next_key == 0);
|
||||
assert(bpf_map_next_key(fd, &next_key, &next_key) == 0 &&
|
||||
next_key == 1);
|
||||
assert(bpf_map_next_key(fd, &next_key, &next_key) == -1 &&
|
||||
errno == ENOENT);
|
||||
|
||||
/* Delete shouldn't succeed. */
|
||||
key = 1;
|
||||
assert(bpf_map_delete(fd, &key) == -1 && errno == EINVAL);
|
||||
|
||||
close(fd);
|
||||
}
|
||||
|
||||
static void test_arraymap_percpu(int task, void *data)
|
||||
{
|
||||
unsigned int nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
|
||||
int key, next_key, fd, i;
|
||||
long values[nr_cpus];
|
||||
|
||||
fd = bpf_map_create(BPF_MAP_TYPE_PERCPU_ARRAY, sizeof(key),
|
||||
sizeof(values[0]), 2, 0);
|
||||
if (fd < 0) {
|
||||
printf("Failed to create arraymap '%s'!\n", strerror(errno));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
for (i = 0; i < nr_cpus; i++)
|
||||
values[i] = i + 100;
|
||||
|
||||
key = 1;
|
||||
/* Insert key=1 element. */
|
||||
assert(bpf_map_update(fd, &key, values, BPF_ANY) == 0);
|
||||
|
||||
values[0] = 0;
|
||||
assert(bpf_map_update(fd, &key, values, BPF_NOEXIST) == -1 &&
|
||||
errno == EEXIST);
|
||||
|
||||
/* Check that key=1 can be found. */
|
||||
assert(bpf_map_lookup(fd, &key, values) == 0 && values[0] == 100);
|
||||
|
||||
key = 0;
|
||||
/* Check that key=0 is also found and zero initialized. */
|
||||
assert(bpf_map_lookup(fd, &key, values) == 0 &&
|
||||
values[0] == 0 && values[nr_cpus - 1] == 0);
|
||||
|
||||
/* Check that key=2 cannot be inserted due to max_entries limit. */
|
||||
key = 2;
|
||||
assert(bpf_map_update(fd, &key, values, BPF_EXIST) == -1 &&
|
||||
errno == E2BIG);
|
||||
|
||||
/* Check that key = 2 doesn't exist. */
|
||||
assert(bpf_map_lookup(fd, &key, values) == -1 && errno == ENOENT);
|
||||
|
||||
/* Iterate over two elements. */
|
||||
assert(bpf_map_next_key(fd, &key, &next_key) == 0 &&
|
||||
next_key == 0);
|
||||
assert(bpf_map_next_key(fd, &next_key, &next_key) == 0 &&
|
||||
next_key == 1);
|
||||
assert(bpf_map_next_key(fd, &next_key, &next_key) == -1 &&
|
||||
errno == ENOENT);
|
||||
|
||||
/* Delete shouldn't succeed. */
|
||||
key = 1;
|
||||
assert(bpf_map_delete(fd, &key) == -1 && errno == EINVAL);
|
||||
|
||||
close(fd);
|
||||
}
|
||||
|
||||
static void test_arraymap_percpu_many_keys(void)
|
||||
{
|
||||
unsigned int nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
|
||||
unsigned int nr_keys = 20000;
|
||||
long values[nr_cpus];
|
||||
int key, fd, i;
|
||||
|
||||
fd = bpf_map_create(BPF_MAP_TYPE_PERCPU_ARRAY, sizeof(key),
|
||||
sizeof(values[0]), nr_keys, 0);
|
||||
if (fd < 0) {
|
||||
printf("Failed to create per-cpu arraymap '%s'!\n",
|
||||
strerror(errno));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
for (i = 0; i < nr_cpus; i++)
|
||||
values[i] = i + 10;
|
||||
|
||||
for (key = 0; key < nr_keys; key++)
|
||||
assert(bpf_map_update(fd, &key, values, BPF_ANY) == 0);
|
||||
|
||||
for (key = 0; key < nr_keys; key++) {
|
||||
for (i = 0; i < nr_cpus; i++)
|
||||
values[i] = 0;
|
||||
|
||||
assert(bpf_map_lookup(fd, &key, values) == 0);
|
||||
|
||||
for (i = 0; i < nr_cpus; i++)
|
||||
assert(values[i] == i + 10);
|
||||
}
|
||||
|
||||
close(fd);
|
||||
}
|
||||
|
||||
#define MAP_SIZE (32 * 1024)
|
||||
|
||||
static void test_map_large(void)
|
||||
{
|
||||
struct bigkey {
|
||||
int a;
|
||||
char b[116];
|
||||
long long c;
|
||||
} key;
|
||||
int fd, i, value;
|
||||
|
||||
fd = bpf_map_create(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value),
|
||||
MAP_SIZE, map_flags);
|
||||
if (fd < 0) {
|
||||
printf("Failed to create large map '%s'!\n", strerror(errno));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
for (i = 0; i < MAP_SIZE; i++) {
|
||||
key = (struct bigkey) { .c = i };
|
||||
value = i;
|
||||
|
||||
assert(bpf_map_update(fd, &key, &value, BPF_NOEXIST) == 0);
|
||||
}
|
||||
|
||||
key.c = -1;
|
||||
assert(bpf_map_update(fd, &key, &value, BPF_NOEXIST) == -1 &&
|
||||
errno == E2BIG);
|
||||
|
||||
/* Iterate through all elements. */
|
||||
for (i = 0; i < MAP_SIZE; i++)
|
||||
assert(bpf_map_next_key(fd, &key, &key) == 0);
|
||||
assert(bpf_map_next_key(fd, &key, &key) == -1 && errno == ENOENT);
|
||||
|
||||
key.c = 0;
|
||||
assert(bpf_map_lookup(fd, &key, &value) == 0 && value == 0);
|
||||
key.a = 1;
|
||||
assert(bpf_map_lookup(fd, &key, &value) == -1 && errno == ENOENT);
|
||||
|
||||
close(fd);
|
||||
}
|
||||
|
||||
static void run_parallel(int tasks, void (*fn)(int task, void *data),
|
||||
void *data)
|
||||
{
|
||||
pid_t pid[tasks];
|
||||
int i;
|
||||
|
||||
for (i = 0; i < tasks; i++) {
|
||||
pid[i] = fork();
|
||||
if (pid[i] == 0) {
|
||||
fn(i, data);
|
||||
exit(0);
|
||||
} else if (pid[i] == -1) {
|
||||
printf("Couldn't spawn #%d process!\n", i);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
for (i = 0; i < tasks; i++) {
|
||||
int status;
|
||||
|
||||
assert(waitpid(pid[i], &status, 0) == pid[i]);
|
||||
assert(status == 0);
|
||||
}
|
||||
}
|
||||
|
||||
static void test_map_stress(void)
|
||||
{
|
||||
run_parallel(100, test_hashmap, NULL);
|
||||
run_parallel(100, test_hashmap_percpu, NULL);
|
||||
|
||||
run_parallel(100, test_arraymap, NULL);
|
||||
run_parallel(100, test_arraymap_percpu, NULL);
|
||||
}
|
||||
|
||||
#define TASKS 1024
|
||||
|
||||
#define DO_UPDATE 1
|
||||
#define DO_DELETE 0
|
||||
|
||||
static void do_work(int fn, void *data)
|
||||
{
|
||||
int do_update = ((int *)data)[1];
|
||||
int fd = ((int *)data)[0];
|
||||
int i, key, value;
|
||||
|
||||
for (i = fn; i < MAP_SIZE; i += TASKS) {
|
||||
key = value = i;
|
||||
|
||||
if (do_update) {
|
||||
assert(bpf_map_update(fd, &key, &value, BPF_NOEXIST) == 0);
|
||||
assert(bpf_map_update(fd, &key, &value, BPF_EXIST) == 0);
|
||||
} else {
|
||||
assert(bpf_map_delete(fd, &key) == 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void test_map_parallel(void)
|
||||
{
|
||||
int i, fd, key = 0, value = 0;
|
||||
int data[2];
|
||||
|
||||
fd = bpf_map_create(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value),
|
||||
MAP_SIZE, map_flags);
|
||||
if (fd < 0) {
|
||||
printf("Failed to create map for parallel test '%s'!\n",
|
||||
strerror(errno));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/* Use the same fd in children to add elements to this map:
|
||||
* child_0 adds key=0, key=1024, key=2048, ...
|
||||
* child_1 adds key=1, key=1025, key=2049, ...
|
||||
* child_1023 adds key=1023, ...
|
||||
*/
|
||||
data[0] = fd;
|
||||
data[1] = DO_UPDATE;
|
||||
run_parallel(TASKS, do_work, data);
|
||||
|
||||
/* Check that key=0 is already there. */
|
||||
assert(bpf_map_update(fd, &key, &value, BPF_NOEXIST) == -1 &&
|
||||
errno == EEXIST);
|
||||
|
||||
/* Check that all elements were inserted. */
|
||||
key = -1;
|
||||
for (i = 0; i < MAP_SIZE; i++)
|
||||
assert(bpf_map_next_key(fd, &key, &key) == 0);
|
||||
assert(bpf_map_next_key(fd, &key, &key) == -1 && errno == ENOENT);
|
||||
|
||||
/* Another check for all elements */
|
||||
for (i = 0; i < MAP_SIZE; i++) {
|
||||
key = MAP_SIZE - i - 1;
|
||||
|
||||
assert(bpf_map_lookup(fd, &key, &value) == 0 &&
|
||||
value == key);
|
||||
}
|
||||
|
||||
/* Now let's delete all elemenets in parallel. */
|
||||
data[1] = DO_DELETE;
|
||||
run_parallel(TASKS, do_work, data);
|
||||
|
||||
/* Nothing should be left. */
|
||||
key = -1;
|
||||
assert(bpf_map_next_key(fd, &key, &key) == -1 && errno == ENOENT);
|
||||
}
|
||||
|
||||
static void run_all_tests(void)
|
||||
{
|
||||
test_hashmap(0, NULL);
|
||||
test_hashmap_percpu(0, NULL);
|
||||
|
||||
test_arraymap(0, NULL);
|
||||
test_arraymap_percpu(0, NULL);
|
||||
|
||||
test_arraymap_percpu_many_keys();
|
||||
|
||||
test_map_large();
|
||||
test_map_parallel();
|
||||
test_map_stress();
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
struct rlimit rinf = { RLIM_INFINITY, RLIM_INFINITY };
|
||||
|
||||
setrlimit(RLIMIT_MEMLOCK, &rinf);
|
||||
|
||||
map_flags = 0;
|
||||
run_all_tests();
|
||||
|
||||
map_flags = BPF_F_NO_PREALLOC;
|
||||
run_all_tests();
|
||||
|
||||
printf("test_maps: OK\n");
|
||||
return 0;
|
||||
}
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user