Skip to content

Commit

Permalink
samples/bpf: unit test for BPF_MAP_TYPE_PERCPU_HASH
Browse files Browse the repository at this point in the history
A sanity test for BPF_MAP_TYPE_PERCPU_HASH.

Signed-off-by: Martin KaFai Lau <[email protected]>
Signed-off-by: Alexei Starovoitov <[email protected]>
Signed-off-by: David S. Miller <[email protected]>
  • Loading branch information
iamkafai authored and davem330 committed Feb 6, 2016
1 parent 15a07b3 commit e155967
Showing 1 changed file with 96 additions and 0 deletions.
96 changes: 96 additions & 0 deletions samples/bpf/test_maps.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit e155967

Please sign in to comment.