2 * Testsuite for eBPF maps
4 * Copyright (c) 2014 PLUMgrid, http://plumgrid.com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of version 2 of the GNU General Public
8 * License as published by the Free Software Foundation.
12 #include <linux/bpf.h>
20 /* sanity tests for map API */
21 static void test_hashmap_sanity(int i
, void *data
)
23 long long key
, next_key
, value
;
26 map_fd
= bpf_create_map(BPF_MAP_TYPE_HASH
, sizeof(key
), sizeof(value
), 2);
28 printf("failed to create hashmap '%s'\n", strerror(errno
));
34 /* insert key=1 element */
35 assert(bpf_update_elem(map_fd
, &key
, &value
, BPF_ANY
) == 0);
38 /* BPF_NOEXIST means: add new element if it doesn't exist */
39 assert(bpf_update_elem(map_fd
, &key
, &value
, BPF_NOEXIST
) == -1 &&
40 /* key=1 already exists */
43 assert(bpf_update_elem(map_fd
, &key
, &value
, -1) == -1 && errno
== EINVAL
);
45 /* check that key=1 can be found */
46 assert(bpf_lookup_elem(map_fd
, &key
, &value
) == 0 && value
== 1234);
49 /* check that key=2 is not found */
50 assert(bpf_lookup_elem(map_fd
, &key
, &value
) == -1 && errno
== ENOENT
);
52 /* BPF_EXIST means: update existing element */
53 assert(bpf_update_elem(map_fd
, &key
, &value
, BPF_EXIST
) == -1 &&
54 /* key=2 is not there */
57 /* insert key=2 element */
58 assert(bpf_update_elem(map_fd
, &key
, &value
, BPF_NOEXIST
) == 0);
60 /* key=1 and key=2 were inserted, check that key=0 cannot be inserted
61 * due to max_entries limit
64 assert(bpf_update_elem(map_fd
, &key
, &value
, BPF_NOEXIST
) == -1 &&
67 /* check that key = 0 doesn't exist */
68 assert(bpf_delete_elem(map_fd
, &key
) == -1 && errno
== ENOENT
);
70 /* iterate over two elements */
71 assert(bpf_get_next_key(map_fd
, &key
, &next_key
) == 0 &&
72 (next_key
== 1 || next_key
== 2));
73 assert(bpf_get_next_key(map_fd
, &next_key
, &next_key
) == 0 &&
74 (next_key
== 1 || next_key
== 2));
75 assert(bpf_get_next_key(map_fd
, &next_key
, &next_key
) == -1 &&
78 /* delete both elements */
80 assert(bpf_delete_elem(map_fd
, &key
) == 0);
82 assert(bpf_delete_elem(map_fd
, &key
) == 0);
83 assert(bpf_delete_elem(map_fd
, &key
) == -1 && errno
== ENOENT
);
86 /* check that map is empty */
87 assert(bpf_get_next_key(map_fd
, &key
, &next_key
) == -1 &&
92 /* sanity tests for percpu map API */
93 static void test_percpu_hashmap_sanity(int task
, void *data
)
95 long long key
, next_key
;
96 int expected_key_mask
= 0;
97 unsigned int nr_cpus
= sysconf(_SC_NPROCESSORS_CONF
);
98 long long value
[nr_cpus
];
101 map_fd
= bpf_create_map(BPF_MAP_TYPE_PERCPU_HASH
, sizeof(key
),
102 sizeof(value
[0]), 2);
104 printf("failed to create hashmap '%s'\n", strerror(errno
));
108 for (i
= 0; i
< nr_cpus
; i
++)
111 /* insert key=1 element */
112 assert(!(expected_key_mask
& key
));
113 assert(bpf_update_elem(map_fd
, &key
, value
, BPF_ANY
) == 0);
114 expected_key_mask
|= key
;
116 /* BPF_NOEXIST means: add new element if it doesn't exist */
117 assert(bpf_update_elem(map_fd
, &key
, value
, BPF_NOEXIST
) == -1 &&
118 /* key=1 already exists */
121 /* -1 is an invalid flag */
122 assert(bpf_update_elem(map_fd
, &key
, value
, -1) == -1 &&
125 /* check that key=1 can be found. value could be 0 if the lookup
126 * was run from a different cpu.
129 assert(bpf_lookup_elem(map_fd
, &key
, value
) == 0 && value
[0] == 100);
132 /* check that key=2 is not found */
133 assert(bpf_lookup_elem(map_fd
, &key
, value
) == -1 && errno
== ENOENT
);
135 /* BPF_EXIST means: update existing element */
136 assert(bpf_update_elem(map_fd
, &key
, value
, BPF_EXIST
) == -1 &&
137 /* key=2 is not there */
140 /* insert key=2 element */
141 assert(!(expected_key_mask
& key
));
142 assert(bpf_update_elem(map_fd
, &key
, value
, BPF_NOEXIST
) == 0);
143 expected_key_mask
|= key
;
145 /* key=1 and key=2 were inserted, check that key=0 cannot be inserted
146 * due to max_entries limit
149 assert(bpf_update_elem(map_fd
, &key
, value
, BPF_NOEXIST
) == -1 &&
152 /* check that key = 0 doesn't exist */
153 assert(bpf_delete_elem(map_fd
, &key
) == -1 && errno
== ENOENT
);
155 /* iterate over two elements */
156 while (!bpf_get_next_key(map_fd
, &key
, &next_key
)) {
157 assert((expected_key_mask
& next_key
) == next_key
);
158 expected_key_mask
&= ~next_key
;
160 assert(bpf_lookup_elem(map_fd
, &next_key
, value
) == 0);
161 for (i
= 0; i
< nr_cpus
; i
++)
162 assert(value
[i
] == i
+ 100);
166 assert(errno
== ENOENT
);
168 /* Update with BPF_EXIST */
170 assert(bpf_update_elem(map_fd
, &key
, value
, BPF_EXIST
) == 0);
172 /* delete both elements */
174 assert(bpf_delete_elem(map_fd
, &key
) == 0);
176 assert(bpf_delete_elem(map_fd
, &key
) == 0);
177 assert(bpf_delete_elem(map_fd
, &key
) == -1 && errno
== ENOENT
);
180 /* check that map is empty */
181 assert(bpf_get_next_key(map_fd
, &key
, &next_key
) == -1 &&
186 static void test_arraymap_sanity(int i
, void *data
)
188 int key
, next_key
, map_fd
;
191 map_fd
= bpf_create_map(BPF_MAP_TYPE_ARRAY
, sizeof(key
), sizeof(value
), 2);
193 printf("failed to create arraymap '%s'\n", strerror(errno
));
199 /* insert key=1 element */
200 assert(bpf_update_elem(map_fd
, &key
, &value
, BPF_ANY
) == 0);
203 assert(bpf_update_elem(map_fd
, &key
, &value
, BPF_NOEXIST
) == -1 &&
206 /* check that key=1 can be found */
207 assert(bpf_lookup_elem(map_fd
, &key
, &value
) == 0 && value
== 1234);
210 /* check that key=0 is also found and zero initialized */
211 assert(bpf_lookup_elem(map_fd
, &key
, &value
) == 0 && value
== 0);
214 /* key=0 and key=1 were inserted, check that key=2 cannot be inserted
215 * due to max_entries limit
218 assert(bpf_update_elem(map_fd
, &key
, &value
, BPF_EXIST
) == -1 &&
221 /* check that key = 2 doesn't exist */
222 assert(bpf_lookup_elem(map_fd
, &key
, &value
) == -1 && errno
== ENOENT
);
224 /* iterate over two elements */
225 assert(bpf_get_next_key(map_fd
, &key
, &next_key
) == 0 &&
227 assert(bpf_get_next_key(map_fd
, &next_key
, &next_key
) == 0 &&
229 assert(bpf_get_next_key(map_fd
, &next_key
, &next_key
) == -1 &&
232 /* delete shouldn't succeed */
234 assert(bpf_delete_elem(map_fd
, &key
) == -1 && errno
== EINVAL
);
239 static void test_percpu_arraymap_many_keys(void)
241 unsigned nr_cpus
= sysconf(_SC_NPROCESSORS_CONF
);
242 unsigned nr_keys
= 20000;
243 long values
[nr_cpus
];
246 map_fd
= bpf_create_map(BPF_MAP_TYPE_PERCPU_ARRAY
, sizeof(key
),
247 sizeof(values
[0]), nr_keys
);
249 printf("failed to create per-cpu arraymap '%s'\n",
254 for (i
= 0; i
< nr_cpus
; i
++)
257 for (key
= 0; key
< nr_keys
; key
++)
258 assert(bpf_update_elem(map_fd
, &key
, values
, BPF_ANY
) == 0);
260 for (key
= 0; key
< nr_keys
; key
++) {
261 for (i
= 0; i
< nr_cpus
; i
++)
263 assert(bpf_lookup_elem(map_fd
, &key
, values
) == 0);
264 for (i
= 0; i
< nr_cpus
; i
++)
265 assert(values
[i
] == i
+ 10);
271 static void test_percpu_arraymap_sanity(int i
, void *data
)
273 unsigned nr_cpus
= sysconf(_SC_NPROCESSORS_CONF
);
274 long values
[nr_cpus
];
275 int key
, next_key
, map_fd
;
277 map_fd
= bpf_create_map(BPF_MAP_TYPE_PERCPU_ARRAY
, sizeof(key
),
278 sizeof(values
[0]), 2);
280 printf("failed to create arraymap '%s'\n", strerror(errno
));
284 for (i
= 0; i
< nr_cpus
; i
++)
288 /* insert key=1 element */
289 assert(bpf_update_elem(map_fd
, &key
, values
, BPF_ANY
) == 0);
292 assert(bpf_update_elem(map_fd
, &key
, values
, BPF_NOEXIST
) == -1 &&
295 /* check that key=1 can be found */
296 assert(bpf_lookup_elem(map_fd
, &key
, values
) == 0 && values
[0] == 100);
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);
304 /* check that key=2 cannot be inserted due to max_entries limit */
306 assert(bpf_update_elem(map_fd
, &key
, values
, BPF_EXIST
) == -1 &&
309 /* check that key = 2 doesn't exist */
310 assert(bpf_lookup_elem(map_fd
, &key
, values
) == -1 && errno
== ENOENT
);
312 /* iterate over two elements */
313 assert(bpf_get_next_key(map_fd
, &key
, &next_key
) == 0 &&
315 assert(bpf_get_next_key(map_fd
, &next_key
, &next_key
) == 0 &&
317 assert(bpf_get_next_key(map_fd
, &next_key
, &next_key
) == -1 &&
320 /* delete shouldn't succeed */
322 assert(bpf_delete_elem(map_fd
, &key
) == -1 && errno
== EINVAL
);
327 #define MAP_SIZE (32 * 1024)
328 static void test_map_large(void)
335 int map_fd
, i
, value
;
337 /* allocate 4Mbyte of memory */
338 map_fd
= bpf_create_map(BPF_MAP_TYPE_HASH
, sizeof(key
), sizeof(value
),
341 printf("failed to create large map '%s'\n", strerror(errno
));
345 for (i
= 0; i
< MAP_SIZE
; i
++) {
346 key
= (struct bigkey
) {.c
= i
};
348 assert(bpf_update_elem(map_fd
, &key
, &value
, BPF_NOEXIST
) == 0);
351 assert(bpf_update_elem(map_fd
, &key
, &value
, BPF_NOEXIST
) == -1 &&
354 /* iterate through all elements */
355 for (i
= 0; i
< MAP_SIZE
; i
++)
356 assert(bpf_get_next_key(map_fd
, &key
, &key
) == 0);
357 assert(bpf_get_next_key(map_fd
, &key
, &key
) == -1 && errno
== ENOENT
);
360 assert(bpf_lookup_elem(map_fd
, &key
, &value
) == 0 && value
== 0);
362 assert(bpf_lookup_elem(map_fd
, &key
, &value
) == -1 && errno
== ENOENT
);
367 /* fork N children and wait for them to complete */
368 static void run_parallel(int tasks
, void (*fn
)(int i
, void *data
), void *data
)
373 for (i
= 0; i
< tasks
; i
++) {
378 } else if (pid
[i
] == -1) {
379 printf("couldn't spawn #%d process\n", i
);
383 for (i
= 0; i
< tasks
; i
++) {
386 assert(waitpid(pid
[i
], &status
, 0) == pid
[i
]);
391 static void test_map_stress(void)
393 run_parallel(100, test_hashmap_sanity
, NULL
);
394 run_parallel(100, test_percpu_hashmap_sanity
, NULL
);
395 run_parallel(100, test_arraymap_sanity
, NULL
);
396 run_parallel(100, test_percpu_arraymap_sanity
, NULL
);
402 static void do_work(int fn
, void *data
)
404 int map_fd
= ((int *)data
)[0];
405 int do_update
= ((int *)data
)[1];
409 for (i
= fn
; i
< MAP_SIZE
; i
+= TASKS
) {
412 assert(bpf_update_elem(map_fd
, &key
, &value
, BPF_NOEXIST
) == 0);
414 assert(bpf_delete_elem(map_fd
, &key
) == 0);
418 static void test_map_parallel(void)
420 int i
, map_fd
, key
= 0, value
= 0;
423 map_fd
= bpf_create_map(BPF_MAP_TYPE_HASH
, sizeof(key
), sizeof(value
),
426 printf("failed to create map for parallel test '%s'\n",
433 /* use the same map_fd in children to add elements to this map
434 * child_0 adds key=0, key=1024, key=2048, ...
435 * child_1 adds key=1, key=1025, key=2049, ...
436 * child_1023 adds key=1023, ...
438 run_parallel(TASKS
, do_work
, data
);
440 /* check that key=0 is already there */
441 assert(bpf_update_elem(map_fd
, &key
, &value
, BPF_NOEXIST
) == -1 &&
444 /* check that all elements were inserted */
446 for (i
= 0; i
< MAP_SIZE
; i
++)
447 assert(bpf_get_next_key(map_fd
, &key
, &key
) == 0);
448 assert(bpf_get_next_key(map_fd
, &key
, &key
) == -1 && errno
== ENOENT
);
450 /* another check for all elements */
451 for (i
= 0; i
< MAP_SIZE
; i
++) {
452 key
= MAP_SIZE
- i
- 1;
453 assert(bpf_lookup_elem(map_fd
, &key
, &value
) == 0 &&
457 /* now let's delete all elemenets in parallel */
459 run_parallel(TASKS
, do_work
, data
);
461 /* nothing should be left */
463 assert(bpf_get_next_key(map_fd
, &key
, &key
) == -1 && errno
== ENOENT
);
468 test_hashmap_sanity(0, NULL
);
469 test_percpu_hashmap_sanity(0, NULL
);
470 test_arraymap_sanity(0, NULL
);
471 test_percpu_arraymap_sanity(0, NULL
);
472 test_percpu_arraymap_many_keys();
477 printf("test_maps: OK\n");