Skip to content

Commit df570f5

Browse files
ming1davem330
authored andcommitted
samples/bpf: unit test for BPF_MAP_TYPE_PERCPU_ARRAY
A sanity test for BPF_MAP_TYPE_PERCPU_ARRAY Signed-off-by: Ming Lei <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent e155967 commit df570f5

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

samples/bpf/test_maps.c

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,94 @@ static void test_arraymap_sanity(int i, void *data)
236236
close(map_fd);
237237
}
238238

239+
static void test_percpu_arraymap_many_keys(void)
240+
{
241+
unsigned nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
242+
unsigned nr_keys = 20000;
243+
long values[nr_cpus];
244+
int key, map_fd, i;
245+
246+
map_fd = bpf_create_map(BPF_MAP_TYPE_PERCPU_ARRAY, sizeof(key),
247+
sizeof(values[0]), nr_keys);
248+
if (map_fd < 0) {
249+
printf("failed to create per-cpu arraymap '%s'\n",
250+
strerror(errno));
251+
exit(1);
252+
}
253+
254+
for (i = 0; i < nr_cpus; i++)
255+
values[i] = i + 10;
256+
257+
for (key = 0; key < nr_keys; key++)
258+
assert(bpf_update_elem(map_fd, &key, values, BPF_ANY) == 0);
259+
260+
for (key = 0; key < nr_keys; key++) {
261+
for (i = 0; i < nr_cpus; i++)
262+
values[i] = 0;
263+
assert(bpf_lookup_elem(map_fd, &key, values) == 0);
264+
for (i = 0; i < nr_cpus; i++)
265+
assert(values[i] == i + 10);
266+
}
267+
268+
close(map_fd);
269+
}
270+
271+
static void test_percpu_arraymap_sanity(int i, void *data)
272+
{
273+
unsigned nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
274+
long values[nr_cpus];
275+
int key, next_key, map_fd;
276+
277+
map_fd = bpf_create_map(BPF_MAP_TYPE_PERCPU_ARRAY, sizeof(key),
278+
sizeof(values[0]), 2);
279+
if (map_fd < 0) {
280+
printf("failed to create arraymap '%s'\n", strerror(errno));
281+
exit(1);
282+
}
283+
284+
for (i = 0; i < nr_cpus; i++)
285+
values[i] = i + 100;
286+
287+
key = 1;
288+
/* insert key=1 element */
289+
assert(bpf_update_elem(map_fd, &key, values, BPF_ANY) == 0);
290+
291+
values[0] = 0;
292+
assert(bpf_update_elem(map_fd, &key, values, BPF_NOEXIST) == -1 &&
293+
errno == EEXIST);
294+
295+
/* check that key=1 can be found */
296+
assert(bpf_lookup_elem(map_fd, &key, values) == 0 && values[0] == 100);
297+
298+
key = 0;
299+
/* check that key=0 is also found and zero initialized */
300+
assert(bpf_lookup_elem(map_fd, &key, values) == 0 &&
301+
values[0] == 0 && values[nr_cpus - 1] == 0);
302+
303+
304+
/* check that key=2 cannot be inserted due to max_entries limit */
305+
key = 2;
306+
assert(bpf_update_elem(map_fd, &key, values, BPF_EXIST) == -1 &&
307+
errno == E2BIG);
308+
309+
/* check that key = 2 doesn't exist */
310+
assert(bpf_lookup_elem(map_fd, &key, values) == -1 && errno == ENOENT);
311+
312+
/* iterate over two elements */
313+
assert(bpf_get_next_key(map_fd, &key, &next_key) == 0 &&
314+
next_key == 0);
315+
assert(bpf_get_next_key(map_fd, &next_key, &next_key) == 0 &&
316+
next_key == 1);
317+
assert(bpf_get_next_key(map_fd, &next_key, &next_key) == -1 &&
318+
errno == ENOENT);
319+
320+
/* delete shouldn't succeed */
321+
key = 1;
322+
assert(bpf_delete_elem(map_fd, &key) == -1 && errno == EINVAL);
323+
324+
close(map_fd);
325+
}
326+
239327
#define MAP_SIZE (32 * 1024)
240328
static void test_map_large(void)
241329
{
@@ -305,6 +393,7 @@ static void test_map_stress(void)
305393
run_parallel(100, test_hashmap_sanity, NULL);
306394
run_parallel(100, test_percpu_hashmap_sanity, NULL);
307395
run_parallel(100, test_arraymap_sanity, NULL);
396+
run_parallel(100, test_percpu_arraymap_sanity, NULL);
308397
}
309398

310399
#define TASKS 1024
@@ -379,6 +468,9 @@ int main(void)
379468
test_hashmap_sanity(0, NULL);
380469
test_percpu_hashmap_sanity(0, NULL);
381470
test_arraymap_sanity(0, NULL);
471+
test_percpu_arraymap_sanity(0, NULL);
472+
test_percpu_arraymap_many_keys();
473+
382474
test_map_large();
383475
test_map_parallel();
384476
test_map_stress();

0 commit comments

Comments
 (0)