2 * Testsuite for eBPF maps
4 * Copyright (c) 2014 PLUMgrid, http://plumgrid.com
5 * Copyright (c) 2016 Facebook
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of version 2 of the GNU General Public
9 * License as published by the Free Software Foundation.
13 #include <linux/bpf.h>
23 /* sanity tests for map API */
24 static void test_hashmap_sanity(int i, void *data)
26 long long key, next_key, value;
29 map_fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value),
32 printf("failed to create hashmap '%s'\n", strerror(errno));
38 /* insert key=1 element */
39 assert(bpf_update_elem(map_fd, &key, &value, BPF_ANY) == 0);
42 /* BPF_NOEXIST means: add new element if it doesn't exist */
43 assert(bpf_update_elem(map_fd, &key, &value, BPF_NOEXIST) == -1 &&
44 /* key=1 already exists */
47 assert(bpf_update_elem(map_fd, &key, &value, -1) == -1 && errno == EINVAL);
49 /* check that key=1 can be found */
50 assert(bpf_lookup_elem(map_fd, &key, &value) == 0 && value == 1234);
53 /* check that key=2 is not found */
54 assert(bpf_lookup_elem(map_fd, &key, &value) == -1 && errno == ENOENT);
56 /* BPF_EXIST means: update existing element */
57 assert(bpf_update_elem(map_fd, &key, &value, BPF_EXIST) == -1 &&
58 /* key=2 is not there */
61 /* insert key=2 element */
62 assert(bpf_update_elem(map_fd, &key, &value, BPF_NOEXIST) == 0);
64 /* key=1 and key=2 were inserted, check that key=0 cannot be inserted
65 * due to max_entries limit
68 assert(bpf_update_elem(map_fd, &key, &value, BPF_NOEXIST) == -1 &&
71 /* update existing element, thought the map is full */
73 assert(bpf_update_elem(map_fd, &key, &value, BPF_EXIST) == 0);
75 assert(bpf_update_elem(map_fd, &key, &value, BPF_ANY) == 0);
77 assert(bpf_update_elem(map_fd, &key, &value, BPF_ANY) == 0);
79 /* check that key = 0 doesn't exist */
81 assert(bpf_delete_elem(map_fd, &key) == -1 && errno == ENOENT);
83 /* iterate over two elements */
84 assert(bpf_get_next_key(map_fd, &key, &next_key) == 0 &&
85 (next_key == 1 || next_key == 2));
86 assert(bpf_get_next_key(map_fd, &next_key, &next_key) == 0 &&
87 (next_key == 1 || next_key == 2));
88 assert(bpf_get_next_key(map_fd, &next_key, &next_key) == -1 &&
91 /* delete both elements */
93 assert(bpf_delete_elem(map_fd, &key) == 0);
95 assert(bpf_delete_elem(map_fd, &key) == 0);
96 assert(bpf_delete_elem(map_fd, &key) == -1 && errno == ENOENT);
99 /* check that map is empty */
100 assert(bpf_get_next_key(map_fd, &key, &next_key) == -1 &&
105 /* sanity tests for percpu map API */
106 static void test_percpu_hashmap_sanity(int task, void *data)
108 long long key, next_key;
109 int expected_key_mask = 0;
110 unsigned int nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
111 long long value[nr_cpus];
114 map_fd = bpf_create_map(BPF_MAP_TYPE_PERCPU_HASH, sizeof(key),
115 sizeof(value[0]), 2, map_flags);
117 printf("failed to create hashmap '%s'\n", strerror(errno));
121 for (i = 0; i < nr_cpus; i++)
124 /* insert key=1 element */
125 assert(!(expected_key_mask & key));
126 assert(bpf_update_elem(map_fd, &key, value, BPF_ANY) == 0);
127 expected_key_mask |= key;
129 /* BPF_NOEXIST means: add new element if it doesn't exist */
130 assert(bpf_update_elem(map_fd, &key, value, BPF_NOEXIST) == -1 &&
131 /* key=1 already exists */
134 /* -1 is an invalid flag */
135 assert(bpf_update_elem(map_fd, &key, value, -1) == -1 &&
138 /* check that key=1 can be found. value could be 0 if the lookup
139 * was run from a different cpu.
142 assert(bpf_lookup_elem(map_fd, &key, value) == 0 && value[0] == 100);
145 /* check that key=2 is not found */
146 assert(bpf_lookup_elem(map_fd, &key, value) == -1 && errno == ENOENT);
148 /* BPF_EXIST means: update existing element */
149 assert(bpf_update_elem(map_fd, &key, value, BPF_EXIST) == -1 &&
150 /* key=2 is not there */
153 /* insert key=2 element */
154 assert(!(expected_key_mask & key));
155 assert(bpf_update_elem(map_fd, &key, value, BPF_NOEXIST) == 0);
156 expected_key_mask |= key;
158 /* key=1 and key=2 were inserted, check that key=0 cannot be inserted
159 * due to max_entries limit
162 assert(bpf_update_elem(map_fd, &key, value, BPF_NOEXIST) == -1 &&
165 /* check that key = 0 doesn't exist */
166 assert(bpf_delete_elem(map_fd, &key) == -1 && errno == ENOENT);
168 /* iterate over two elements */
169 while (!bpf_get_next_key(map_fd, &key, &next_key)) {
170 assert((expected_key_mask & next_key) == next_key);
171 expected_key_mask &= ~next_key;
173 assert(bpf_lookup_elem(map_fd, &next_key, value) == 0);
174 for (i = 0; i < nr_cpus; i++)
175 assert(value[i] == i + 100);
179 assert(errno == ENOENT);
181 /* Update with BPF_EXIST */
183 assert(bpf_update_elem(map_fd, &key, value, BPF_EXIST) == 0);
185 /* delete both elements */
187 assert(bpf_delete_elem(map_fd, &key) == 0);
189 assert(bpf_delete_elem(map_fd, &key) == 0);
190 assert(bpf_delete_elem(map_fd, &key) == -1 && errno == ENOENT);
193 /* check that map is empty */
194 assert(bpf_get_next_key(map_fd, &key, &next_key) == -1 &&
199 static void test_arraymap_sanity(int i, void *data)
201 int key, next_key, map_fd;
204 map_fd = bpf_create_map(BPF_MAP_TYPE_ARRAY, sizeof(key), sizeof(value),
207 printf("failed to create arraymap '%s'\n", strerror(errno));
213 /* insert key=1 element */
214 assert(bpf_update_elem(map_fd, &key, &value, BPF_ANY) == 0);
217 assert(bpf_update_elem(map_fd, &key, &value, BPF_NOEXIST) == -1 &&
220 /* check that key=1 can be found */
221 assert(bpf_lookup_elem(map_fd, &key, &value) == 0 && value == 1234);
224 /* check that key=0 is also found and zero initialized */
225 assert(bpf_lookup_elem(map_fd, &key, &value) == 0 && value == 0);
228 /* key=0 and key=1 were inserted, check that key=2 cannot be inserted
229 * due to max_entries limit
232 assert(bpf_update_elem(map_fd, &key, &value, BPF_EXIST) == -1 &&
235 /* check that key = 2 doesn't exist */
236 assert(bpf_lookup_elem(map_fd, &key, &value) == -1 && errno == ENOENT);
238 /* iterate over two elements */
239 assert(bpf_get_next_key(map_fd, &key, &next_key) == 0 &&
241 assert(bpf_get_next_key(map_fd, &next_key, &next_key) == 0 &&
243 assert(bpf_get_next_key(map_fd, &next_key, &next_key) == -1 &&
246 /* delete shouldn't succeed */
248 assert(bpf_delete_elem(map_fd, &key) == -1 && errno == EINVAL);
253 static void test_percpu_arraymap_many_keys(void)
255 unsigned nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
256 unsigned nr_keys = 20000;
257 long values[nr_cpus];
260 map_fd = bpf_create_map(BPF_MAP_TYPE_PERCPU_ARRAY, sizeof(key),
261 sizeof(values[0]), nr_keys, 0);
263 printf("failed to create per-cpu arraymap '%s'\n",
268 for (i = 0; i < nr_cpus; i++)
271 for (key = 0; key < nr_keys; key++)
272 assert(bpf_update_elem(map_fd, &key, values, BPF_ANY) == 0);
274 for (key = 0; key < nr_keys; key++) {
275 for (i = 0; i < nr_cpus; i++)
277 assert(bpf_lookup_elem(map_fd, &key, values) == 0);
278 for (i = 0; i < nr_cpus; i++)
279 assert(values[i] == i + 10);
285 static void test_percpu_arraymap_sanity(int i, void *data)
287 unsigned nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
288 long values[nr_cpus];
289 int key, next_key, map_fd;
291 map_fd = bpf_create_map(BPF_MAP_TYPE_PERCPU_ARRAY, sizeof(key),
292 sizeof(values[0]), 2, 0);
294 printf("failed to create arraymap '%s'\n", strerror(errno));
298 for (i = 0; i < nr_cpus; i++)
302 /* insert key=1 element */
303 assert(bpf_update_elem(map_fd, &key, values, BPF_ANY) == 0);
306 assert(bpf_update_elem(map_fd, &key, values, BPF_NOEXIST) == -1 &&
309 /* check that key=1 can be found */
310 assert(bpf_lookup_elem(map_fd, &key, values) == 0 && values[0] == 100);
313 /* check that key=0 is also found and zero initialized */
314 assert(bpf_lookup_elem(map_fd, &key, values) == 0 &&
315 values[0] == 0 && values[nr_cpus - 1] == 0);
318 /* check that key=2 cannot be inserted due to max_entries limit */
320 assert(bpf_update_elem(map_fd, &key, values, BPF_EXIST) == -1 &&
323 /* check that key = 2 doesn't exist */
324 assert(bpf_lookup_elem(map_fd, &key, values) == -1 && errno == ENOENT);
326 /* iterate over two elements */
327 assert(bpf_get_next_key(map_fd, &key, &next_key) == 0 &&
329 assert(bpf_get_next_key(map_fd, &next_key, &next_key) == 0 &&
331 assert(bpf_get_next_key(map_fd, &next_key, &next_key) == -1 &&
334 /* delete shouldn't succeed */
336 assert(bpf_delete_elem(map_fd, &key) == -1 && errno == EINVAL);
341 #define MAP_SIZE (32 * 1024)
342 static void test_map_large(void)
349 int map_fd, i, value;
351 /* allocate 4Mbyte of memory */
352 map_fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value),
353 MAP_SIZE, map_flags);
355 printf("failed to create large map '%s'\n", strerror(errno));
359 for (i = 0; i < MAP_SIZE; i++) {
360 key = (struct bigkey) {.c = i};
362 assert(bpf_update_elem(map_fd, &key, &value, BPF_NOEXIST) == 0);
365 assert(bpf_update_elem(map_fd, &key, &value, BPF_NOEXIST) == -1 &&
368 /* iterate through all elements */
369 for (i = 0; i < MAP_SIZE; i++)
370 assert(bpf_get_next_key(map_fd, &key, &key) == 0);
371 assert(bpf_get_next_key(map_fd, &key, &key) == -1 && errno == ENOENT);
374 assert(bpf_lookup_elem(map_fd, &key, &value) == 0 && value == 0);
376 assert(bpf_lookup_elem(map_fd, &key, &value) == -1 && errno == ENOENT);
381 /* fork N children and wait for them to complete */
382 static void run_parallel(int tasks, void (*fn)(int i, void *data), void *data)
387 for (i = 0; i < tasks; i++) {
392 } else if (pid[i] == -1) {
393 printf("couldn't spawn #%d process\n", i);
397 for (i = 0; i < tasks; i++) {
400 assert(waitpid(pid[i], &status, 0) == pid[i]);
405 static void test_map_stress(void)
407 run_parallel(100, test_hashmap_sanity, NULL);
408 run_parallel(100, test_percpu_hashmap_sanity, NULL);
409 run_parallel(100, test_arraymap_sanity, NULL);
410 run_parallel(100, test_percpu_arraymap_sanity, NULL);
416 static void do_work(int fn, void *data)
418 int map_fd = ((int *)data)[0];
419 int do_update = ((int *)data)[1];
423 for (i = fn; i < MAP_SIZE; i += TASKS) {
426 assert(bpf_update_elem(map_fd, &key, &value, BPF_NOEXIST) == 0);
427 assert(bpf_update_elem(map_fd, &key, &value, BPF_EXIST) == 0);
429 assert(bpf_delete_elem(map_fd, &key) == 0);
434 static void test_map_parallel(void)
436 int i, map_fd, key = 0, value = 0;
439 map_fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value),
440 MAP_SIZE, map_flags);
442 printf("failed to create map for parallel test '%s'\n",
449 /* use the same map_fd in children to add elements to this map
450 * child_0 adds key=0, key=1024, key=2048, ...
451 * child_1 adds key=1, key=1025, key=2049, ...
452 * child_1023 adds key=1023, ...
454 run_parallel(TASKS, do_work, data);
456 /* check that key=0 is already there */
457 assert(bpf_update_elem(map_fd, &key, &value, BPF_NOEXIST) == -1 &&
460 /* check that all elements were inserted */
462 for (i = 0; i < MAP_SIZE; i++)
463 assert(bpf_get_next_key(map_fd, &key, &key) == 0);
464 assert(bpf_get_next_key(map_fd, &key, &key) == -1 && errno == ENOENT);
466 /* another check for all elements */
467 for (i = 0; i < MAP_SIZE; i++) {
468 key = MAP_SIZE - i - 1;
469 assert(bpf_lookup_elem(map_fd, &key, &value) == 0 &&
473 /* now let's delete all elemenets in parallel */
475 run_parallel(TASKS, do_work, data);
477 /* nothing should be left */
479 assert(bpf_get_next_key(map_fd, &key, &key) == -1 && errno == ENOENT);
482 static void run_all_tests(void)
484 test_hashmap_sanity(0, NULL);
485 test_percpu_hashmap_sanity(0, NULL);
486 test_arraymap_sanity(0, NULL);
487 test_percpu_arraymap_sanity(0, NULL);
488 test_percpu_arraymap_many_keys();
499 map_flags = BPF_F_NO_PREALLOC;
501 printf("test_maps: OK\n");