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_ANY
) == 0);
86 /* Check that key = 0 doesn't exist. */
88 assert(bpf_map_delete_elem(fd
, &key
) == -1 && errno
== ENOENT
);
90 /* Iterate over two elements. */
91 assert(bpf_map_get_next_key(fd
, &key
, &next_key
) == 0 &&
92 (next_key
== 1 || next_key
== 2));
93 assert(bpf_map_get_next_key(fd
, &next_key
, &next_key
) == 0 &&
94 (next_key
== 1 || next_key
== 2));
95 assert(bpf_map_get_next_key(fd
, &next_key
, &next_key
) == -1 &&
98 /* Delete both elements. */
100 assert(bpf_map_delete_elem(fd
, &key
) == 0);
102 assert(bpf_map_delete_elem(fd
, &key
) == 0);
103 assert(bpf_map_delete_elem(fd
, &key
) == -1 && errno
== ENOENT
);
106 /* Check that map is empty. */
107 assert(bpf_map_get_next_key(fd
, &key
, &next_key
) == -1 &&
113 static void test_hashmap_percpu(int task
, void *data
)
115 unsigned int nr_cpus
= bpf_num_possible_cpus();
116 long long value
[nr_cpus
];
117 long long key
, next_key
;
118 int expected_key_mask
= 0;
121 fd
= bpf_create_map(BPF_MAP_TYPE_PERCPU_HASH
, sizeof(key
),
122 sizeof(value
[0]), 2, map_flags
);
124 printf("Failed to create hashmap '%s'!\n", strerror(errno
));
128 for (i
= 0; i
< nr_cpus
; i
++)
132 /* Insert key=1 element. */
133 assert(!(expected_key_mask
& key
));
134 assert(bpf_map_update_elem(fd
, &key
, value
, BPF_ANY
) == 0);
135 expected_key_mask
|= key
;
137 /* BPF_NOEXIST means add new element if it doesn't exist. */
138 assert(bpf_map_update_elem(fd
, &key
, value
, BPF_NOEXIST
) == -1 &&
139 /* key=1 already exists. */
142 /* -1 is an invalid flag. */
143 assert(bpf_map_update_elem(fd
, &key
, value
, -1) == -1 &&
146 /* Check that key=1 can be found. Value could be 0 if the lookup
147 * was run from a different CPU.
150 assert(bpf_map_lookup_elem(fd
, &key
, value
) == 0 && value
[0] == 100);
153 /* Check that key=2 is not found. */
154 assert(bpf_map_lookup_elem(fd
, &key
, value
) == -1 && errno
== ENOENT
);
156 /* BPF_EXIST means update existing element. */
157 assert(bpf_map_update_elem(fd
, &key
, value
, BPF_EXIST
) == -1 &&
158 /* key=2 is not there. */
161 /* Insert key=2 element. */
162 assert(!(expected_key_mask
& key
));
163 assert(bpf_map_update_elem(fd
, &key
, value
, BPF_NOEXIST
) == 0);
164 expected_key_mask
|= key
;
166 /* key=1 and key=2 were inserted, check that key=0 cannot be
167 * inserted due to max_entries limit.
170 assert(bpf_map_update_elem(fd
, &key
, value
, BPF_NOEXIST
) == -1 &&
173 /* Check that key = 0 doesn't exist. */
174 assert(bpf_map_delete_elem(fd
, &key
) == -1 && errno
== ENOENT
);
176 /* Iterate over two elements. */
177 while (!bpf_map_get_next_key(fd
, &key
, &next_key
)) {
178 assert((expected_key_mask
& next_key
) == next_key
);
179 expected_key_mask
&= ~next_key
;
181 assert(bpf_map_lookup_elem(fd
, &next_key
, value
) == 0);
183 for (i
= 0; i
< nr_cpus
; i
++)
184 assert(value
[i
] == i
+ 100);
188 assert(errno
== ENOENT
);
190 /* Update with BPF_EXIST. */
192 assert(bpf_map_update_elem(fd
, &key
, value
, BPF_EXIST
) == 0);
194 /* Delete both elements. */
196 assert(bpf_map_delete_elem(fd
, &key
) == 0);
198 assert(bpf_map_delete_elem(fd
, &key
) == 0);
199 assert(bpf_map_delete_elem(fd
, &key
) == -1 && errno
== ENOENT
);
202 /* Check that map is empty. */
203 assert(bpf_map_get_next_key(fd
, &key
, &next_key
) == -1 &&
209 static void test_arraymap(int task
, void *data
)
211 int key
, next_key
, fd
;
214 fd
= bpf_create_map(BPF_MAP_TYPE_ARRAY
, sizeof(key
), sizeof(value
),
217 printf("Failed to create arraymap '%s'!\n", strerror(errno
));
223 /* Insert key=1 element. */
224 assert(bpf_map_update_elem(fd
, &key
, &value
, BPF_ANY
) == 0);
227 assert(bpf_map_update_elem(fd
, &key
, &value
, BPF_NOEXIST
) == -1 &&
230 /* Check that key=1 can be found. */
231 assert(bpf_map_lookup_elem(fd
, &key
, &value
) == 0 && value
== 1234);
234 /* Check that key=0 is also found and zero initialized. */
235 assert(bpf_map_lookup_elem(fd
, &key
, &value
) == 0 && value
== 0);
237 /* key=0 and key=1 were inserted, check that key=2 cannot be inserted
238 * due to max_entries limit.
241 assert(bpf_map_update_elem(fd
, &key
, &value
, BPF_EXIST
) == -1 &&
244 /* Check that key = 2 doesn't exist. */
245 assert(bpf_map_lookup_elem(fd
, &key
, &value
) == -1 && errno
== ENOENT
);
247 /* Iterate over two elements. */
248 assert(bpf_map_get_next_key(fd
, &key
, &next_key
) == 0 &&
250 assert(bpf_map_get_next_key(fd
, &next_key
, &next_key
) == 0 &&
252 assert(bpf_map_get_next_key(fd
, &next_key
, &next_key
) == -1 &&
255 /* Delete shouldn't succeed. */
257 assert(bpf_map_delete_elem(fd
, &key
) == -1 && errno
== EINVAL
);
262 static void test_arraymap_percpu(int task
, void *data
)
264 unsigned int nr_cpus
= bpf_num_possible_cpus();
265 int key
, next_key
, fd
, i
;
266 long values
[nr_cpus
];
268 fd
= bpf_create_map(BPF_MAP_TYPE_PERCPU_ARRAY
, sizeof(key
),
269 sizeof(values
[0]), 2, 0);
271 printf("Failed to create arraymap '%s'!\n", strerror(errno
));
275 for (i
= 0; i
< nr_cpus
; i
++)
279 /* Insert key=1 element. */
280 assert(bpf_map_update_elem(fd
, &key
, values
, BPF_ANY
) == 0);
283 assert(bpf_map_update_elem(fd
, &key
, values
, BPF_NOEXIST
) == -1 &&
286 /* Check that key=1 can be found. */
287 assert(bpf_map_lookup_elem(fd
, &key
, values
) == 0 && values
[0] == 100);
290 /* Check that key=0 is also found and zero initialized. */
291 assert(bpf_map_lookup_elem(fd
, &key
, values
) == 0 &&
292 values
[0] == 0 && values
[nr_cpus
- 1] == 0);
294 /* Check that key=2 cannot be inserted due to max_entries limit. */
296 assert(bpf_map_update_elem(fd
, &key
, values
, BPF_EXIST
) == -1 &&
299 /* Check that key = 2 doesn't exist. */
300 assert(bpf_map_lookup_elem(fd
, &key
, values
) == -1 && errno
== ENOENT
);
302 /* Iterate over two elements. */
303 assert(bpf_map_get_next_key(fd
, &key
, &next_key
) == 0 &&
305 assert(bpf_map_get_next_key(fd
, &next_key
, &next_key
) == 0 &&
307 assert(bpf_map_get_next_key(fd
, &next_key
, &next_key
) == -1 &&
310 /* Delete shouldn't succeed. */
312 assert(bpf_map_delete_elem(fd
, &key
) == -1 && errno
== EINVAL
);
317 static void test_arraymap_percpu_many_keys(void)
319 unsigned int nr_cpus
= bpf_num_possible_cpus();
320 unsigned int nr_keys
= 20000;
321 long values
[nr_cpus
];
324 fd
= bpf_create_map(BPF_MAP_TYPE_PERCPU_ARRAY
, sizeof(key
),
325 sizeof(values
[0]), nr_keys
, 0);
327 printf("Failed to create per-cpu arraymap '%s'!\n",
332 for (i
= 0; i
< nr_cpus
; i
++)
335 for (key
= 0; key
< nr_keys
; key
++)
336 assert(bpf_map_update_elem(fd
, &key
, values
, BPF_ANY
) == 0);
338 for (key
= 0; key
< nr_keys
; key
++) {
339 for (i
= 0; i
< nr_cpus
; i
++)
342 assert(bpf_map_lookup_elem(fd
, &key
, values
) == 0);
344 for (i
= 0; i
< nr_cpus
; i
++)
345 assert(values
[i
] == i
+ 10);
351 #define MAP_SIZE (32 * 1024)
353 static void test_map_large(void)
362 fd
= bpf_create_map(BPF_MAP_TYPE_HASH
, sizeof(key
), sizeof(value
),
363 MAP_SIZE
, map_flags
);
365 printf("Failed to create large map '%s'!\n", strerror(errno
));
369 for (i
= 0; i
< MAP_SIZE
; i
++) {
370 key
= (struct bigkey
) { .c
= i
};
373 assert(bpf_map_update_elem(fd
, &key
, &value
, BPF_NOEXIST
) == 0);
377 assert(bpf_map_update_elem(fd
, &key
, &value
, BPF_NOEXIST
) == -1 &&
380 /* Iterate through all elements. */
381 for (i
= 0; i
< MAP_SIZE
; i
++)
382 assert(bpf_map_get_next_key(fd
, &key
, &key
) == 0);
383 assert(bpf_map_get_next_key(fd
, &key
, &key
) == -1 && errno
== ENOENT
);
386 assert(bpf_map_lookup_elem(fd
, &key
, &value
) == 0 && value
== 0);
388 assert(bpf_map_lookup_elem(fd
, &key
, &value
) == -1 && errno
== ENOENT
);
393 static void run_parallel(int tasks
, void (*fn
)(int task
, void *data
),
399 for (i
= 0; i
< tasks
; i
++) {
404 } else if (pid
[i
] == -1) {
405 printf("Couldn't spawn #%d process!\n", i
);
410 for (i
= 0; i
< tasks
; i
++) {
413 assert(waitpid(pid
[i
], &status
, 0) == pid
[i
]);
418 static void test_map_stress(void)
420 run_parallel(100, test_hashmap
, NULL
);
421 run_parallel(100, test_hashmap_percpu
, NULL
);
423 run_parallel(100, test_arraymap
, NULL
);
424 run_parallel(100, test_arraymap_percpu
, NULL
);
432 static void do_work(int fn
, void *data
)
434 int do_update
= ((int *)data
)[1];
435 int fd
= ((int *)data
)[0];
438 for (i
= fn
; i
< MAP_SIZE
; i
+= TASKS
) {
442 assert(bpf_map_update_elem(fd
, &key
, &value
,
444 assert(bpf_map_update_elem(fd
, &key
, &value
,
447 assert(bpf_map_delete_elem(fd
, &key
) == 0);
452 static void test_map_parallel(void)
454 int i
, fd
, key
= 0, value
= 0;
457 fd
= bpf_create_map(BPF_MAP_TYPE_HASH
, sizeof(key
), sizeof(value
),
458 MAP_SIZE
, map_flags
);
460 printf("Failed to create map for parallel test '%s'!\n",
465 /* Use the same fd in children to add elements to this map:
466 * child_0 adds key=0, key=1024, key=2048, ...
467 * child_1 adds key=1, key=1025, key=2049, ...
468 * child_1023 adds key=1023, ...
472 run_parallel(TASKS
, do_work
, data
);
474 /* Check that key=0 is already there. */
475 assert(bpf_map_update_elem(fd
, &key
, &value
, BPF_NOEXIST
) == -1 &&
478 /* Check that all elements were inserted. */
480 for (i
= 0; i
< MAP_SIZE
; i
++)
481 assert(bpf_map_get_next_key(fd
, &key
, &key
) == 0);
482 assert(bpf_map_get_next_key(fd
, &key
, &key
) == -1 && errno
== ENOENT
);
484 /* Another check for all elements */
485 for (i
= 0; i
< MAP_SIZE
; i
++) {
486 key
= MAP_SIZE
- i
- 1;
488 assert(bpf_map_lookup_elem(fd
, &key
, &value
) == 0 &&
492 /* Now let's delete all elemenets in parallel. */
494 run_parallel(TASKS
, do_work
, data
);
496 /* Nothing should be left. */
498 assert(bpf_map_get_next_key(fd
, &key
, &key
) == -1 && errno
== ENOENT
);
501 static void run_all_tests(void)
503 test_hashmap(0, NULL
);
504 test_hashmap_percpu(0, NULL
);
506 test_arraymap(0, NULL
);
507 test_arraymap_percpu(0, NULL
);
509 test_arraymap_percpu_many_keys();
518 struct rlimit rinf
= { RLIM_INFINITY
, RLIM_INFINITY
};
520 setrlimit(RLIMIT_MEMLOCK
, &rinf
);
525 map_flags
= BPF_F_NO_PREALLOC
;
528 printf("test_maps: OK\n");