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.
20 #include <sys/resource.h>
22 #include <linux/bpf.h>
29 static void test_hashmap(int task, void *data)
31 long long key, next_key, value;
34 fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value),
37 printf("Failed to create hashmap '%s'!\n", strerror(errno));
43 /* Insert key=1 element. */
44 assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == 0);
47 /* BPF_NOEXIST means add new element if it doesn't exist. */
48 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == -1 &&
49 /* key=1 already exists. */
52 /* -1 is an invalid flag. */
53 assert(bpf_map_update_elem(fd, &key, &value, -1) == -1 &&
56 /* Check that key=1 can be found. */
57 assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && value == 1234);
60 /* Check that key=2 is not found. */
61 assert(bpf_map_lookup_elem(fd, &key, &value) == -1 && errno == ENOENT);
63 /* BPF_EXIST means update existing element. */
64 assert(bpf_map_update_elem(fd, &key, &value, BPF_EXIST) == -1 &&
65 /* key=2 is not there. */
68 /* Insert key=2 element. */
69 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == 0);
71 /* key=1 and key=2 were inserted, check that key=0 cannot be
72 * inserted due to max_entries limit.
75 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == -1 &&
78 /* Update existing element, though the map is full. */
80 assert(bpf_map_update_elem(fd, &key, &value, BPF_EXIST) == 0);
82 assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == 0);
84 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == -1 &&
87 /* Check that key = 0 doesn't exist. */
89 assert(bpf_map_delete_elem(fd, &key) == -1 && errno == ENOENT);
91 /* Iterate over two elements. */
92 assert(bpf_map_get_next_key(fd, &key, &next_key) == 0 &&
93 (next_key == 1 || next_key == 2));
94 assert(bpf_map_get_next_key(fd, &next_key, &next_key) == 0 &&
95 (next_key == 1 || next_key == 2));
96 assert(bpf_map_get_next_key(fd, &next_key, &next_key) == -1 &&
99 /* Delete both elements. */
101 assert(bpf_map_delete_elem(fd, &key) == 0);
103 assert(bpf_map_delete_elem(fd, &key) == 0);
104 assert(bpf_map_delete_elem(fd, &key) == -1 && errno == ENOENT);
107 /* Check that map is empty. */
108 assert(bpf_map_get_next_key(fd, &key, &next_key) == -1 &&
114 static void test_hashmap_sizes(int task, void *data)
118 for (i = 1; i <= 512; i <<= 1)
119 for (j = 1; j <= 1 << 18; j <<= 1) {
120 fd = bpf_create_map(BPF_MAP_TYPE_HASH, i, j,
123 printf("Failed to create hashmap key=%d value=%d '%s'\n",
124 i, j, strerror(errno));
128 usleep(10); /* give kernel time to destroy */
132 static void test_hashmap_percpu(int task, void *data)
134 unsigned int nr_cpus = bpf_num_possible_cpus();
135 long long value[nr_cpus];
136 long long key, next_key;
137 int expected_key_mask = 0;
140 fd = bpf_create_map(BPF_MAP_TYPE_PERCPU_HASH, sizeof(key),
141 sizeof(value[0]), 2, map_flags);
143 printf("Failed to create hashmap '%s'!\n", strerror(errno));
147 for (i = 0; i < nr_cpus; i++)
151 /* Insert key=1 element. */
152 assert(!(expected_key_mask & key));
153 assert(bpf_map_update_elem(fd, &key, value, BPF_ANY) == 0);
154 expected_key_mask |= key;
156 /* BPF_NOEXIST means add new element if it doesn't exist. */
157 assert(bpf_map_update_elem(fd, &key, value, BPF_NOEXIST) == -1 &&
158 /* key=1 already exists. */
161 /* -1 is an invalid flag. */
162 assert(bpf_map_update_elem(fd, &key, value, -1) == -1 &&
165 /* Check that key=1 can be found. Value could be 0 if the lookup
166 * was run from a different CPU.
169 assert(bpf_map_lookup_elem(fd, &key, value) == 0 && value[0] == 100);
172 /* Check that key=2 is not found. */
173 assert(bpf_map_lookup_elem(fd, &key, value) == -1 && errno == ENOENT);
175 /* BPF_EXIST means update existing element. */
176 assert(bpf_map_update_elem(fd, &key, value, BPF_EXIST) == -1 &&
177 /* key=2 is not there. */
180 /* Insert key=2 element. */
181 assert(!(expected_key_mask & key));
182 assert(bpf_map_update_elem(fd, &key, value, BPF_NOEXIST) == 0);
183 expected_key_mask |= key;
185 /* key=1 and key=2 were inserted, check that key=0 cannot be
186 * inserted due to max_entries limit.
189 assert(bpf_map_update_elem(fd, &key, value, BPF_NOEXIST) == -1 &&
192 /* Check that key = 0 doesn't exist. */
193 assert(bpf_map_delete_elem(fd, &key) == -1 && errno == ENOENT);
195 /* Iterate over two elements. */
196 while (!bpf_map_get_next_key(fd, &key, &next_key)) {
197 assert((expected_key_mask & next_key) == next_key);
198 expected_key_mask &= ~next_key;
200 assert(bpf_map_lookup_elem(fd, &next_key, value) == 0);
202 for (i = 0; i < nr_cpus; i++)
203 assert(value[i] == i + 100);
207 assert(errno == ENOENT);
209 /* Update with BPF_EXIST. */
211 assert(bpf_map_update_elem(fd, &key, value, BPF_EXIST) == 0);
213 /* Delete both elements. */
215 assert(bpf_map_delete_elem(fd, &key) == 0);
217 assert(bpf_map_delete_elem(fd, &key) == 0);
218 assert(bpf_map_delete_elem(fd, &key) == -1 && errno == ENOENT);
221 /* Check that map is empty. */
222 assert(bpf_map_get_next_key(fd, &key, &next_key) == -1 &&
228 static void test_arraymap(int task, void *data)
230 int key, next_key, fd;
233 fd = bpf_create_map(BPF_MAP_TYPE_ARRAY, sizeof(key), sizeof(value),
236 printf("Failed to create arraymap '%s'!\n", strerror(errno));
242 /* Insert key=1 element. */
243 assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == 0);
246 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == -1 &&
249 /* Check that key=1 can be found. */
250 assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && value == 1234);
253 /* Check that key=0 is also found and zero initialized. */
254 assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && value == 0);
256 /* key=0 and key=1 were inserted, check that key=2 cannot be inserted
257 * due to max_entries limit.
260 assert(bpf_map_update_elem(fd, &key, &value, BPF_EXIST) == -1 &&
263 /* Check that key = 2 doesn't exist. */
264 assert(bpf_map_lookup_elem(fd, &key, &value) == -1 && errno == ENOENT);
266 /* Iterate over two elements. */
267 assert(bpf_map_get_next_key(fd, &key, &next_key) == 0 &&
269 assert(bpf_map_get_next_key(fd, &next_key, &next_key) == 0 &&
271 assert(bpf_map_get_next_key(fd, &next_key, &next_key) == -1 &&
274 /* Delete shouldn't succeed. */
276 assert(bpf_map_delete_elem(fd, &key) == -1 && errno == EINVAL);
281 static void test_arraymap_percpu(int task, void *data)
283 unsigned int nr_cpus = bpf_num_possible_cpus();
284 int key, next_key, fd, i;
285 long values[nr_cpus];
287 fd = bpf_create_map(BPF_MAP_TYPE_PERCPU_ARRAY, sizeof(key),
288 sizeof(values[0]), 2, 0);
290 printf("Failed to create arraymap '%s'!\n", strerror(errno));
294 for (i = 0; i < nr_cpus; i++)
298 /* Insert key=1 element. */
299 assert(bpf_map_update_elem(fd, &key, values, BPF_ANY) == 0);
302 assert(bpf_map_update_elem(fd, &key, values, BPF_NOEXIST) == -1 &&
305 /* Check that key=1 can be found. */
306 assert(bpf_map_lookup_elem(fd, &key, values) == 0 && values[0] == 100);
309 /* Check that key=0 is also found and zero initialized. */
310 assert(bpf_map_lookup_elem(fd, &key, values) == 0 &&
311 values[0] == 0 && values[nr_cpus - 1] == 0);
313 /* Check that key=2 cannot be inserted due to max_entries limit. */
315 assert(bpf_map_update_elem(fd, &key, values, BPF_EXIST) == -1 &&
318 /* Check that key = 2 doesn't exist. */
319 assert(bpf_map_lookup_elem(fd, &key, values) == -1 && errno == ENOENT);
321 /* Iterate over two elements. */
322 assert(bpf_map_get_next_key(fd, &key, &next_key) == 0 &&
324 assert(bpf_map_get_next_key(fd, &next_key, &next_key) == 0 &&
326 assert(bpf_map_get_next_key(fd, &next_key, &next_key) == -1 &&
329 /* Delete shouldn't succeed. */
331 assert(bpf_map_delete_elem(fd, &key) == -1 && errno == EINVAL);
336 static void test_arraymap_percpu_many_keys(void)
338 unsigned int nr_cpus = bpf_num_possible_cpus();
339 /* nr_keys is not too large otherwise the test stresses percpu
340 * allocator more than anything else
342 unsigned int nr_keys = 2000;
343 long values[nr_cpus];
346 fd = bpf_create_map(BPF_MAP_TYPE_PERCPU_ARRAY, sizeof(key),
347 sizeof(values[0]), nr_keys, 0);
349 printf("Failed to create per-cpu arraymap '%s'!\n",
354 for (i = 0; i < nr_cpus; i++)
357 for (key = 0; key < nr_keys; key++)
358 assert(bpf_map_update_elem(fd, &key, values, BPF_ANY) == 0);
360 for (key = 0; key < nr_keys; key++) {
361 for (i = 0; i < nr_cpus; i++)
364 assert(bpf_map_lookup_elem(fd, &key, values) == 0);
366 for (i = 0; i < nr_cpus; i++)
367 assert(values[i] == i + 10);
373 #define MAP_SIZE (32 * 1024)
375 static void test_map_large(void)
384 fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value),
385 MAP_SIZE, map_flags);
387 printf("Failed to create large map '%s'!\n", strerror(errno));
391 for (i = 0; i < MAP_SIZE; i++) {
392 key = (struct bigkey) { .c = i };
395 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == 0);
399 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == -1 &&
402 /* Iterate through all elements. */
403 for (i = 0; i < MAP_SIZE; i++)
404 assert(bpf_map_get_next_key(fd, &key, &key) == 0);
405 assert(bpf_map_get_next_key(fd, &key, &key) == -1 && errno == ENOENT);
408 assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && value == 0);
410 assert(bpf_map_lookup_elem(fd, &key, &value) == -1 && errno == ENOENT);
415 static void run_parallel(int tasks, void (*fn)(int task, void *data),
421 for (i = 0; i < tasks; i++) {
426 } else if (pid[i] == -1) {
427 printf("Couldn't spawn #%d process!\n", i);
432 for (i = 0; i < tasks; i++) {
435 assert(waitpid(pid[i], &status, 0) == pid[i]);
440 static void test_map_stress(void)
442 run_parallel(100, test_hashmap, NULL);
443 run_parallel(100, test_hashmap_percpu, NULL);
444 run_parallel(100, test_hashmap_sizes, NULL);
446 run_parallel(100, test_arraymap, NULL);
447 run_parallel(100, test_arraymap_percpu, NULL);
455 static void do_work(int fn, void *data)
457 int do_update = ((int *)data)[1];
458 int fd = ((int *)data)[0];
461 for (i = fn; i < MAP_SIZE; i += TASKS) {
465 assert(bpf_map_update_elem(fd, &key, &value,
467 assert(bpf_map_update_elem(fd, &key, &value,
470 assert(bpf_map_delete_elem(fd, &key) == 0);
475 static void test_map_parallel(void)
477 int i, fd, key = 0, value = 0;
480 fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value),
481 MAP_SIZE, map_flags);
483 printf("Failed to create map for parallel test '%s'!\n",
488 /* Use the same fd in children to add elements to this map:
489 * child_0 adds key=0, key=1024, key=2048, ...
490 * child_1 adds key=1, key=1025, key=2049, ...
491 * child_1023 adds key=1023, ...
495 run_parallel(TASKS, do_work, data);
497 /* Check that key=0 is already there. */
498 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == -1 &&
501 /* Check that all elements were inserted. */
503 for (i = 0; i < MAP_SIZE; i++)
504 assert(bpf_map_get_next_key(fd, &key, &key) == 0);
505 assert(bpf_map_get_next_key(fd, &key, &key) == -1 && errno == ENOENT);
507 /* Another check for all elements */
508 for (i = 0; i < MAP_SIZE; i++) {
509 key = MAP_SIZE - i - 1;
511 assert(bpf_map_lookup_elem(fd, &key, &value) == 0 &&
515 /* Now let's delete all elemenets in parallel. */
517 run_parallel(TASKS, do_work, data);
519 /* Nothing should be left. */
521 assert(bpf_map_get_next_key(fd, &key, &key) == -1 && errno == ENOENT);
524 static void run_all_tests(void)
526 test_hashmap(0, NULL);
527 test_hashmap_percpu(0, NULL);
529 test_arraymap(0, NULL);
530 test_arraymap_percpu(0, NULL);
532 test_arraymap_percpu_many_keys();
541 struct rlimit rinf = { RLIM_INFINITY, RLIM_INFINITY };
543 setrlimit(RLIMIT_MEMLOCK, &rinf);
548 map_flags = BPF_F_NO_PREALLOC;
551 printf("test_maps: OK\n");