Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Testsuite for eBPF maps
4 *
5 * Copyright (c) 2014 PLUMgrid, http://plumgrid.com
6 * Copyright (c) 2016 Facebook
7 */
8
9#include <stdio.h>
10#include <unistd.h>
11#include <errno.h>
12#include <string.h>
13#include <assert.h>
14#include <stdlib.h>
15#include <time.h>
16
17#include <sys/wait.h>
18#include <sys/socket.h>
19#include <netinet/in.h>
20#include <linux/bpf.h>
21
22#include <bpf/bpf.h>
23#include <bpf/libbpf.h>
24
25#include "bpf_util.h"
26#include "test_maps.h"
27#include "testing_helpers.h"
28
29#ifndef ENOTSUPP
30#define ENOTSUPP 524
31#endif
32
33int skips;
34
35static struct bpf_map_create_opts map_opts = { .sz = sizeof(map_opts) };
36
37static void test_hashmap(unsigned int task, void *data)
38{
39 long long key, next_key, first_key, value;
40 int fd;
41
42 fd = bpf_map_create(BPF_MAP_TYPE_HASH, NULL, sizeof(key), sizeof(value), 2, &map_opts);
43 if (fd < 0) {
44 printf("Failed to create hashmap '%s'!\n", strerror(errno));
45 exit(1);
46 }
47
48 key = 1;
49 value = 1234;
50 /* Insert key=1 element. */
51 assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == 0);
52
53 value = 0;
54 /* BPF_NOEXIST means add new element if it doesn't exist. */
55 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) < 0 &&
56 /* key=1 already exists. */
57 errno == EEXIST);
58
59 /* -1 is an invalid flag. */
60 assert(bpf_map_update_elem(fd, &key, &value, -1) < 0 &&
61 errno == EINVAL);
62
63 /* Check that key=1 can be found. */
64 assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && value == 1234);
65
66 key = 2;
67 value = 1234;
68 /* Insert key=2 element. */
69 assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == 0);
70
71 /* Check that key=2 matches the value and delete it */
72 assert(bpf_map_lookup_and_delete_elem(fd, &key, &value) == 0 && value == 1234);
73
74 /* Check that key=2 is not found. */
75 assert(bpf_map_lookup_elem(fd, &key, &value) < 0 && errno == ENOENT);
76
77 /* BPF_EXIST means update existing element. */
78 assert(bpf_map_update_elem(fd, &key, &value, BPF_EXIST) < 0 &&
79 /* key=2 is not there. */
80 errno == ENOENT);
81
82 /* Insert key=2 element. */
83 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == 0);
84
85 /* key=1 and key=2 were inserted, check that key=0 cannot be
86 * inserted due to max_entries limit.
87 */
88 key = 0;
89 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) < 0 &&
90 errno == E2BIG);
91
92 /* Update existing element, though the map is full. */
93 key = 1;
94 assert(bpf_map_update_elem(fd, &key, &value, BPF_EXIST) == 0);
95 key = 2;
96 assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == 0);
97 key = 3;
98 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) < 0 &&
99 errno == E2BIG);
100
101 /* Check that key = 0 doesn't exist. */
102 key = 0;
103 assert(bpf_map_delete_elem(fd, &key) < 0 && errno == ENOENT);
104
105 /* Iterate over two elements. */
106 assert(bpf_map_get_next_key(fd, NULL, &first_key) == 0 &&
107 (first_key == 1 || first_key == 2));
108 assert(bpf_map_get_next_key(fd, &key, &next_key) == 0 &&
109 (next_key == first_key));
110 assert(bpf_map_get_next_key(fd, &next_key, &next_key) == 0 &&
111 (next_key == 1 || next_key == 2) &&
112 (next_key != first_key));
113 assert(bpf_map_get_next_key(fd, &next_key, &next_key) < 0 &&
114 errno == ENOENT);
115
116 /* Delete both elements. */
117 key = 1;
118 assert(bpf_map_delete_elem(fd, &key) == 0);
119 key = 2;
120 assert(bpf_map_delete_elem(fd, &key) == 0);
121 assert(bpf_map_delete_elem(fd, &key) < 0 && errno == ENOENT);
122
123 key = 0;
124 /* Check that map is empty. */
125 assert(bpf_map_get_next_key(fd, NULL, &next_key) < 0 &&
126 errno == ENOENT);
127 assert(bpf_map_get_next_key(fd, &key, &next_key) < 0 &&
128 errno == ENOENT);
129
130 close(fd);
131}
132
133static void test_hashmap_sizes(unsigned int task, void *data)
134{
135 int fd, i, j;
136
137 for (i = 1; i <= 512; i <<= 1)
138 for (j = 1; j <= 1 << 18; j <<= 1) {
139 fd = bpf_map_create(BPF_MAP_TYPE_HASH, NULL, i, j, 2, &map_opts);
140 if (fd < 0) {
141 if (errno == ENOMEM)
142 return;
143 printf("Failed to create hashmap key=%d value=%d '%s'\n",
144 i, j, strerror(errno));
145 exit(1);
146 }
147 close(fd);
148 usleep(10); /* give kernel time to destroy */
149 }
150}
151
152static void test_hashmap_percpu(unsigned int task, void *data)
153{
154 unsigned int nr_cpus = bpf_num_possible_cpus();
155 BPF_DECLARE_PERCPU(long, value);
156 long long key, next_key, first_key;
157 int expected_key_mask = 0;
158 int fd, i;
159
160 fd = bpf_map_create(BPF_MAP_TYPE_PERCPU_HASH, NULL, sizeof(key),
161 sizeof(bpf_percpu(value, 0)), 2, &map_opts);
162 if (fd < 0) {
163 printf("Failed to create hashmap '%s'!\n", strerror(errno));
164 exit(1);
165 }
166
167 for (i = 0; i < nr_cpus; i++)
168 bpf_percpu(value, i) = i + 100;
169
170 key = 1;
171 /* Insert key=1 element. */
172 assert(!(expected_key_mask & key));
173 assert(bpf_map_update_elem(fd, &key, value, BPF_ANY) == 0);
174
175 /* Lookup and delete elem key=1 and check value. */
176 assert(bpf_map_lookup_and_delete_elem(fd, &key, value) == 0 &&
177 bpf_percpu(value,0) == 100);
178
179 for (i = 0; i < nr_cpus; i++)
180 bpf_percpu(value,i) = i + 100;
181
182 /* Insert key=1 element which should not exist. */
183 assert(bpf_map_update_elem(fd, &key, value, BPF_NOEXIST) == 0);
184 expected_key_mask |= key;
185
186 /* BPF_NOEXIST means add new element if it doesn't exist. */
187 assert(bpf_map_update_elem(fd, &key, value, BPF_NOEXIST) < 0 &&
188 /* key=1 already exists. */
189 errno == EEXIST);
190
191 /* -1 is an invalid flag. */
192 assert(bpf_map_update_elem(fd, &key, value, -1) < 0 &&
193 errno == EINVAL);
194
195 /* Check that key=1 can be found. Value could be 0 if the lookup
196 * was run from a different CPU.
197 */
198 bpf_percpu(value, 0) = 1;
199 assert(bpf_map_lookup_elem(fd, &key, value) == 0 &&
200 bpf_percpu(value, 0) == 100);
201
202 key = 2;
203 /* Check that key=2 is not found. */
204 assert(bpf_map_lookup_elem(fd, &key, value) < 0 && errno == ENOENT);
205
206 /* BPF_EXIST means update existing element. */
207 assert(bpf_map_update_elem(fd, &key, value, BPF_EXIST) < 0 &&
208 /* key=2 is not there. */
209 errno == ENOENT);
210
211 /* Insert key=2 element. */
212 assert(!(expected_key_mask & key));
213 assert(bpf_map_update_elem(fd, &key, value, BPF_NOEXIST) == 0);
214 expected_key_mask |= key;
215
216 /* key=1 and key=2 were inserted, check that key=0 cannot be
217 * inserted due to max_entries limit.
218 */
219 key = 0;
220 assert(bpf_map_update_elem(fd, &key, value, BPF_NOEXIST) < 0 &&
221 errno == E2BIG);
222
223 /* Check that key = 0 doesn't exist. */
224 assert(bpf_map_delete_elem(fd, &key) < 0 && errno == ENOENT);
225
226 /* Iterate over two elements. */
227 assert(bpf_map_get_next_key(fd, NULL, &first_key) == 0 &&
228 ((expected_key_mask & first_key) == first_key));
229 while (!bpf_map_get_next_key(fd, &key, &next_key)) {
230 if (first_key) {
231 assert(next_key == first_key);
232 first_key = 0;
233 }
234 assert((expected_key_mask & next_key) == next_key);
235 expected_key_mask &= ~next_key;
236
237 assert(bpf_map_lookup_elem(fd, &next_key, value) == 0);
238
239 for (i = 0; i < nr_cpus; i++)
240 assert(bpf_percpu(value, i) == i + 100);
241
242 key = next_key;
243 }
244 assert(errno == ENOENT);
245
246 /* Update with BPF_EXIST. */
247 key = 1;
248 assert(bpf_map_update_elem(fd, &key, value, BPF_EXIST) == 0);
249
250 /* Delete both elements. */
251 key = 1;
252 assert(bpf_map_delete_elem(fd, &key) == 0);
253 key = 2;
254 assert(bpf_map_delete_elem(fd, &key) == 0);
255 assert(bpf_map_delete_elem(fd, &key) < 0 && errno == ENOENT);
256
257 key = 0;
258 /* Check that map is empty. */
259 assert(bpf_map_get_next_key(fd, NULL, &next_key) < 0 &&
260 errno == ENOENT);
261 assert(bpf_map_get_next_key(fd, &key, &next_key) < 0 &&
262 errno == ENOENT);
263
264 close(fd);
265}
266
267#define VALUE_SIZE 3
268static int helper_fill_hashmap(int max_entries)
269{
270 int i, fd, ret;
271 long long key, value[VALUE_SIZE] = {};
272
273 fd = bpf_map_create(BPF_MAP_TYPE_HASH, NULL, sizeof(key), sizeof(value),
274 max_entries, &map_opts);
275 CHECK(fd < 0,
276 "failed to create hashmap",
277 "err: %s, flags: 0x%x\n", strerror(errno), map_opts.map_flags);
278
279 for (i = 0; i < max_entries; i++) {
280 key = i; value[0] = key;
281 ret = bpf_map_update_elem(fd, &key, value, BPF_NOEXIST);
282 CHECK(ret != 0,
283 "can't update hashmap",
284 "err: %s\n", strerror(ret));
285 }
286
287 return fd;
288}
289
290static void test_hashmap_walk(unsigned int task, void *data)
291{
292 int fd, i, max_entries = 10000;
293 long long key, value[VALUE_SIZE], next_key;
294 bool next_key_valid = true;
295
296 fd = helper_fill_hashmap(max_entries);
297
298 for (i = 0; bpf_map_get_next_key(fd, !i ? NULL : &key,
299 &next_key) == 0; i++) {
300 key = next_key;
301 assert(bpf_map_lookup_elem(fd, &key, value) == 0);
302 }
303
304 assert(i == max_entries);
305
306 assert(bpf_map_get_next_key(fd, NULL, &key) == 0);
307 for (i = 0; next_key_valid; i++) {
308 next_key_valid = bpf_map_get_next_key(fd, &key, &next_key) == 0;
309 assert(bpf_map_lookup_elem(fd, &key, value) == 0);
310 value[0]++;
311 assert(bpf_map_update_elem(fd, &key, value, BPF_EXIST) == 0);
312 key = next_key;
313 }
314
315 assert(i == max_entries);
316
317 for (i = 0; bpf_map_get_next_key(fd, !i ? NULL : &key,
318 &next_key) == 0; i++) {
319 key = next_key;
320 assert(bpf_map_lookup_elem(fd, &key, value) == 0);
321 assert(value[0] - 1 == key);
322 }
323
324 assert(i == max_entries);
325 close(fd);
326}
327
328static void test_hashmap_zero_seed(void)
329{
330 int i, first, second, old_flags;
331 long long key, next_first, next_second;
332
333 old_flags = map_opts.map_flags;
334 map_opts.map_flags |= BPF_F_ZERO_SEED;
335
336 first = helper_fill_hashmap(3);
337 second = helper_fill_hashmap(3);
338
339 for (i = 0; ; i++) {
340 void *key_ptr = !i ? NULL : &key;
341
342 if (bpf_map_get_next_key(first, key_ptr, &next_first) != 0)
343 break;
344
345 CHECK(bpf_map_get_next_key(second, key_ptr, &next_second) != 0,
346 "next_key for second map must succeed",
347 "key_ptr: %p", key_ptr);
348 CHECK(next_first != next_second,
349 "keys must match",
350 "i: %d first: %lld second: %lld\n", i,
351 next_first, next_second);
352
353 key = next_first;
354 }
355
356 map_opts.map_flags = old_flags;
357 close(first);
358 close(second);
359}
360
361static void test_arraymap(unsigned int task, void *data)
362{
363 int key, next_key, fd;
364 long long value;
365
366 fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, NULL, sizeof(key), sizeof(value), 2, NULL);
367 if (fd < 0) {
368 printf("Failed to create arraymap '%s'!\n", strerror(errno));
369 exit(1);
370 }
371
372 key = 1;
373 value = 1234;
374 /* Insert key=1 element. */
375 assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == 0);
376
377 value = 0;
378 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) < 0 &&
379 errno == EEXIST);
380
381 /* Check that key=1 can be found. */
382 assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && value == 1234);
383
384 key = 0;
385 /* Check that key=0 is also found and zero initialized. */
386 assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && value == 0);
387
388 /* key=0 and key=1 were inserted, check that key=2 cannot be inserted
389 * due to max_entries limit.
390 */
391 key = 2;
392 assert(bpf_map_update_elem(fd, &key, &value, BPF_EXIST) < 0 &&
393 errno == E2BIG);
394
395 /* Check that key = 2 doesn't exist. */
396 assert(bpf_map_lookup_elem(fd, &key, &value) < 0 && errno == ENOENT);
397
398 /* Iterate over two elements. */
399 assert(bpf_map_get_next_key(fd, NULL, &next_key) == 0 &&
400 next_key == 0);
401 assert(bpf_map_get_next_key(fd, &key, &next_key) == 0 &&
402 next_key == 0);
403 assert(bpf_map_get_next_key(fd, &next_key, &next_key) == 0 &&
404 next_key == 1);
405 assert(bpf_map_get_next_key(fd, &next_key, &next_key) < 0 &&
406 errno == ENOENT);
407
408 /* Delete shouldn't succeed. */
409 key = 1;
410 assert(bpf_map_delete_elem(fd, &key) < 0 && errno == EINVAL);
411
412 close(fd);
413}
414
415static void test_arraymap_percpu(unsigned int task, void *data)
416{
417 unsigned int nr_cpus = bpf_num_possible_cpus();
418 BPF_DECLARE_PERCPU(long, values);
419 int key, next_key, fd, i;
420
421 fd = bpf_map_create(BPF_MAP_TYPE_PERCPU_ARRAY, NULL, sizeof(key),
422 sizeof(bpf_percpu(values, 0)), 2, NULL);
423 if (fd < 0) {
424 printf("Failed to create arraymap '%s'!\n", strerror(errno));
425 exit(1);
426 }
427
428 for (i = 0; i < nr_cpus; i++)
429 bpf_percpu(values, i) = i + 100;
430
431 key = 1;
432 /* Insert key=1 element. */
433 assert(bpf_map_update_elem(fd, &key, values, BPF_ANY) == 0);
434
435 bpf_percpu(values, 0) = 0;
436 assert(bpf_map_update_elem(fd, &key, values, BPF_NOEXIST) < 0 &&
437 errno == EEXIST);
438
439 /* Check that key=1 can be found. */
440 assert(bpf_map_lookup_elem(fd, &key, values) == 0 &&
441 bpf_percpu(values, 0) == 100);
442
443 key = 0;
444 /* Check that key=0 is also found and zero initialized. */
445 assert(bpf_map_lookup_elem(fd, &key, values) == 0 &&
446 bpf_percpu(values, 0) == 0 &&
447 bpf_percpu(values, nr_cpus - 1) == 0);
448
449 /* Check that key=2 cannot be inserted due to max_entries limit. */
450 key = 2;
451 assert(bpf_map_update_elem(fd, &key, values, BPF_EXIST) < 0 &&
452 errno == E2BIG);
453
454 /* Check that key = 2 doesn't exist. */
455 assert(bpf_map_lookup_elem(fd, &key, values) < 0 && errno == ENOENT);
456
457 /* Iterate over two elements. */
458 assert(bpf_map_get_next_key(fd, NULL, &next_key) == 0 &&
459 next_key == 0);
460 assert(bpf_map_get_next_key(fd, &key, &next_key) == 0 &&
461 next_key == 0);
462 assert(bpf_map_get_next_key(fd, &next_key, &next_key) == 0 &&
463 next_key == 1);
464 assert(bpf_map_get_next_key(fd, &next_key, &next_key) < 0 &&
465 errno == ENOENT);
466
467 /* Delete shouldn't succeed. */
468 key = 1;
469 assert(bpf_map_delete_elem(fd, &key) < 0 && errno == EINVAL);
470
471 close(fd);
472}
473
474static void test_arraymap_percpu_many_keys(void)
475{
476 unsigned int nr_cpus = bpf_num_possible_cpus();
477 BPF_DECLARE_PERCPU(long, values);
478 /* nr_keys is not too large otherwise the test stresses percpu
479 * allocator more than anything else
480 */
481 unsigned int nr_keys = 2000;
482 int key, fd, i;
483
484 fd = bpf_map_create(BPF_MAP_TYPE_PERCPU_ARRAY, NULL, sizeof(key),
485 sizeof(bpf_percpu(values, 0)), nr_keys, NULL);
486 if (fd < 0) {
487 printf("Failed to create per-cpu arraymap '%s'!\n",
488 strerror(errno));
489 exit(1);
490 }
491
492 for (i = 0; i < nr_cpus; i++)
493 bpf_percpu(values, i) = i + 10;
494
495 for (key = 0; key < nr_keys; key++)
496 assert(bpf_map_update_elem(fd, &key, values, BPF_ANY) == 0);
497
498 for (key = 0; key < nr_keys; key++) {
499 for (i = 0; i < nr_cpus; i++)
500 bpf_percpu(values, i) = 0;
501
502 assert(bpf_map_lookup_elem(fd, &key, values) == 0);
503
504 for (i = 0; i < nr_cpus; i++)
505 assert(bpf_percpu(values, i) == i + 10);
506 }
507
508 close(fd);
509}
510
511static void test_devmap(unsigned int task, void *data)
512{
513 int fd;
514 __u32 key, value;
515
516 fd = bpf_map_create(BPF_MAP_TYPE_DEVMAP, NULL, sizeof(key), sizeof(value), 2, NULL);
517 if (fd < 0) {
518 printf("Failed to create devmap '%s'!\n", strerror(errno));
519 exit(1);
520 }
521
522 close(fd);
523}
524
525static void test_devmap_hash(unsigned int task, void *data)
526{
527 int fd;
528 __u32 key, value;
529
530 fd = bpf_map_create(BPF_MAP_TYPE_DEVMAP_HASH, NULL, sizeof(key), sizeof(value), 2, NULL);
531 if (fd < 0) {
532 printf("Failed to create devmap_hash '%s'!\n", strerror(errno));
533 exit(1);
534 }
535
536 close(fd);
537}
538
539static void test_queuemap(unsigned int task, void *data)
540{
541 const int MAP_SIZE = 32;
542 __u32 vals[MAP_SIZE + MAP_SIZE/2], val;
543 int fd, i;
544
545 /* Fill test values to be used */
546 for (i = 0; i < MAP_SIZE + MAP_SIZE/2; i++)
547 vals[i] = rand();
548
549 /* Invalid key size */
550 fd = bpf_map_create(BPF_MAP_TYPE_QUEUE, NULL, 4, sizeof(val), MAP_SIZE, &map_opts);
551 assert(fd < 0 && errno == EINVAL);
552
553 fd = bpf_map_create(BPF_MAP_TYPE_QUEUE, NULL, 0, sizeof(val), MAP_SIZE, &map_opts);
554 /* Queue map does not support BPF_F_NO_PREALLOC */
555 if (map_opts.map_flags & BPF_F_NO_PREALLOC) {
556 assert(fd < 0 && errno == EINVAL);
557 return;
558 }
559 if (fd < 0) {
560 printf("Failed to create queuemap '%s'!\n", strerror(errno));
561 exit(1);
562 }
563
564 /* Push MAP_SIZE elements */
565 for (i = 0; i < MAP_SIZE; i++)
566 assert(bpf_map_update_elem(fd, NULL, &vals[i], 0) == 0);
567
568 /* Check that element cannot be pushed due to max_entries limit */
569 assert(bpf_map_update_elem(fd, NULL, &val, 0) < 0 &&
570 errno == E2BIG);
571
572 /* Peek element */
573 assert(bpf_map_lookup_elem(fd, NULL, &val) == 0 && val == vals[0]);
574
575 /* Replace half elements */
576 for (i = MAP_SIZE; i < MAP_SIZE + MAP_SIZE/2; i++)
577 assert(bpf_map_update_elem(fd, NULL, &vals[i], BPF_EXIST) == 0);
578
579 /* Pop all elements */
580 for (i = MAP_SIZE/2; i < MAP_SIZE + MAP_SIZE/2; i++)
581 assert(bpf_map_lookup_and_delete_elem(fd, NULL, &val) == 0 &&
582 val == vals[i]);
583
584 /* Check that there are not elements left */
585 assert(bpf_map_lookup_and_delete_elem(fd, NULL, &val) < 0 &&
586 errno == ENOENT);
587
588 /* Check that non supported functions set errno to EINVAL */
589 assert(bpf_map_delete_elem(fd, NULL) < 0 && errno == EINVAL);
590 assert(bpf_map_get_next_key(fd, NULL, NULL) < 0 && errno == EINVAL);
591
592 close(fd);
593}
594
595static void test_stackmap(unsigned int task, void *data)
596{
597 const int MAP_SIZE = 32;
598 __u32 vals[MAP_SIZE + MAP_SIZE/2], val;
599 int fd, i;
600
601 /* Fill test values to be used */
602 for (i = 0; i < MAP_SIZE + MAP_SIZE/2; i++)
603 vals[i] = rand();
604
605 /* Invalid key size */
606 fd = bpf_map_create(BPF_MAP_TYPE_STACK, NULL, 4, sizeof(val), MAP_SIZE, &map_opts);
607 assert(fd < 0 && errno == EINVAL);
608
609 fd = bpf_map_create(BPF_MAP_TYPE_STACK, NULL, 0, sizeof(val), MAP_SIZE, &map_opts);
610 /* Stack map does not support BPF_F_NO_PREALLOC */
611 if (map_opts.map_flags & BPF_F_NO_PREALLOC) {
612 assert(fd < 0 && errno == EINVAL);
613 return;
614 }
615 if (fd < 0) {
616 printf("Failed to create stackmap '%s'!\n", strerror(errno));
617 exit(1);
618 }
619
620 /* Push MAP_SIZE elements */
621 for (i = 0; i < MAP_SIZE; i++)
622 assert(bpf_map_update_elem(fd, NULL, &vals[i], 0) == 0);
623
624 /* Check that element cannot be pushed due to max_entries limit */
625 assert(bpf_map_update_elem(fd, NULL, &val, 0) < 0 &&
626 errno == E2BIG);
627
628 /* Peek element */
629 assert(bpf_map_lookup_elem(fd, NULL, &val) == 0 && val == vals[i - 1]);
630
631 /* Replace half elements */
632 for (i = MAP_SIZE; i < MAP_SIZE + MAP_SIZE/2; i++)
633 assert(bpf_map_update_elem(fd, NULL, &vals[i], BPF_EXIST) == 0);
634
635 /* Pop all elements */
636 for (i = MAP_SIZE + MAP_SIZE/2 - 1; i >= MAP_SIZE/2; i--)
637 assert(bpf_map_lookup_and_delete_elem(fd, NULL, &val) == 0 &&
638 val == vals[i]);
639
640 /* Check that there are not elements left */
641 assert(bpf_map_lookup_and_delete_elem(fd, NULL, &val) < 0 &&
642 errno == ENOENT);
643
644 /* Check that non supported functions set errno to EINVAL */
645 assert(bpf_map_delete_elem(fd, NULL) < 0 && errno == EINVAL);
646 assert(bpf_map_get_next_key(fd, NULL, NULL) < 0 && errno == EINVAL);
647
648 close(fd);
649}
650
651#include <sys/ioctl.h>
652#include <arpa/inet.h>
653#include <sys/select.h>
654#include <linux/err.h>
655#define SOCKMAP_PARSE_PROG "./sockmap_parse_prog.bpf.o"
656#define SOCKMAP_VERDICT_PROG "./sockmap_verdict_prog.bpf.o"
657#define SOCKMAP_TCP_MSG_PROG "./sockmap_tcp_msg_prog.bpf.o"
658static void test_sockmap(unsigned int tasks, void *data)
659{
660 struct bpf_map *bpf_map_rx, *bpf_map_tx, *bpf_map_msg, *bpf_map_break;
661 int map_fd_msg = 0, map_fd_rx = 0, map_fd_tx = 0, map_fd_break;
662 struct bpf_object *parse_obj, *verdict_obj, *msg_obj;
663 int ports[] = {50200, 50201, 50202, 50204};
664 int err, i, fd, udp, sfd[6] = {0xdeadbeef};
665 u8 buf[20] = {0x0, 0x5, 0x3, 0x2, 0x1, 0x0};
666 int parse_prog, verdict_prog, msg_prog;
667 struct sockaddr_in addr;
668 int one = 1, s, sc, rc;
669 struct timeval to;
670 __u32 key, value;
671 pid_t pid[tasks];
672 fd_set w;
673
674 /* Create some sockets to use with sockmap */
675 for (i = 0; i < 2; i++) {
676 sfd[i] = socket(AF_INET, SOCK_STREAM, 0);
677 if (sfd[i] < 0)
678 goto out;
679 err = setsockopt(sfd[i], SOL_SOCKET, SO_REUSEADDR,
680 (char *)&one, sizeof(one));
681 if (err) {
682 printf("failed to setsockopt\n");
683 goto out;
684 }
685 err = ioctl(sfd[i], FIONBIO, (char *)&one);
686 if (err < 0) {
687 printf("failed to ioctl\n");
688 goto out;
689 }
690 memset(&addr, 0, sizeof(struct sockaddr_in));
691 addr.sin_family = AF_INET;
692 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
693 addr.sin_port = htons(ports[i]);
694 err = bind(sfd[i], (struct sockaddr *)&addr, sizeof(addr));
695 if (err < 0) {
696 printf("failed to bind: err %i: %i:%i\n",
697 err, i, sfd[i]);
698 goto out;
699 }
700 err = listen(sfd[i], 32);
701 if (err < 0) {
702 printf("failed to listen\n");
703 goto out;
704 }
705 }
706
707 for (i = 2; i < 4; i++) {
708 sfd[i] = socket(AF_INET, SOCK_STREAM, 0);
709 if (sfd[i] < 0)
710 goto out;
711 err = setsockopt(sfd[i], SOL_SOCKET, SO_REUSEADDR,
712 (char *)&one, sizeof(one));
713 if (err) {
714 printf("set sock opt\n");
715 goto out;
716 }
717 memset(&addr, 0, sizeof(struct sockaddr_in));
718 addr.sin_family = AF_INET;
719 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
720 addr.sin_port = htons(ports[i - 2]);
721 err = connect(sfd[i], (struct sockaddr *)&addr, sizeof(addr));
722 if (err) {
723 printf("failed to connect\n");
724 goto out;
725 }
726 }
727
728
729 for (i = 4; i < 6; i++) {
730 sfd[i] = accept(sfd[i - 4], NULL, NULL);
731 if (sfd[i] < 0) {
732 printf("accept failed\n");
733 goto out;
734 }
735 }
736
737 /* Test sockmap with connected sockets */
738 fd = bpf_map_create(BPF_MAP_TYPE_SOCKMAP, NULL,
739 sizeof(key), sizeof(value),
740 6, NULL);
741 if (fd < 0) {
742 if (!libbpf_probe_bpf_map_type(BPF_MAP_TYPE_SOCKMAP, NULL)) {
743 printf("%s SKIP (unsupported map type BPF_MAP_TYPE_SOCKMAP)\n",
744 __func__);
745 skips++;
746 for (i = 0; i < 6; i++)
747 close(sfd[i]);
748 return;
749 }
750
751 printf("Failed to create sockmap %i\n", fd);
752 goto out_sockmap;
753 }
754
755 /* Test update with unsupported UDP socket */
756 udp = socket(AF_INET, SOCK_DGRAM, 0);
757 i = 0;
758 err = bpf_map_update_elem(fd, &i, &udp, BPF_ANY);
759 if (err) {
760 printf("Failed socket update SOCK_DGRAM '%i:%i'\n",
761 i, udp);
762 goto out_sockmap;
763 }
764 close(udp);
765
766 /* Test update without programs */
767 for (i = 0; i < 6; i++) {
768 err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_ANY);
769 if (err) {
770 printf("Failed noprog update sockmap '%i:%i'\n",
771 i, sfd[i]);
772 goto out_sockmap;
773 }
774 }
775
776 /* Test attaching/detaching bad fds */
777 err = bpf_prog_attach(-1, fd, BPF_SK_SKB_STREAM_PARSER, 0);
778 if (!err) {
779 printf("Failed invalid parser prog attach\n");
780 goto out_sockmap;
781 }
782
783 err = bpf_prog_attach(-1, fd, BPF_SK_SKB_STREAM_VERDICT, 0);
784 if (!err) {
785 printf("Failed invalid verdict prog attach\n");
786 goto out_sockmap;
787 }
788
789 err = bpf_prog_attach(-1, fd, BPF_SK_MSG_VERDICT, 0);
790 if (!err) {
791 printf("Failed invalid msg verdict prog attach\n");
792 goto out_sockmap;
793 }
794
795 err = bpf_prog_attach(-1, fd, __MAX_BPF_ATTACH_TYPE, 0);
796 if (!err) {
797 printf("Failed unknown prog attach\n");
798 goto out_sockmap;
799 }
800
801 err = bpf_prog_detach(fd, BPF_SK_SKB_STREAM_PARSER);
802 if (!err) {
803 printf("Failed empty parser prog detach\n");
804 goto out_sockmap;
805 }
806
807 err = bpf_prog_detach(fd, BPF_SK_SKB_STREAM_VERDICT);
808 if (!err) {
809 printf("Failed empty verdict prog detach\n");
810 goto out_sockmap;
811 }
812
813 err = bpf_prog_detach(fd, BPF_SK_MSG_VERDICT);
814 if (!err) {
815 printf("Failed empty msg verdict prog detach\n");
816 goto out_sockmap;
817 }
818
819 err = bpf_prog_detach(fd, __MAX_BPF_ATTACH_TYPE);
820 if (!err) {
821 printf("Detach invalid prog successful\n");
822 goto out_sockmap;
823 }
824
825 /* Load SK_SKB program and Attach */
826 err = bpf_prog_test_load(SOCKMAP_PARSE_PROG,
827 BPF_PROG_TYPE_SK_SKB, &parse_obj, &parse_prog);
828 if (err) {
829 printf("Failed to load SK_SKB parse prog\n");
830 goto out_sockmap;
831 }
832
833 err = bpf_prog_test_load(SOCKMAP_TCP_MSG_PROG,
834 BPF_PROG_TYPE_SK_MSG, &msg_obj, &msg_prog);
835 if (err) {
836 printf("Failed to load SK_SKB msg prog\n");
837 goto out_sockmap;
838 }
839
840 err = bpf_prog_test_load(SOCKMAP_VERDICT_PROG,
841 BPF_PROG_TYPE_SK_SKB, &verdict_obj, &verdict_prog);
842 if (err) {
843 printf("Failed to load SK_SKB verdict prog\n");
844 goto out_sockmap;
845 }
846
847 bpf_map_rx = bpf_object__find_map_by_name(verdict_obj, "sock_map_rx");
848 if (!bpf_map_rx) {
849 printf("Failed to load map rx from verdict prog\n");
850 goto out_sockmap;
851 }
852
853 map_fd_rx = bpf_map__fd(bpf_map_rx);
854 if (map_fd_rx < 0) {
855 printf("Failed to get map rx fd\n");
856 goto out_sockmap;
857 }
858
859 bpf_map_tx = bpf_object__find_map_by_name(verdict_obj, "sock_map_tx");
860 if (!bpf_map_tx) {
861 printf("Failed to load map tx from verdict prog\n");
862 goto out_sockmap;
863 }
864
865 map_fd_tx = bpf_map__fd(bpf_map_tx);
866 if (map_fd_tx < 0) {
867 printf("Failed to get map tx fd\n");
868 goto out_sockmap;
869 }
870
871 bpf_map_msg = bpf_object__find_map_by_name(verdict_obj, "sock_map_msg");
872 if (!bpf_map_msg) {
873 printf("Failed to load map msg from msg_verdict prog\n");
874 goto out_sockmap;
875 }
876
877 map_fd_msg = bpf_map__fd(bpf_map_msg);
878 if (map_fd_msg < 0) {
879 printf("Failed to get map msg fd\n");
880 goto out_sockmap;
881 }
882
883 bpf_map_break = bpf_object__find_map_by_name(verdict_obj, "sock_map_break");
884 if (!bpf_map_break) {
885 printf("Failed to load map tx from verdict prog\n");
886 goto out_sockmap;
887 }
888
889 map_fd_break = bpf_map__fd(bpf_map_break);
890 if (map_fd_break < 0) {
891 printf("Failed to get map tx fd\n");
892 goto out_sockmap;
893 }
894
895 err = bpf_prog_attach(parse_prog, map_fd_break,
896 BPF_SK_SKB_STREAM_PARSER, 0);
897 if (!err) {
898 printf("Allowed attaching SK_SKB program to invalid map\n");
899 goto out_sockmap;
900 }
901
902 err = bpf_prog_attach(parse_prog, map_fd_rx,
903 BPF_SK_SKB_STREAM_PARSER, 0);
904 if (err) {
905 printf("Failed stream parser bpf prog attach\n");
906 goto out_sockmap;
907 }
908
909 err = bpf_prog_attach(verdict_prog, map_fd_rx,
910 BPF_SK_SKB_STREAM_VERDICT, 0);
911 if (err) {
912 printf("Failed stream verdict bpf prog attach\n");
913 goto out_sockmap;
914 }
915
916 err = bpf_prog_attach(msg_prog, map_fd_msg, BPF_SK_MSG_VERDICT, 0);
917 if (err) {
918 printf("Failed msg verdict bpf prog attach\n");
919 goto out_sockmap;
920 }
921
922 err = bpf_prog_attach(verdict_prog, map_fd_rx,
923 __MAX_BPF_ATTACH_TYPE, 0);
924 if (!err) {
925 printf("Attached unknown bpf prog\n");
926 goto out_sockmap;
927 }
928
929 /* Test map update elem afterwards fd lives in fd and map_fd */
930 for (i = 2; i < 6; i++) {
931 err = bpf_map_update_elem(map_fd_rx, &i, &sfd[i], BPF_ANY);
932 if (err) {
933 printf("Failed map_fd_rx update sockmap %i '%i:%i'\n",
934 err, i, sfd[i]);
935 goto out_sockmap;
936 }
937 err = bpf_map_update_elem(map_fd_tx, &i, &sfd[i], BPF_ANY);
938 if (err) {
939 printf("Failed map_fd_tx update sockmap %i '%i:%i'\n",
940 err, i, sfd[i]);
941 goto out_sockmap;
942 }
943 }
944
945 /* Test map delete elem and remove send/recv sockets */
946 for (i = 2; i < 4; i++) {
947 err = bpf_map_delete_elem(map_fd_rx, &i);
948 if (err) {
949 printf("Failed delete sockmap rx %i '%i:%i'\n",
950 err, i, sfd[i]);
951 goto out_sockmap;
952 }
953 err = bpf_map_delete_elem(map_fd_tx, &i);
954 if (err) {
955 printf("Failed delete sockmap tx %i '%i:%i'\n",
956 err, i, sfd[i]);
957 goto out_sockmap;
958 }
959 }
960
961 /* Put sfd[2] (sending fd below) into msg map to test sendmsg bpf */
962 i = 0;
963 err = bpf_map_update_elem(map_fd_msg, &i, &sfd[2], BPF_ANY);
964 if (err) {
965 printf("Failed map_fd_msg update sockmap %i\n", err);
966 goto out_sockmap;
967 }
968
969 /* Test map send/recv */
970 for (i = 0; i < 2; i++) {
971 buf[0] = i;
972 buf[1] = 0x5;
973 sc = send(sfd[2], buf, 20, 0);
974 if (sc < 0) {
975 printf("Failed sockmap send\n");
976 goto out_sockmap;
977 }
978
979 FD_ZERO(&w);
980 FD_SET(sfd[3], &w);
981 to.tv_sec = 30;
982 to.tv_usec = 0;
983 s = select(sfd[3] + 1, &w, NULL, NULL, &to);
984 if (s == -1) {
985 perror("Failed sockmap select()");
986 goto out_sockmap;
987 } else if (!s) {
988 printf("Failed sockmap unexpected timeout\n");
989 goto out_sockmap;
990 }
991
992 if (!FD_ISSET(sfd[3], &w)) {
993 printf("Failed sockmap select/recv\n");
994 goto out_sockmap;
995 }
996
997 rc = recv(sfd[3], buf, sizeof(buf), 0);
998 if (rc < 0) {
999 printf("Failed sockmap recv\n");
1000 goto out_sockmap;
1001 }
1002 }
1003
1004 /* Negative null entry lookup from datapath should be dropped */
1005 buf[0] = 1;
1006 buf[1] = 12;
1007 sc = send(sfd[2], buf, 20, 0);
1008 if (sc < 0) {
1009 printf("Failed sockmap send\n");
1010 goto out_sockmap;
1011 }
1012
1013 /* Push fd into same slot */
1014 i = 2;
1015 err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_NOEXIST);
1016 if (!err) {
1017 printf("Failed allowed sockmap dup slot BPF_NOEXIST\n");
1018 goto out_sockmap;
1019 }
1020
1021 err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_ANY);
1022 if (err) {
1023 printf("Failed sockmap update new slot BPF_ANY\n");
1024 goto out_sockmap;
1025 }
1026
1027 err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_EXIST);
1028 if (err) {
1029 printf("Failed sockmap update new slot BPF_EXIST\n");
1030 goto out_sockmap;
1031 }
1032
1033 /* Delete the elems without programs */
1034 for (i = 2; i < 6; i++) {
1035 err = bpf_map_delete_elem(fd, &i);
1036 if (err) {
1037 printf("Failed delete sockmap %i '%i:%i'\n",
1038 err, i, sfd[i]);
1039 }
1040 }
1041
1042 /* Test having multiple maps open and set with programs on same fds */
1043 err = bpf_prog_attach(parse_prog, fd,
1044 BPF_SK_SKB_STREAM_PARSER, 0);
1045 if (err) {
1046 printf("Failed fd bpf parse prog attach\n");
1047 goto out_sockmap;
1048 }
1049 err = bpf_prog_attach(verdict_prog, fd,
1050 BPF_SK_SKB_STREAM_VERDICT, 0);
1051 if (err) {
1052 printf("Failed fd bpf verdict prog attach\n");
1053 goto out_sockmap;
1054 }
1055
1056 for (i = 4; i < 6; i++) {
1057 err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_ANY);
1058 if (!err) {
1059 printf("Failed allowed duplicate programs in update ANY sockmap %i '%i:%i'\n",
1060 err, i, sfd[i]);
1061 goto out_sockmap;
1062 }
1063 err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_NOEXIST);
1064 if (!err) {
1065 printf("Failed allowed duplicate program in update NOEXIST sockmap %i '%i:%i'\n",
1066 err, i, sfd[i]);
1067 goto out_sockmap;
1068 }
1069 err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_EXIST);
1070 if (!err) {
1071 printf("Failed allowed duplicate program in update EXIST sockmap %i '%i:%i'\n",
1072 err, i, sfd[i]);
1073 goto out_sockmap;
1074 }
1075 }
1076
1077 /* Test tasks number of forked operations */
1078 for (i = 0; i < tasks; i++) {
1079 pid[i] = fork();
1080 if (pid[i] == 0) {
1081 for (i = 0; i < 6; i++) {
1082 bpf_map_delete_elem(map_fd_tx, &i);
1083 bpf_map_delete_elem(map_fd_rx, &i);
1084 bpf_map_update_elem(map_fd_tx, &i,
1085 &sfd[i], BPF_ANY);
1086 bpf_map_update_elem(map_fd_rx, &i,
1087 &sfd[i], BPF_ANY);
1088 }
1089 exit(0);
1090 } else if (pid[i] == -1) {
1091 printf("Couldn't spawn #%d process!\n", i);
1092 exit(1);
1093 }
1094 }
1095
1096 for (i = 0; i < tasks; i++) {
1097 int status;
1098
1099 assert(waitpid(pid[i], &status, 0) == pid[i]);
1100 assert(status == 0);
1101 }
1102
1103 err = bpf_prog_detach2(parse_prog, map_fd_rx, __MAX_BPF_ATTACH_TYPE);
1104 if (!err) {
1105 printf("Detached an invalid prog type.\n");
1106 goto out_sockmap;
1107 }
1108
1109 err = bpf_prog_detach2(parse_prog, map_fd_rx, BPF_SK_SKB_STREAM_PARSER);
1110 if (err) {
1111 printf("Failed parser prog detach\n");
1112 goto out_sockmap;
1113 }
1114
1115 err = bpf_prog_detach2(verdict_prog, map_fd_rx, BPF_SK_SKB_STREAM_VERDICT);
1116 if (err) {
1117 printf("Failed parser prog detach\n");
1118 goto out_sockmap;
1119 }
1120
1121 /* Test map close sockets and empty maps */
1122 for (i = 0; i < 6; i++) {
1123 bpf_map_delete_elem(map_fd_tx, &i);
1124 bpf_map_delete_elem(map_fd_rx, &i);
1125 close(sfd[i]);
1126 }
1127 close(fd);
1128 close(map_fd_rx);
1129 bpf_object__close(parse_obj);
1130 bpf_object__close(msg_obj);
1131 bpf_object__close(verdict_obj);
1132 return;
1133out:
1134 for (i = 0; i < 6; i++)
1135 close(sfd[i]);
1136 printf("Failed to create sockmap '%i:%s'!\n", i, strerror(errno));
1137 exit(1);
1138out_sockmap:
1139 for (i = 0; i < 6; i++) {
1140 if (map_fd_tx)
1141 bpf_map_delete_elem(map_fd_tx, &i);
1142 if (map_fd_rx)
1143 bpf_map_delete_elem(map_fd_rx, &i);
1144 close(sfd[i]);
1145 }
1146 close(fd);
1147 exit(1);
1148}
1149
1150#define MAPINMAP_PROG "./test_map_in_map.bpf.o"
1151#define MAPINMAP_INVALID_PROG "./test_map_in_map_invalid.bpf.o"
1152static void test_map_in_map(void)
1153{
1154 struct bpf_object *obj;
1155 struct bpf_map *map;
1156 int mim_fd, fd, err;
1157 int pos = 0;
1158 struct bpf_map_info info = {};
1159 __u32 len = sizeof(info);
1160 __u32 id = 0;
1161 libbpf_print_fn_t old_print_fn;
1162
1163 obj = bpf_object__open(MAPINMAP_PROG);
1164
1165 fd = bpf_map_create(BPF_MAP_TYPE_HASH, NULL, sizeof(int), sizeof(int), 2, NULL);
1166 if (fd < 0) {
1167 printf("Failed to create hashmap '%s'!\n", strerror(errno));
1168 exit(1);
1169 }
1170
1171 map = bpf_object__find_map_by_name(obj, "mim_array");
1172 if (!map) {
1173 printf("Failed to load array of maps from test prog\n");
1174 goto out_map_in_map;
1175 }
1176 err = bpf_map__set_inner_map_fd(map, fd);
1177 if (err) {
1178 printf("Failed to set inner_map_fd for array of maps\n");
1179 goto out_map_in_map;
1180 }
1181
1182 map = bpf_object__find_map_by_name(obj, "mim_hash");
1183 if (!map) {
1184 printf("Failed to load hash of maps from test prog\n");
1185 goto out_map_in_map;
1186 }
1187 err = bpf_map__set_inner_map_fd(map, fd);
1188 if (err) {
1189 printf("Failed to set inner_map_fd for hash of maps\n");
1190 goto out_map_in_map;
1191 }
1192
1193 bpf_object__load(obj);
1194
1195 map = bpf_object__find_map_by_name(obj, "mim_array");
1196 if (!map) {
1197 printf("Failed to load array of maps from test prog\n");
1198 goto out_map_in_map;
1199 }
1200 mim_fd = bpf_map__fd(map);
1201 if (mim_fd < 0) {
1202 printf("Failed to get descriptor for array of maps\n");
1203 goto out_map_in_map;
1204 }
1205
1206 err = bpf_map_update_elem(mim_fd, &pos, &fd, 0);
1207 if (err) {
1208 printf("Failed to update array of maps\n");
1209 goto out_map_in_map;
1210 }
1211
1212 map = bpf_object__find_map_by_name(obj, "mim_hash");
1213 if (!map) {
1214 printf("Failed to load hash of maps from test prog\n");
1215 goto out_map_in_map;
1216 }
1217 mim_fd = bpf_map__fd(map);
1218 if (mim_fd < 0) {
1219 printf("Failed to get descriptor for hash of maps\n");
1220 goto out_map_in_map;
1221 }
1222
1223 err = bpf_map_update_elem(mim_fd, &pos, &fd, 0);
1224 if (err) {
1225 printf("Failed to update hash of maps\n");
1226 goto out_map_in_map;
1227 }
1228
1229 close(fd);
1230 fd = -1;
1231 bpf_object__close(obj);
1232
1233 /* Test that failing bpf_object__create_map() destroys the inner map */
1234 obj = bpf_object__open(MAPINMAP_INVALID_PROG);
1235 err = libbpf_get_error(obj);
1236 if (err) {
1237 printf("Failed to load %s program: %d %d",
1238 MAPINMAP_INVALID_PROG, err, errno);
1239 goto out_map_in_map;
1240 }
1241
1242 map = bpf_object__find_map_by_name(obj, "mim");
1243 if (!map) {
1244 printf("Failed to load array of maps from test prog\n");
1245 goto out_map_in_map;
1246 }
1247
1248 old_print_fn = libbpf_set_print(NULL);
1249
1250 err = bpf_object__load(obj);
1251 if (!err) {
1252 printf("Loading obj supposed to fail\n");
1253 goto out_map_in_map;
1254 }
1255
1256 libbpf_set_print(old_print_fn);
1257
1258 /* Iterate over all maps to check whether the internal map
1259 * ("mim.internal") has been destroyed.
1260 */
1261 while (true) {
1262 err = bpf_map_get_next_id(id, &id);
1263 if (err) {
1264 if (errno == ENOENT)
1265 break;
1266 printf("Failed to get next map: %d", errno);
1267 goto out_map_in_map;
1268 }
1269
1270 fd = bpf_map_get_fd_by_id(id);
1271 if (fd < 0) {
1272 if (errno == ENOENT)
1273 continue;
1274 printf("Failed to get map by id %u: %d", id, errno);
1275 goto out_map_in_map;
1276 }
1277
1278 err = bpf_map_get_info_by_fd(fd, &info, &len);
1279 if (err) {
1280 printf("Failed to get map info by fd %d: %d", fd,
1281 errno);
1282 goto out_map_in_map;
1283 }
1284
1285 if (!strcmp(info.name, "mim.inner")) {
1286 printf("Inner map mim.inner was not destroyed\n");
1287 goto out_map_in_map;
1288 }
1289
1290 close(fd);
1291 }
1292
1293 bpf_object__close(obj);
1294 return;
1295
1296out_map_in_map:
1297 if (fd >= 0)
1298 close(fd);
1299 exit(1);
1300}
1301
1302#define MAP_SIZE (32 * 1024)
1303
1304static void test_map_large(void)
1305{
1306
1307 struct bigkey {
1308 int a;
1309 char b[4096];
1310 long long c;
1311 } key;
1312 int fd, i, value;
1313
1314 fd = bpf_map_create(BPF_MAP_TYPE_HASH, NULL, sizeof(key), sizeof(value),
1315 MAP_SIZE, &map_opts);
1316 if (fd < 0) {
1317 printf("Failed to create large map '%s'!\n", strerror(errno));
1318 exit(1);
1319 }
1320
1321 for (i = 0; i < MAP_SIZE; i++) {
1322 key = (struct bigkey) { .c = i };
1323 value = i;
1324
1325 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == 0);
1326 }
1327
1328 key.c = -1;
1329 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) < 0 &&
1330 errno == E2BIG);
1331
1332 /* Iterate through all elements. */
1333 assert(bpf_map_get_next_key(fd, NULL, &key) == 0);
1334 key.c = -1;
1335 for (i = 0; i < MAP_SIZE; i++)
1336 assert(bpf_map_get_next_key(fd, &key, &key) == 0);
1337 assert(bpf_map_get_next_key(fd, &key, &key) < 0 && errno == ENOENT);
1338
1339 key.c = 0;
1340 assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && value == 0);
1341 key.a = 1;
1342 assert(bpf_map_lookup_elem(fd, &key, &value) < 0 && errno == ENOENT);
1343
1344 close(fd);
1345}
1346
1347#define run_parallel(N, FN, DATA) \
1348 printf("Fork %u tasks to '" #FN "'\n", N); \
1349 __run_parallel(N, FN, DATA)
1350
1351static void __run_parallel(unsigned int tasks,
1352 void (*fn)(unsigned int task, void *data),
1353 void *data)
1354{
1355 pid_t pid[tasks];
1356 int i;
1357
1358 fflush(stdout);
1359
1360 for (i = 0; i < tasks; i++) {
1361 pid[i] = fork();
1362 if (pid[i] == 0) {
1363 fn(i, data);
1364 exit(0);
1365 } else if (pid[i] == -1) {
1366 printf("Couldn't spawn #%d process!\n", i);
1367 exit(1);
1368 }
1369 }
1370
1371 for (i = 0; i < tasks; i++) {
1372 int status;
1373
1374 assert(waitpid(pid[i], &status, 0) == pid[i]);
1375 assert(status == 0);
1376 }
1377}
1378
1379static void test_map_stress(void)
1380{
1381 run_parallel(100, test_hashmap_walk, NULL);
1382 run_parallel(100, test_hashmap, NULL);
1383 run_parallel(100, test_hashmap_percpu, NULL);
1384 run_parallel(100, test_hashmap_sizes, NULL);
1385
1386 run_parallel(100, test_arraymap, NULL);
1387 run_parallel(100, test_arraymap_percpu, NULL);
1388}
1389
1390#define TASKS 100
1391
1392#define DO_UPDATE 1
1393#define DO_DELETE 0
1394
1395#define MAP_RETRIES 20
1396#define MAX_DELAY_US 50000
1397#define MIN_DELAY_RANGE_US 5000
1398
1399static bool retry_for_again_or_busy(int err)
1400{
1401 return (err == EAGAIN || err == EBUSY);
1402}
1403
1404int map_update_retriable(int map_fd, const void *key, const void *value, int flags, int attempts,
1405 retry_for_error_fn need_retry)
1406{
1407 int delay = rand() % MIN_DELAY_RANGE_US;
1408
1409 while (bpf_map_update_elem(map_fd, key, value, flags)) {
1410 if (!attempts || !need_retry(errno))
1411 return -errno;
1412
1413 if (delay <= MAX_DELAY_US / 2)
1414 delay *= 2;
1415
1416 usleep(delay);
1417 attempts--;
1418 }
1419
1420 return 0;
1421}
1422
1423static int map_delete_retriable(int map_fd, const void *key, int attempts)
1424{
1425 int delay = rand() % MIN_DELAY_RANGE_US;
1426
1427 while (bpf_map_delete_elem(map_fd, key)) {
1428 if (!attempts || (errno != EAGAIN && errno != EBUSY))
1429 return -errno;
1430
1431 if (delay <= MAX_DELAY_US / 2)
1432 delay *= 2;
1433
1434 usleep(delay);
1435 attempts--;
1436 }
1437
1438 return 0;
1439}
1440
1441static void test_update_delete(unsigned int fn, void *data)
1442{
1443 int do_update = ((int *)data)[1];
1444 int fd = ((int *)data)[0];
1445 int i, key, value, err;
1446
1447 if (fn & 1)
1448 test_hashmap_walk(fn, NULL);
1449 for (i = fn; i < MAP_SIZE; i += TASKS) {
1450 key = value = i;
1451
1452 if (do_update) {
1453 err = map_update_retriable(fd, &key, &value, BPF_NOEXIST, MAP_RETRIES,
1454 retry_for_again_or_busy);
1455 if (err)
1456 printf("error %d %d\n", err, errno);
1457 assert(err == 0);
1458 err = map_update_retriable(fd, &key, &value, BPF_EXIST, MAP_RETRIES,
1459 retry_for_again_or_busy);
1460 if (err)
1461 printf("error %d %d\n", err, errno);
1462 assert(err == 0);
1463 } else {
1464 err = map_delete_retriable(fd, &key, MAP_RETRIES);
1465 if (err)
1466 printf("error %d %d\n", err, errno);
1467 assert(err == 0);
1468 }
1469 }
1470}
1471
1472static void test_map_parallel(void)
1473{
1474 int i, fd, key = 0, value = 0, j = 0;
1475 int data[2];
1476
1477 fd = bpf_map_create(BPF_MAP_TYPE_HASH, NULL, sizeof(key), sizeof(value),
1478 MAP_SIZE, &map_opts);
1479 if (fd < 0) {
1480 printf("Failed to create map for parallel test '%s'!\n",
1481 strerror(errno));
1482 exit(1);
1483 }
1484
1485again:
1486 /* Use the same fd in children to add elements to this map:
1487 * child_0 adds key=0, key=1024, key=2048, ...
1488 * child_1 adds key=1, key=1025, key=2049, ...
1489 * child_1023 adds key=1023, ...
1490 */
1491 data[0] = fd;
1492 data[1] = DO_UPDATE;
1493 run_parallel(TASKS, test_update_delete, data);
1494
1495 /* Check that key=0 is already there. */
1496 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) < 0 &&
1497 errno == EEXIST);
1498
1499 /* Check that all elements were inserted. */
1500 assert(bpf_map_get_next_key(fd, NULL, &key) == 0);
1501 key = -1;
1502 for (i = 0; i < MAP_SIZE; i++)
1503 assert(bpf_map_get_next_key(fd, &key, &key) == 0);
1504 assert(bpf_map_get_next_key(fd, &key, &key) < 0 && errno == ENOENT);
1505
1506 /* Another check for all elements */
1507 for (i = 0; i < MAP_SIZE; i++) {
1508 key = MAP_SIZE - i - 1;
1509
1510 assert(bpf_map_lookup_elem(fd, &key, &value) == 0 &&
1511 value == key);
1512 }
1513
1514 /* Now let's delete all elemenets in parallel. */
1515 data[1] = DO_DELETE;
1516 run_parallel(TASKS, test_update_delete, data);
1517
1518 /* Nothing should be left. */
1519 key = -1;
1520 assert(bpf_map_get_next_key(fd, NULL, &key) < 0 && errno == ENOENT);
1521 assert(bpf_map_get_next_key(fd, &key, &key) < 0 && errno == ENOENT);
1522
1523 key = 0;
1524 bpf_map_delete_elem(fd, &key);
1525 if (j++ < 5)
1526 goto again;
1527 close(fd);
1528}
1529
1530static void test_map_rdonly(void)
1531{
1532 int fd, key = 0, value = 0;
1533 __u32 old_flags;
1534
1535 old_flags = map_opts.map_flags;
1536 map_opts.map_flags |= BPF_F_RDONLY;
1537 fd = bpf_map_create(BPF_MAP_TYPE_HASH, NULL, sizeof(key), sizeof(value),
1538 MAP_SIZE, &map_opts);
1539 map_opts.map_flags = old_flags;
1540 if (fd < 0) {
1541 printf("Failed to create map for read only test '%s'!\n",
1542 strerror(errno));
1543 exit(1);
1544 }
1545
1546 key = 1;
1547 value = 1234;
1548 /* Try to insert key=1 element. */
1549 assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) < 0 &&
1550 errno == EPERM);
1551
1552 /* Check that key=1 is not found. */
1553 assert(bpf_map_lookup_elem(fd, &key, &value) < 0 && errno == ENOENT);
1554 assert(bpf_map_get_next_key(fd, &key, &value) < 0 && errno == ENOENT);
1555
1556 close(fd);
1557}
1558
1559static void test_map_wronly_hash(void)
1560{
1561 int fd, key = 0, value = 0;
1562 __u32 old_flags;
1563
1564 old_flags = map_opts.map_flags;
1565 map_opts.map_flags |= BPF_F_WRONLY;
1566 fd = bpf_map_create(BPF_MAP_TYPE_HASH, NULL, sizeof(key), sizeof(value),
1567 MAP_SIZE, &map_opts);
1568 map_opts.map_flags = old_flags;
1569 if (fd < 0) {
1570 printf("Failed to create map for write only test '%s'!\n",
1571 strerror(errno));
1572 exit(1);
1573 }
1574
1575 key = 1;
1576 value = 1234;
1577 /* Insert key=1 element. */
1578 assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == 0);
1579
1580 /* Check that reading elements and keys from the map is not allowed. */
1581 assert(bpf_map_lookup_elem(fd, &key, &value) < 0 && errno == EPERM);
1582 assert(bpf_map_get_next_key(fd, &key, &value) < 0 && errno == EPERM);
1583
1584 close(fd);
1585}
1586
1587static void test_map_wronly_stack_or_queue(enum bpf_map_type map_type)
1588{
1589 int fd, value = 0;
1590 __u32 old_flags;
1591
1592
1593 assert(map_type == BPF_MAP_TYPE_QUEUE ||
1594 map_type == BPF_MAP_TYPE_STACK);
1595 old_flags = map_opts.map_flags;
1596 map_opts.map_flags |= BPF_F_WRONLY;
1597 fd = bpf_map_create(map_type, NULL, 0, sizeof(value), MAP_SIZE, &map_opts);
1598 map_opts.map_flags = old_flags;
1599 /* Stack/Queue maps do not support BPF_F_NO_PREALLOC */
1600 if (map_opts.map_flags & BPF_F_NO_PREALLOC) {
1601 assert(fd < 0 && errno == EINVAL);
1602 return;
1603 }
1604 if (fd < 0) {
1605 printf("Failed to create map '%s'!\n", strerror(errno));
1606 exit(1);
1607 }
1608
1609 value = 1234;
1610 assert(bpf_map_update_elem(fd, NULL, &value, BPF_ANY) == 0);
1611
1612 /* Peek element should fail */
1613 assert(bpf_map_lookup_elem(fd, NULL, &value) < 0 && errno == EPERM);
1614
1615 /* Pop element should fail */
1616 assert(bpf_map_lookup_and_delete_elem(fd, NULL, &value) < 0 &&
1617 errno == EPERM);
1618
1619 close(fd);
1620}
1621
1622static void test_map_wronly(void)
1623{
1624 test_map_wronly_hash();
1625 test_map_wronly_stack_or_queue(BPF_MAP_TYPE_STACK);
1626 test_map_wronly_stack_or_queue(BPF_MAP_TYPE_QUEUE);
1627}
1628
1629static void prepare_reuseport_grp(int type, int map_fd, size_t map_elem_size,
1630 __s64 *fds64, __u64 *sk_cookies,
1631 unsigned int n)
1632{
1633 socklen_t optlen, addrlen;
1634 struct sockaddr_in6 s6;
1635 const __u32 index0 = 0;
1636 const int optval = 1;
1637 unsigned int i;
1638 u64 sk_cookie;
1639 void *value;
1640 __s32 fd32;
1641 __s64 fd64;
1642 int err;
1643
1644 s6.sin6_family = AF_INET6;
1645 s6.sin6_addr = in6addr_any;
1646 s6.sin6_port = 0;
1647 addrlen = sizeof(s6);
1648 optlen = sizeof(sk_cookie);
1649
1650 for (i = 0; i < n; i++) {
1651 fd64 = socket(AF_INET6, type, 0);
1652 CHECK(fd64 == -1, "socket()",
1653 "sock_type:%d fd64:%lld errno:%d\n",
1654 type, fd64, errno);
1655
1656 err = setsockopt(fd64, SOL_SOCKET, SO_REUSEPORT,
1657 &optval, sizeof(optval));
1658 CHECK(err == -1, "setsockopt(SO_REUSEPORT)",
1659 "err:%d errno:%d\n", err, errno);
1660
1661 /* reuseport_array does not allow unbound sk */
1662 if (map_elem_size == sizeof(__u64))
1663 value = &fd64;
1664 else {
1665 assert(map_elem_size == sizeof(__u32));
1666 fd32 = (__s32)fd64;
1667 value = &fd32;
1668 }
1669 err = bpf_map_update_elem(map_fd, &index0, value, BPF_ANY);
1670 CHECK(err >= 0 || errno != EINVAL,
1671 "reuseport array update unbound sk",
1672 "sock_type:%d err:%d errno:%d\n",
1673 type, err, errno);
1674
1675 err = bind(fd64, (struct sockaddr *)&s6, sizeof(s6));
1676 CHECK(err == -1, "bind()",
1677 "sock_type:%d err:%d errno:%d\n", type, err, errno);
1678
1679 if (i == 0) {
1680 err = getsockname(fd64, (struct sockaddr *)&s6,
1681 &addrlen);
1682 CHECK(err == -1, "getsockname()",
1683 "sock_type:%d err:%d errno:%d\n",
1684 type, err, errno);
1685 }
1686
1687 err = getsockopt(fd64, SOL_SOCKET, SO_COOKIE, &sk_cookie,
1688 &optlen);
1689 CHECK(err == -1, "getsockopt(SO_COOKIE)",
1690 "sock_type:%d err:%d errno:%d\n", type, err, errno);
1691
1692 if (type == SOCK_STREAM) {
1693 /*
1694 * reuseport_array does not allow
1695 * non-listening tcp sk.
1696 */
1697 err = bpf_map_update_elem(map_fd, &index0, value,
1698 BPF_ANY);
1699 CHECK(err >= 0 || errno != EINVAL,
1700 "reuseport array update non-listening sk",
1701 "sock_type:%d err:%d errno:%d\n",
1702 type, err, errno);
1703 err = listen(fd64, 0);
1704 CHECK(err == -1, "listen()",
1705 "sock_type:%d, err:%d errno:%d\n",
1706 type, err, errno);
1707 }
1708
1709 fds64[i] = fd64;
1710 sk_cookies[i] = sk_cookie;
1711 }
1712}
1713
1714static void test_reuseport_array(void)
1715{
1716#define REUSEPORT_FD_IDX(err, last) ({ (err) ? last : !last; })
1717
1718 const __u32 array_size = 4, index0 = 0, index3 = 3;
1719 int types[2] = { SOCK_STREAM, SOCK_DGRAM }, type;
1720 __u64 grpa_cookies[2], sk_cookie, map_cookie;
1721 __s64 grpa_fds64[2] = { -1, -1 }, fd64 = -1;
1722 const __u32 bad_index = array_size;
1723 int map_fd, err, t, f;
1724 __u32 fds_idx = 0;
1725 int fd;
1726
1727 map_fd = bpf_map_create(BPF_MAP_TYPE_REUSEPORT_SOCKARRAY, NULL,
1728 sizeof(__u32), sizeof(__u64), array_size, NULL);
1729 CHECK(map_fd < 0, "reuseport array create",
1730 "map_fd:%d, errno:%d\n", map_fd, errno);
1731
1732 /* Test lookup/update/delete with invalid index */
1733 err = bpf_map_delete_elem(map_fd, &bad_index);
1734 CHECK(err >= 0 || errno != E2BIG, "reuseport array del >=max_entries",
1735 "err:%d errno:%d\n", err, errno);
1736
1737 err = bpf_map_update_elem(map_fd, &bad_index, &fd64, BPF_ANY);
1738 CHECK(err >= 0 || errno != E2BIG,
1739 "reuseport array update >=max_entries",
1740 "err:%d errno:%d\n", err, errno);
1741
1742 err = bpf_map_lookup_elem(map_fd, &bad_index, &map_cookie);
1743 CHECK(err >= 0 || errno != ENOENT,
1744 "reuseport array update >=max_entries",
1745 "err:%d errno:%d\n", err, errno);
1746
1747 /* Test lookup/delete non existence elem */
1748 err = bpf_map_lookup_elem(map_fd, &index3, &map_cookie);
1749 CHECK(err >= 0 || errno != ENOENT,
1750 "reuseport array lookup not-exist elem",
1751 "err:%d errno:%d\n", err, errno);
1752 err = bpf_map_delete_elem(map_fd, &index3);
1753 CHECK(err >= 0 || errno != ENOENT,
1754 "reuseport array del not-exist elem",
1755 "err:%d errno:%d\n", err, errno);
1756
1757 for (t = 0; t < ARRAY_SIZE(types); t++) {
1758 type = types[t];
1759
1760 prepare_reuseport_grp(type, map_fd, sizeof(__u64), grpa_fds64,
1761 grpa_cookies, ARRAY_SIZE(grpa_fds64));
1762
1763 /* Test BPF_* update flags */
1764 /* BPF_EXIST failure case */
1765 err = bpf_map_update_elem(map_fd, &index3, &grpa_fds64[fds_idx],
1766 BPF_EXIST);
1767 CHECK(err >= 0 || errno != ENOENT,
1768 "reuseport array update empty elem BPF_EXIST",
1769 "sock_type:%d err:%d errno:%d\n",
1770 type, err, errno);
1771 fds_idx = REUSEPORT_FD_IDX(err, fds_idx);
1772
1773 /* BPF_NOEXIST success case */
1774 err = bpf_map_update_elem(map_fd, &index3, &grpa_fds64[fds_idx],
1775 BPF_NOEXIST);
1776 CHECK(err < 0,
1777 "reuseport array update empty elem BPF_NOEXIST",
1778 "sock_type:%d err:%d errno:%d\n",
1779 type, err, errno);
1780 fds_idx = REUSEPORT_FD_IDX(err, fds_idx);
1781
1782 /* BPF_EXIST success case. */
1783 err = bpf_map_update_elem(map_fd, &index3, &grpa_fds64[fds_idx],
1784 BPF_EXIST);
1785 CHECK(err < 0,
1786 "reuseport array update same elem BPF_EXIST",
1787 "sock_type:%d err:%d errno:%d\n", type, err, errno);
1788 fds_idx = REUSEPORT_FD_IDX(err, fds_idx);
1789
1790 /* BPF_NOEXIST failure case */
1791 err = bpf_map_update_elem(map_fd, &index3, &grpa_fds64[fds_idx],
1792 BPF_NOEXIST);
1793 CHECK(err >= 0 || errno != EEXIST,
1794 "reuseport array update non-empty elem BPF_NOEXIST",
1795 "sock_type:%d err:%d errno:%d\n",
1796 type, err, errno);
1797 fds_idx = REUSEPORT_FD_IDX(err, fds_idx);
1798
1799 /* BPF_ANY case (always succeed) */
1800 err = bpf_map_update_elem(map_fd, &index3, &grpa_fds64[fds_idx],
1801 BPF_ANY);
1802 CHECK(err < 0,
1803 "reuseport array update same sk with BPF_ANY",
1804 "sock_type:%d err:%d errno:%d\n", type, err, errno);
1805
1806 fd64 = grpa_fds64[fds_idx];
1807 sk_cookie = grpa_cookies[fds_idx];
1808
1809 /* The same sk cannot be added to reuseport_array twice */
1810 err = bpf_map_update_elem(map_fd, &index3, &fd64, BPF_ANY);
1811 CHECK(err >= 0 || errno != EBUSY,
1812 "reuseport array update same sk with same index",
1813 "sock_type:%d err:%d errno:%d\n",
1814 type, err, errno);
1815
1816 err = bpf_map_update_elem(map_fd, &index0, &fd64, BPF_ANY);
1817 CHECK(err >= 0 || errno != EBUSY,
1818 "reuseport array update same sk with different index",
1819 "sock_type:%d err:%d errno:%d\n",
1820 type, err, errno);
1821
1822 /* Test delete elem */
1823 err = bpf_map_delete_elem(map_fd, &index3);
1824 CHECK(err < 0, "reuseport array delete sk",
1825 "sock_type:%d err:%d errno:%d\n",
1826 type, err, errno);
1827
1828 /* Add it back with BPF_NOEXIST */
1829 err = bpf_map_update_elem(map_fd, &index3, &fd64, BPF_NOEXIST);
1830 CHECK(err < 0,
1831 "reuseport array re-add with BPF_NOEXIST after del",
1832 "sock_type:%d err:%d errno:%d\n", type, err, errno);
1833
1834 /* Test cookie */
1835 err = bpf_map_lookup_elem(map_fd, &index3, &map_cookie);
1836 CHECK(err < 0 || sk_cookie != map_cookie,
1837 "reuseport array lookup re-added sk",
1838 "sock_type:%d err:%d errno:%d sk_cookie:0x%llx map_cookie:0x%llxn",
1839 type, err, errno, sk_cookie, map_cookie);
1840
1841 /* Test elem removed by close() */
1842 for (f = 0; f < ARRAY_SIZE(grpa_fds64); f++)
1843 close(grpa_fds64[f]);
1844 err = bpf_map_lookup_elem(map_fd, &index3, &map_cookie);
1845 CHECK(err >= 0 || errno != ENOENT,
1846 "reuseport array lookup after close()",
1847 "sock_type:%d err:%d errno:%d\n",
1848 type, err, errno);
1849 }
1850
1851 /* Test SOCK_RAW */
1852 fd64 = socket(AF_INET6, SOCK_RAW, IPPROTO_UDP);
1853 CHECK(fd64 == -1, "socket(SOCK_RAW)", "err:%d errno:%d\n",
1854 err, errno);
1855 err = bpf_map_update_elem(map_fd, &index3, &fd64, BPF_NOEXIST);
1856 CHECK(err >= 0 || errno != ENOTSUPP, "reuseport array update SOCK_RAW",
1857 "err:%d errno:%d\n", err, errno);
1858 close(fd64);
1859
1860 /* Close the 64 bit value map */
1861 close(map_fd);
1862
1863 /* Test 32 bit fd */
1864 map_fd = bpf_map_create(BPF_MAP_TYPE_REUSEPORT_SOCKARRAY, NULL,
1865 sizeof(__u32), sizeof(__u32), array_size, NULL);
1866 CHECK(map_fd < 0, "reuseport array create",
1867 "map_fd:%d, errno:%d\n", map_fd, errno);
1868 prepare_reuseport_grp(SOCK_STREAM, map_fd, sizeof(__u32), &fd64,
1869 &sk_cookie, 1);
1870 fd = fd64;
1871 err = bpf_map_update_elem(map_fd, &index3, &fd, BPF_NOEXIST);
1872 CHECK(err < 0, "reuseport array update 32 bit fd",
1873 "err:%d errno:%d\n", err, errno);
1874 err = bpf_map_lookup_elem(map_fd, &index3, &map_cookie);
1875 CHECK(err >= 0 || errno != ENOSPC,
1876 "reuseport array lookup 32 bit fd",
1877 "err:%d errno:%d\n", err, errno);
1878 close(fd);
1879 close(map_fd);
1880}
1881
1882static void run_all_tests(void)
1883{
1884 test_hashmap(0, NULL);
1885 test_hashmap_percpu(0, NULL);
1886 test_hashmap_walk(0, NULL);
1887 test_hashmap_zero_seed();
1888
1889 test_arraymap(0, NULL);
1890 test_arraymap_percpu(0, NULL);
1891
1892 test_arraymap_percpu_many_keys();
1893
1894 test_devmap(0, NULL);
1895 test_devmap_hash(0, NULL);
1896 test_sockmap(0, NULL);
1897
1898 test_map_large();
1899 test_map_parallel();
1900 test_map_stress();
1901
1902 test_map_rdonly();
1903 test_map_wronly();
1904
1905 test_reuseport_array();
1906
1907 test_queuemap(0, NULL);
1908 test_stackmap(0, NULL);
1909
1910 test_map_in_map();
1911}
1912
1913#define DEFINE_TEST(name) extern void test_##name(void);
1914#include <map_tests/tests.h>
1915#undef DEFINE_TEST
1916
1917int main(void)
1918{
1919 srand(time(NULL));
1920
1921 libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
1922
1923 map_opts.map_flags = 0;
1924 run_all_tests();
1925
1926 map_opts.map_flags = BPF_F_NO_PREALLOC;
1927 run_all_tests();
1928
1929#define DEFINE_TEST(name) test_##name();
1930#include <map_tests/tests.h>
1931#undef DEFINE_TEST
1932
1933 printf("test_maps: OK, %d SKIPPED\n", skips);
1934 return 0;
1935}
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Testsuite for eBPF maps
4 *
5 * Copyright (c) 2014 PLUMgrid, http://plumgrid.com
6 * Copyright (c) 2016 Facebook
7 */
8
9#include <stdio.h>
10#include <unistd.h>
11#include <errno.h>
12#include <string.h>
13#include <assert.h>
14#include <stdlib.h>
15#include <time.h>
16
17#include <sys/wait.h>
18#include <sys/socket.h>
19#include <netinet/in.h>
20#include <linux/bpf.h>
21
22#include <bpf/bpf.h>
23#include <bpf/libbpf.h>
24
25#include "bpf_util.h"
26#include "bpf_rlimit.h"
27#include "test_maps.h"
28
29#ifndef ENOTSUPP
30#define ENOTSUPP 524
31#endif
32
33static int skips;
34
35static int map_flags;
36
37static void test_hashmap(unsigned int task, void *data)
38{
39 long long key, next_key, first_key, value;
40 int fd;
41
42 fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value),
43 2, map_flags);
44 if (fd < 0) {
45 printf("Failed to create hashmap '%s'!\n", strerror(errno));
46 exit(1);
47 }
48
49 key = 1;
50 value = 1234;
51 /* Insert key=1 element. */
52 assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == 0);
53
54 value = 0;
55 /* BPF_NOEXIST means add new element if it doesn't exist. */
56 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) < 0 &&
57 /* key=1 already exists. */
58 errno == EEXIST);
59
60 /* -1 is an invalid flag. */
61 assert(bpf_map_update_elem(fd, &key, &value, -1) < 0 &&
62 errno == EINVAL);
63
64 /* Check that key=1 can be found. */
65 assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && value == 1234);
66
67 key = 2;
68 value = 1234;
69 /* Insert key=2 element. */
70 assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == 0);
71
72 /* Check that key=2 matches the value and delete it */
73 assert(bpf_map_lookup_and_delete_elem(fd, &key, &value) == 0 && value == 1234);
74
75 /* Check that key=2 is not found. */
76 assert(bpf_map_lookup_elem(fd, &key, &value) < 0 && errno == ENOENT);
77
78 /* BPF_EXIST means update existing element. */
79 assert(bpf_map_update_elem(fd, &key, &value, BPF_EXIST) < 0 &&
80 /* key=2 is not there. */
81 errno == ENOENT);
82
83 /* Insert key=2 element. */
84 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == 0);
85
86 /* key=1 and key=2 were inserted, check that key=0 cannot be
87 * inserted due to max_entries limit.
88 */
89 key = 0;
90 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) < 0 &&
91 errno == E2BIG);
92
93 /* Update existing element, though the map is full. */
94 key = 1;
95 assert(bpf_map_update_elem(fd, &key, &value, BPF_EXIST) == 0);
96 key = 2;
97 assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == 0);
98 key = 3;
99 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) < 0 &&
100 errno == E2BIG);
101
102 /* Check that key = 0 doesn't exist. */
103 key = 0;
104 assert(bpf_map_delete_elem(fd, &key) < 0 && errno == ENOENT);
105
106 /* Iterate over two elements. */
107 assert(bpf_map_get_next_key(fd, NULL, &first_key) == 0 &&
108 (first_key == 1 || first_key == 2));
109 assert(bpf_map_get_next_key(fd, &key, &next_key) == 0 &&
110 (next_key == first_key));
111 assert(bpf_map_get_next_key(fd, &next_key, &next_key) == 0 &&
112 (next_key == 1 || next_key == 2) &&
113 (next_key != first_key));
114 assert(bpf_map_get_next_key(fd, &next_key, &next_key) < 0 &&
115 errno == ENOENT);
116
117 /* Delete both elements. */
118 key = 1;
119 assert(bpf_map_delete_elem(fd, &key) == 0);
120 key = 2;
121 assert(bpf_map_delete_elem(fd, &key) == 0);
122 assert(bpf_map_delete_elem(fd, &key) < 0 && errno == ENOENT);
123
124 key = 0;
125 /* Check that map is empty. */
126 assert(bpf_map_get_next_key(fd, NULL, &next_key) < 0 &&
127 errno == ENOENT);
128 assert(bpf_map_get_next_key(fd, &key, &next_key) < 0 &&
129 errno == ENOENT);
130
131 close(fd);
132}
133
134static void test_hashmap_sizes(unsigned int task, void *data)
135{
136 int fd, i, j;
137
138 for (i = 1; i <= 512; i <<= 1)
139 for (j = 1; j <= 1 << 18; j <<= 1) {
140 fd = bpf_create_map(BPF_MAP_TYPE_HASH, i, j,
141 2, map_flags);
142 if (fd < 0) {
143 if (errno == ENOMEM)
144 return;
145 printf("Failed to create hashmap key=%d value=%d '%s'\n",
146 i, j, strerror(errno));
147 exit(1);
148 }
149 close(fd);
150 usleep(10); /* give kernel time to destroy */
151 }
152}
153
154static void test_hashmap_percpu(unsigned int task, void *data)
155{
156 unsigned int nr_cpus = bpf_num_possible_cpus();
157 BPF_DECLARE_PERCPU(long, value);
158 long long key, next_key, first_key;
159 int expected_key_mask = 0;
160 int fd, i;
161
162 fd = bpf_create_map(BPF_MAP_TYPE_PERCPU_HASH, sizeof(key),
163 sizeof(bpf_percpu(value, 0)), 2, map_flags);
164 if (fd < 0) {
165 printf("Failed to create hashmap '%s'!\n", strerror(errno));
166 exit(1);
167 }
168
169 for (i = 0; i < nr_cpus; i++)
170 bpf_percpu(value, i) = i + 100;
171
172 key = 1;
173 /* Insert key=1 element. */
174 assert(!(expected_key_mask & key));
175 assert(bpf_map_update_elem(fd, &key, value, BPF_ANY) == 0);
176
177 /* Lookup and delete elem key=1 and check value. */
178 assert(bpf_map_lookup_and_delete_elem(fd, &key, value) == 0 &&
179 bpf_percpu(value,0) == 100);
180
181 for (i = 0; i < nr_cpus; i++)
182 bpf_percpu(value,i) = i + 100;
183
184 /* Insert key=1 element which should not exist. */
185 assert(bpf_map_update_elem(fd, &key, value, BPF_NOEXIST) == 0);
186 expected_key_mask |= key;
187
188 /* BPF_NOEXIST means add new element if it doesn't exist. */
189 assert(bpf_map_update_elem(fd, &key, value, BPF_NOEXIST) < 0 &&
190 /* key=1 already exists. */
191 errno == EEXIST);
192
193 /* -1 is an invalid flag. */
194 assert(bpf_map_update_elem(fd, &key, value, -1) < 0 &&
195 errno == EINVAL);
196
197 /* Check that key=1 can be found. Value could be 0 if the lookup
198 * was run from a different CPU.
199 */
200 bpf_percpu(value, 0) = 1;
201 assert(bpf_map_lookup_elem(fd, &key, value) == 0 &&
202 bpf_percpu(value, 0) == 100);
203
204 key = 2;
205 /* Check that key=2 is not found. */
206 assert(bpf_map_lookup_elem(fd, &key, value) < 0 && errno == ENOENT);
207
208 /* BPF_EXIST means update existing element. */
209 assert(bpf_map_update_elem(fd, &key, value, BPF_EXIST) < 0 &&
210 /* key=2 is not there. */
211 errno == ENOENT);
212
213 /* Insert key=2 element. */
214 assert(!(expected_key_mask & key));
215 assert(bpf_map_update_elem(fd, &key, value, BPF_NOEXIST) == 0);
216 expected_key_mask |= key;
217
218 /* key=1 and key=2 were inserted, check that key=0 cannot be
219 * inserted due to max_entries limit.
220 */
221 key = 0;
222 assert(bpf_map_update_elem(fd, &key, value, BPF_NOEXIST) < 0 &&
223 errno == E2BIG);
224
225 /* Check that key = 0 doesn't exist. */
226 assert(bpf_map_delete_elem(fd, &key) < 0 && errno == ENOENT);
227
228 /* Iterate over two elements. */
229 assert(bpf_map_get_next_key(fd, NULL, &first_key) == 0 &&
230 ((expected_key_mask & first_key) == first_key));
231 while (!bpf_map_get_next_key(fd, &key, &next_key)) {
232 if (first_key) {
233 assert(next_key == first_key);
234 first_key = 0;
235 }
236 assert((expected_key_mask & next_key) == next_key);
237 expected_key_mask &= ~next_key;
238
239 assert(bpf_map_lookup_elem(fd, &next_key, value) == 0);
240
241 for (i = 0; i < nr_cpus; i++)
242 assert(bpf_percpu(value, i) == i + 100);
243
244 key = next_key;
245 }
246 assert(errno == ENOENT);
247
248 /* Update with BPF_EXIST. */
249 key = 1;
250 assert(bpf_map_update_elem(fd, &key, value, BPF_EXIST) == 0);
251
252 /* Delete both elements. */
253 key = 1;
254 assert(bpf_map_delete_elem(fd, &key) == 0);
255 key = 2;
256 assert(bpf_map_delete_elem(fd, &key) == 0);
257 assert(bpf_map_delete_elem(fd, &key) < 0 && errno == ENOENT);
258
259 key = 0;
260 /* Check that map is empty. */
261 assert(bpf_map_get_next_key(fd, NULL, &next_key) < 0 &&
262 errno == ENOENT);
263 assert(bpf_map_get_next_key(fd, &key, &next_key) < 0 &&
264 errno == ENOENT);
265
266 close(fd);
267}
268
269static int helper_fill_hashmap(int max_entries)
270{
271 int i, fd, ret;
272 long long key, value;
273
274 fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value),
275 max_entries, map_flags);
276 CHECK(fd < 0,
277 "failed to create hashmap",
278 "err: %s, flags: 0x%x\n", strerror(errno), map_flags);
279
280 for (i = 0; i < max_entries; i++) {
281 key = i; value = key;
282 ret = bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST);
283 CHECK(ret != 0,
284 "can't update hashmap",
285 "err: %s\n", strerror(ret));
286 }
287
288 return fd;
289}
290
291static void test_hashmap_walk(unsigned int task, void *data)
292{
293 int fd, i, max_entries = 1000;
294 long long key, value, next_key;
295 bool next_key_valid = true;
296
297 fd = helper_fill_hashmap(max_entries);
298
299 for (i = 0; bpf_map_get_next_key(fd, !i ? NULL : &key,
300 &next_key) == 0; i++) {
301 key = next_key;
302 assert(bpf_map_lookup_elem(fd, &key, &value) == 0);
303 }
304
305 assert(i == max_entries);
306
307 assert(bpf_map_get_next_key(fd, NULL, &key) == 0);
308 for (i = 0; next_key_valid; i++) {
309 next_key_valid = bpf_map_get_next_key(fd, &key, &next_key) == 0;
310 assert(bpf_map_lookup_elem(fd, &key, &value) == 0);
311 value++;
312 assert(bpf_map_update_elem(fd, &key, &value, BPF_EXIST) == 0);
313 key = next_key;
314 }
315
316 assert(i == max_entries);
317
318 for (i = 0; bpf_map_get_next_key(fd, !i ? NULL : &key,
319 &next_key) == 0; i++) {
320 key = next_key;
321 assert(bpf_map_lookup_elem(fd, &key, &value) == 0);
322 assert(value - 1 == key);
323 }
324
325 assert(i == max_entries);
326 close(fd);
327}
328
329static void test_hashmap_zero_seed(void)
330{
331 int i, first, second, old_flags;
332 long long key, next_first, next_second;
333
334 old_flags = map_flags;
335 map_flags |= BPF_F_ZERO_SEED;
336
337 first = helper_fill_hashmap(3);
338 second = helper_fill_hashmap(3);
339
340 for (i = 0; ; i++) {
341 void *key_ptr = !i ? NULL : &key;
342
343 if (bpf_map_get_next_key(first, key_ptr, &next_first) != 0)
344 break;
345
346 CHECK(bpf_map_get_next_key(second, key_ptr, &next_second) != 0,
347 "next_key for second map must succeed",
348 "key_ptr: %p", key_ptr);
349 CHECK(next_first != next_second,
350 "keys must match",
351 "i: %d first: %lld second: %lld\n", i,
352 next_first, next_second);
353
354 key = next_first;
355 }
356
357 map_flags = old_flags;
358 close(first);
359 close(second);
360}
361
362static void test_arraymap(unsigned int task, void *data)
363{
364 int key, next_key, fd;
365 long long value;
366
367 fd = bpf_create_map(BPF_MAP_TYPE_ARRAY, sizeof(key), sizeof(value),
368 2, 0);
369 if (fd < 0) {
370 printf("Failed to create arraymap '%s'!\n", strerror(errno));
371 exit(1);
372 }
373
374 key = 1;
375 value = 1234;
376 /* Insert key=1 element. */
377 assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == 0);
378
379 value = 0;
380 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) < 0 &&
381 errno == EEXIST);
382
383 /* Check that key=1 can be found. */
384 assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && value == 1234);
385
386 key = 0;
387 /* Check that key=0 is also found and zero initialized. */
388 assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && value == 0);
389
390 /* key=0 and key=1 were inserted, check that key=2 cannot be inserted
391 * due to max_entries limit.
392 */
393 key = 2;
394 assert(bpf_map_update_elem(fd, &key, &value, BPF_EXIST) < 0 &&
395 errno == E2BIG);
396
397 /* Check that key = 2 doesn't exist. */
398 assert(bpf_map_lookup_elem(fd, &key, &value) < 0 && errno == ENOENT);
399
400 /* Iterate over two elements. */
401 assert(bpf_map_get_next_key(fd, NULL, &next_key) == 0 &&
402 next_key == 0);
403 assert(bpf_map_get_next_key(fd, &key, &next_key) == 0 &&
404 next_key == 0);
405 assert(bpf_map_get_next_key(fd, &next_key, &next_key) == 0 &&
406 next_key == 1);
407 assert(bpf_map_get_next_key(fd, &next_key, &next_key) < 0 &&
408 errno == ENOENT);
409
410 /* Delete shouldn't succeed. */
411 key = 1;
412 assert(bpf_map_delete_elem(fd, &key) < 0 && errno == EINVAL);
413
414 close(fd);
415}
416
417static void test_arraymap_percpu(unsigned int task, void *data)
418{
419 unsigned int nr_cpus = bpf_num_possible_cpus();
420 BPF_DECLARE_PERCPU(long, values);
421 int key, next_key, fd, i;
422
423 fd = bpf_create_map(BPF_MAP_TYPE_PERCPU_ARRAY, sizeof(key),
424 sizeof(bpf_percpu(values, 0)), 2, 0);
425 if (fd < 0) {
426 printf("Failed to create arraymap '%s'!\n", strerror(errno));
427 exit(1);
428 }
429
430 for (i = 0; i < nr_cpus; i++)
431 bpf_percpu(values, i) = i + 100;
432
433 key = 1;
434 /* Insert key=1 element. */
435 assert(bpf_map_update_elem(fd, &key, values, BPF_ANY) == 0);
436
437 bpf_percpu(values, 0) = 0;
438 assert(bpf_map_update_elem(fd, &key, values, BPF_NOEXIST) < 0 &&
439 errno == EEXIST);
440
441 /* Check that key=1 can be found. */
442 assert(bpf_map_lookup_elem(fd, &key, values) == 0 &&
443 bpf_percpu(values, 0) == 100);
444
445 key = 0;
446 /* Check that key=0 is also found and zero initialized. */
447 assert(bpf_map_lookup_elem(fd, &key, values) == 0 &&
448 bpf_percpu(values, 0) == 0 &&
449 bpf_percpu(values, nr_cpus - 1) == 0);
450
451 /* Check that key=2 cannot be inserted due to max_entries limit. */
452 key = 2;
453 assert(bpf_map_update_elem(fd, &key, values, BPF_EXIST) < 0 &&
454 errno == E2BIG);
455
456 /* Check that key = 2 doesn't exist. */
457 assert(bpf_map_lookup_elem(fd, &key, values) < 0 && errno == ENOENT);
458
459 /* Iterate over two elements. */
460 assert(bpf_map_get_next_key(fd, NULL, &next_key) == 0 &&
461 next_key == 0);
462 assert(bpf_map_get_next_key(fd, &key, &next_key) == 0 &&
463 next_key == 0);
464 assert(bpf_map_get_next_key(fd, &next_key, &next_key) == 0 &&
465 next_key == 1);
466 assert(bpf_map_get_next_key(fd, &next_key, &next_key) < 0 &&
467 errno == ENOENT);
468
469 /* Delete shouldn't succeed. */
470 key = 1;
471 assert(bpf_map_delete_elem(fd, &key) < 0 && errno == EINVAL);
472
473 close(fd);
474}
475
476static void test_arraymap_percpu_many_keys(void)
477{
478 unsigned int nr_cpus = bpf_num_possible_cpus();
479 BPF_DECLARE_PERCPU(long, values);
480 /* nr_keys is not too large otherwise the test stresses percpu
481 * allocator more than anything else
482 */
483 unsigned int nr_keys = 2000;
484 int key, fd, i;
485
486 fd = bpf_create_map(BPF_MAP_TYPE_PERCPU_ARRAY, sizeof(key),
487 sizeof(bpf_percpu(values, 0)), nr_keys, 0);
488 if (fd < 0) {
489 printf("Failed to create per-cpu arraymap '%s'!\n",
490 strerror(errno));
491 exit(1);
492 }
493
494 for (i = 0; i < nr_cpus; i++)
495 bpf_percpu(values, i) = i + 10;
496
497 for (key = 0; key < nr_keys; key++)
498 assert(bpf_map_update_elem(fd, &key, values, BPF_ANY) == 0);
499
500 for (key = 0; key < nr_keys; key++) {
501 for (i = 0; i < nr_cpus; i++)
502 bpf_percpu(values, i) = 0;
503
504 assert(bpf_map_lookup_elem(fd, &key, values) == 0);
505
506 for (i = 0; i < nr_cpus; i++)
507 assert(bpf_percpu(values, i) == i + 10);
508 }
509
510 close(fd);
511}
512
513static void test_devmap(unsigned int task, void *data)
514{
515 int fd;
516 __u32 key, value;
517
518 fd = bpf_create_map(BPF_MAP_TYPE_DEVMAP, sizeof(key), sizeof(value),
519 2, 0);
520 if (fd < 0) {
521 printf("Failed to create devmap '%s'!\n", strerror(errno));
522 exit(1);
523 }
524
525 close(fd);
526}
527
528static void test_devmap_hash(unsigned int task, void *data)
529{
530 int fd;
531 __u32 key, value;
532
533 fd = bpf_create_map(BPF_MAP_TYPE_DEVMAP_HASH, sizeof(key), sizeof(value),
534 2, 0);
535 if (fd < 0) {
536 printf("Failed to create devmap_hash '%s'!\n", strerror(errno));
537 exit(1);
538 }
539
540 close(fd);
541}
542
543static void test_queuemap(unsigned int task, void *data)
544{
545 const int MAP_SIZE = 32;
546 __u32 vals[MAP_SIZE + MAP_SIZE/2], val;
547 int fd, i;
548
549 /* Fill test values to be used */
550 for (i = 0; i < MAP_SIZE + MAP_SIZE/2; i++)
551 vals[i] = rand();
552
553 /* Invalid key size */
554 fd = bpf_create_map(BPF_MAP_TYPE_QUEUE, 4, sizeof(val), MAP_SIZE,
555 map_flags);
556 assert(fd < 0 && errno == EINVAL);
557
558 fd = bpf_create_map(BPF_MAP_TYPE_QUEUE, 0, sizeof(val), MAP_SIZE,
559 map_flags);
560 /* Queue map does not support BPF_F_NO_PREALLOC */
561 if (map_flags & BPF_F_NO_PREALLOC) {
562 assert(fd < 0 && errno == EINVAL);
563 return;
564 }
565 if (fd < 0) {
566 printf("Failed to create queuemap '%s'!\n", strerror(errno));
567 exit(1);
568 }
569
570 /* Push MAP_SIZE elements */
571 for (i = 0; i < MAP_SIZE; i++)
572 assert(bpf_map_update_elem(fd, NULL, &vals[i], 0) == 0);
573
574 /* Check that element cannot be pushed due to max_entries limit */
575 assert(bpf_map_update_elem(fd, NULL, &val, 0) < 0 &&
576 errno == E2BIG);
577
578 /* Peek element */
579 assert(bpf_map_lookup_elem(fd, NULL, &val) == 0 && val == vals[0]);
580
581 /* Replace half elements */
582 for (i = MAP_SIZE; i < MAP_SIZE + MAP_SIZE/2; i++)
583 assert(bpf_map_update_elem(fd, NULL, &vals[i], BPF_EXIST) == 0);
584
585 /* Pop all elements */
586 for (i = MAP_SIZE/2; i < MAP_SIZE + MAP_SIZE/2; i++)
587 assert(bpf_map_lookup_and_delete_elem(fd, NULL, &val) == 0 &&
588 val == vals[i]);
589
590 /* Check that there are not elements left */
591 assert(bpf_map_lookup_and_delete_elem(fd, NULL, &val) < 0 &&
592 errno == ENOENT);
593
594 /* Check that non supported functions set errno to EINVAL */
595 assert(bpf_map_delete_elem(fd, NULL) < 0 && errno == EINVAL);
596 assert(bpf_map_get_next_key(fd, NULL, NULL) < 0 && errno == EINVAL);
597
598 close(fd);
599}
600
601static void test_stackmap(unsigned int task, void *data)
602{
603 const int MAP_SIZE = 32;
604 __u32 vals[MAP_SIZE + MAP_SIZE/2], val;
605 int fd, i;
606
607 /* Fill test values to be used */
608 for (i = 0; i < MAP_SIZE + MAP_SIZE/2; i++)
609 vals[i] = rand();
610
611 /* Invalid key size */
612 fd = bpf_create_map(BPF_MAP_TYPE_STACK, 4, sizeof(val), MAP_SIZE,
613 map_flags);
614 assert(fd < 0 && errno == EINVAL);
615
616 fd = bpf_create_map(BPF_MAP_TYPE_STACK, 0, sizeof(val), MAP_SIZE,
617 map_flags);
618 /* Stack map does not support BPF_F_NO_PREALLOC */
619 if (map_flags & BPF_F_NO_PREALLOC) {
620 assert(fd < 0 && errno == EINVAL);
621 return;
622 }
623 if (fd < 0) {
624 printf("Failed to create stackmap '%s'!\n", strerror(errno));
625 exit(1);
626 }
627
628 /* Push MAP_SIZE elements */
629 for (i = 0; i < MAP_SIZE; i++)
630 assert(bpf_map_update_elem(fd, NULL, &vals[i], 0) == 0);
631
632 /* Check that element cannot be pushed due to max_entries limit */
633 assert(bpf_map_update_elem(fd, NULL, &val, 0) < 0 &&
634 errno == E2BIG);
635
636 /* Peek element */
637 assert(bpf_map_lookup_elem(fd, NULL, &val) == 0 && val == vals[i - 1]);
638
639 /* Replace half elements */
640 for (i = MAP_SIZE; i < MAP_SIZE + MAP_SIZE/2; i++)
641 assert(bpf_map_update_elem(fd, NULL, &vals[i], BPF_EXIST) == 0);
642
643 /* Pop all elements */
644 for (i = MAP_SIZE + MAP_SIZE/2 - 1; i >= MAP_SIZE/2; i--)
645 assert(bpf_map_lookup_and_delete_elem(fd, NULL, &val) == 0 &&
646 val == vals[i]);
647
648 /* Check that there are not elements left */
649 assert(bpf_map_lookup_and_delete_elem(fd, NULL, &val) < 0 &&
650 errno == ENOENT);
651
652 /* Check that non supported functions set errno to EINVAL */
653 assert(bpf_map_delete_elem(fd, NULL) < 0 && errno == EINVAL);
654 assert(bpf_map_get_next_key(fd, NULL, NULL) < 0 && errno == EINVAL);
655
656 close(fd);
657}
658
659#include <sys/ioctl.h>
660#include <arpa/inet.h>
661#include <sys/select.h>
662#include <linux/err.h>
663#define SOCKMAP_PARSE_PROG "./sockmap_parse_prog.o"
664#define SOCKMAP_VERDICT_PROG "./sockmap_verdict_prog.o"
665#define SOCKMAP_TCP_MSG_PROG "./sockmap_tcp_msg_prog.o"
666static void test_sockmap(unsigned int tasks, void *data)
667{
668 struct bpf_map *bpf_map_rx, *bpf_map_tx, *bpf_map_msg, *bpf_map_break;
669 int map_fd_msg = 0, map_fd_rx = 0, map_fd_tx = 0, map_fd_break;
670 int ports[] = {50200, 50201, 50202, 50204};
671 int err, i, fd, udp, sfd[6] = {0xdeadbeef};
672 u8 buf[20] = {0x0, 0x5, 0x3, 0x2, 0x1, 0x0};
673 int parse_prog, verdict_prog, msg_prog;
674 struct sockaddr_in addr;
675 int one = 1, s, sc, rc;
676 struct bpf_object *obj;
677 struct timeval to;
678 __u32 key, value;
679 pid_t pid[tasks];
680 fd_set w;
681
682 /* Create some sockets to use with sockmap */
683 for (i = 0; i < 2; i++) {
684 sfd[i] = socket(AF_INET, SOCK_STREAM, 0);
685 if (sfd[i] < 0)
686 goto out;
687 err = setsockopt(sfd[i], SOL_SOCKET, SO_REUSEADDR,
688 (char *)&one, sizeof(one));
689 if (err) {
690 printf("failed to setsockopt\n");
691 goto out;
692 }
693 err = ioctl(sfd[i], FIONBIO, (char *)&one);
694 if (err < 0) {
695 printf("failed to ioctl\n");
696 goto out;
697 }
698 memset(&addr, 0, sizeof(struct sockaddr_in));
699 addr.sin_family = AF_INET;
700 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
701 addr.sin_port = htons(ports[i]);
702 err = bind(sfd[i], (struct sockaddr *)&addr, sizeof(addr));
703 if (err < 0) {
704 printf("failed to bind: err %i: %i:%i\n",
705 err, i, sfd[i]);
706 goto out;
707 }
708 err = listen(sfd[i], 32);
709 if (err < 0) {
710 printf("failed to listen\n");
711 goto out;
712 }
713 }
714
715 for (i = 2; i < 4; i++) {
716 sfd[i] = socket(AF_INET, SOCK_STREAM, 0);
717 if (sfd[i] < 0)
718 goto out;
719 err = setsockopt(sfd[i], SOL_SOCKET, SO_REUSEADDR,
720 (char *)&one, sizeof(one));
721 if (err) {
722 printf("set sock opt\n");
723 goto out;
724 }
725 memset(&addr, 0, sizeof(struct sockaddr_in));
726 addr.sin_family = AF_INET;
727 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
728 addr.sin_port = htons(ports[i - 2]);
729 err = connect(sfd[i], (struct sockaddr *)&addr, sizeof(addr));
730 if (err) {
731 printf("failed to connect\n");
732 goto out;
733 }
734 }
735
736
737 for (i = 4; i < 6; i++) {
738 sfd[i] = accept(sfd[i - 4], NULL, NULL);
739 if (sfd[i] < 0) {
740 printf("accept failed\n");
741 goto out;
742 }
743 }
744
745 /* Test sockmap with connected sockets */
746 fd = bpf_create_map(BPF_MAP_TYPE_SOCKMAP,
747 sizeof(key), sizeof(value),
748 6, 0);
749 if (fd < 0) {
750 if (!bpf_probe_map_type(BPF_MAP_TYPE_SOCKMAP, 0)) {
751 printf("%s SKIP (unsupported map type BPF_MAP_TYPE_SOCKMAP)\n",
752 __func__);
753 skips++;
754 for (i = 0; i < 6; i++)
755 close(sfd[i]);
756 return;
757 }
758
759 printf("Failed to create sockmap %i\n", fd);
760 goto out_sockmap;
761 }
762
763 /* Test update with unsupported UDP socket */
764 udp = socket(AF_INET, SOCK_DGRAM, 0);
765 i = 0;
766 err = bpf_map_update_elem(fd, &i, &udp, BPF_ANY);
767 if (err) {
768 printf("Failed socket update SOCK_DGRAM '%i:%i'\n",
769 i, udp);
770 goto out_sockmap;
771 }
772
773 /* Test update without programs */
774 for (i = 0; i < 6; i++) {
775 err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_ANY);
776 if (err) {
777 printf("Failed noprog update sockmap '%i:%i'\n",
778 i, sfd[i]);
779 goto out_sockmap;
780 }
781 }
782
783 /* Test attaching/detaching bad fds */
784 err = bpf_prog_attach(-1, fd, BPF_SK_SKB_STREAM_PARSER, 0);
785 if (!err) {
786 printf("Failed invalid parser prog attach\n");
787 goto out_sockmap;
788 }
789
790 err = bpf_prog_attach(-1, fd, BPF_SK_SKB_STREAM_VERDICT, 0);
791 if (!err) {
792 printf("Failed invalid verdict prog attach\n");
793 goto out_sockmap;
794 }
795
796 err = bpf_prog_attach(-1, fd, BPF_SK_MSG_VERDICT, 0);
797 if (!err) {
798 printf("Failed invalid msg verdict prog attach\n");
799 goto out_sockmap;
800 }
801
802 err = bpf_prog_attach(-1, fd, __MAX_BPF_ATTACH_TYPE, 0);
803 if (!err) {
804 printf("Failed unknown prog attach\n");
805 goto out_sockmap;
806 }
807
808 err = bpf_prog_detach(fd, BPF_SK_SKB_STREAM_PARSER);
809 if (!err) {
810 printf("Failed empty parser prog detach\n");
811 goto out_sockmap;
812 }
813
814 err = bpf_prog_detach(fd, BPF_SK_SKB_STREAM_VERDICT);
815 if (!err) {
816 printf("Failed empty verdict prog detach\n");
817 goto out_sockmap;
818 }
819
820 err = bpf_prog_detach(fd, BPF_SK_MSG_VERDICT);
821 if (!err) {
822 printf("Failed empty msg verdict prog detach\n");
823 goto out_sockmap;
824 }
825
826 err = bpf_prog_detach(fd, __MAX_BPF_ATTACH_TYPE);
827 if (!err) {
828 printf("Detach invalid prog successful\n");
829 goto out_sockmap;
830 }
831
832 /* Load SK_SKB program and Attach */
833 err = bpf_prog_load(SOCKMAP_PARSE_PROG,
834 BPF_PROG_TYPE_SK_SKB, &obj, &parse_prog);
835 if (err) {
836 printf("Failed to load SK_SKB parse prog\n");
837 goto out_sockmap;
838 }
839
840 err = bpf_prog_load(SOCKMAP_TCP_MSG_PROG,
841 BPF_PROG_TYPE_SK_MSG, &obj, &msg_prog);
842 if (err) {
843 printf("Failed to load SK_SKB msg prog\n");
844 goto out_sockmap;
845 }
846
847 err = bpf_prog_load(SOCKMAP_VERDICT_PROG,
848 BPF_PROG_TYPE_SK_SKB, &obj, &verdict_prog);
849 if (err) {
850 printf("Failed to load SK_SKB verdict prog\n");
851 goto out_sockmap;
852 }
853
854 bpf_map_rx = bpf_object__find_map_by_name(obj, "sock_map_rx");
855 if (!bpf_map_rx) {
856 printf("Failed to load map rx from verdict prog\n");
857 goto out_sockmap;
858 }
859
860 map_fd_rx = bpf_map__fd(bpf_map_rx);
861 if (map_fd_rx < 0) {
862 printf("Failed to get map rx fd\n");
863 goto out_sockmap;
864 }
865
866 bpf_map_tx = bpf_object__find_map_by_name(obj, "sock_map_tx");
867 if (!bpf_map_tx) {
868 printf("Failed to load map tx from verdict prog\n");
869 goto out_sockmap;
870 }
871
872 map_fd_tx = bpf_map__fd(bpf_map_tx);
873 if (map_fd_tx < 0) {
874 printf("Failed to get map tx fd\n");
875 goto out_sockmap;
876 }
877
878 bpf_map_msg = bpf_object__find_map_by_name(obj, "sock_map_msg");
879 if (!bpf_map_msg) {
880 printf("Failed to load map msg from msg_verdict prog\n");
881 goto out_sockmap;
882 }
883
884 map_fd_msg = bpf_map__fd(bpf_map_msg);
885 if (map_fd_msg < 0) {
886 printf("Failed to get map msg fd\n");
887 goto out_sockmap;
888 }
889
890 bpf_map_break = bpf_object__find_map_by_name(obj, "sock_map_break");
891 if (!bpf_map_break) {
892 printf("Failed to load map tx from verdict prog\n");
893 goto out_sockmap;
894 }
895
896 map_fd_break = bpf_map__fd(bpf_map_break);
897 if (map_fd_break < 0) {
898 printf("Failed to get map tx fd\n");
899 goto out_sockmap;
900 }
901
902 err = bpf_prog_attach(parse_prog, map_fd_break,
903 BPF_SK_SKB_STREAM_PARSER, 0);
904 if (!err) {
905 printf("Allowed attaching SK_SKB program to invalid map\n");
906 goto out_sockmap;
907 }
908
909 err = bpf_prog_attach(parse_prog, map_fd_rx,
910 BPF_SK_SKB_STREAM_PARSER, 0);
911 if (err) {
912 printf("Failed stream parser bpf prog attach\n");
913 goto out_sockmap;
914 }
915
916 err = bpf_prog_attach(verdict_prog, map_fd_rx,
917 BPF_SK_SKB_STREAM_VERDICT, 0);
918 if (err) {
919 printf("Failed stream verdict bpf prog attach\n");
920 goto out_sockmap;
921 }
922
923 err = bpf_prog_attach(msg_prog, map_fd_msg, BPF_SK_MSG_VERDICT, 0);
924 if (err) {
925 printf("Failed msg verdict bpf prog attach\n");
926 goto out_sockmap;
927 }
928
929 err = bpf_prog_attach(verdict_prog, map_fd_rx,
930 __MAX_BPF_ATTACH_TYPE, 0);
931 if (!err) {
932 printf("Attached unknown bpf prog\n");
933 goto out_sockmap;
934 }
935
936 /* Test map update elem afterwards fd lives in fd and map_fd */
937 for (i = 2; i < 6; i++) {
938 err = bpf_map_update_elem(map_fd_rx, &i, &sfd[i], BPF_ANY);
939 if (err) {
940 printf("Failed map_fd_rx update sockmap %i '%i:%i'\n",
941 err, i, sfd[i]);
942 goto out_sockmap;
943 }
944 err = bpf_map_update_elem(map_fd_tx, &i, &sfd[i], BPF_ANY);
945 if (err) {
946 printf("Failed map_fd_tx update sockmap %i '%i:%i'\n",
947 err, i, sfd[i]);
948 goto out_sockmap;
949 }
950 }
951
952 /* Test map delete elem and remove send/recv sockets */
953 for (i = 2; i < 4; i++) {
954 err = bpf_map_delete_elem(map_fd_rx, &i);
955 if (err) {
956 printf("Failed delete sockmap rx %i '%i:%i'\n",
957 err, i, sfd[i]);
958 goto out_sockmap;
959 }
960 err = bpf_map_delete_elem(map_fd_tx, &i);
961 if (err) {
962 printf("Failed delete sockmap tx %i '%i:%i'\n",
963 err, i, sfd[i]);
964 goto out_sockmap;
965 }
966 }
967
968 /* Put sfd[2] (sending fd below) into msg map to test sendmsg bpf */
969 i = 0;
970 err = bpf_map_update_elem(map_fd_msg, &i, &sfd[2], BPF_ANY);
971 if (err) {
972 printf("Failed map_fd_msg update sockmap %i\n", err);
973 goto out_sockmap;
974 }
975
976 /* Test map send/recv */
977 for (i = 0; i < 2; i++) {
978 buf[0] = i;
979 buf[1] = 0x5;
980 sc = send(sfd[2], buf, 20, 0);
981 if (sc < 0) {
982 printf("Failed sockmap send\n");
983 goto out_sockmap;
984 }
985
986 FD_ZERO(&w);
987 FD_SET(sfd[3], &w);
988 to.tv_sec = 30;
989 to.tv_usec = 0;
990 s = select(sfd[3] + 1, &w, NULL, NULL, &to);
991 if (s == -1) {
992 perror("Failed sockmap select()");
993 goto out_sockmap;
994 } else if (!s) {
995 printf("Failed sockmap unexpected timeout\n");
996 goto out_sockmap;
997 }
998
999 if (!FD_ISSET(sfd[3], &w)) {
1000 printf("Failed sockmap select/recv\n");
1001 goto out_sockmap;
1002 }
1003
1004 rc = recv(sfd[3], buf, sizeof(buf), 0);
1005 if (rc < 0) {
1006 printf("Failed sockmap recv\n");
1007 goto out_sockmap;
1008 }
1009 }
1010
1011 /* Negative null entry lookup from datapath should be dropped */
1012 buf[0] = 1;
1013 buf[1] = 12;
1014 sc = send(sfd[2], buf, 20, 0);
1015 if (sc < 0) {
1016 printf("Failed sockmap send\n");
1017 goto out_sockmap;
1018 }
1019
1020 /* Push fd into same slot */
1021 i = 2;
1022 err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_NOEXIST);
1023 if (!err) {
1024 printf("Failed allowed sockmap dup slot BPF_NOEXIST\n");
1025 goto out_sockmap;
1026 }
1027
1028 err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_ANY);
1029 if (err) {
1030 printf("Failed sockmap update new slot BPF_ANY\n");
1031 goto out_sockmap;
1032 }
1033
1034 err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_EXIST);
1035 if (err) {
1036 printf("Failed sockmap update new slot BPF_EXIST\n");
1037 goto out_sockmap;
1038 }
1039
1040 /* Delete the elems without programs */
1041 for (i = 2; i < 6; i++) {
1042 err = bpf_map_delete_elem(fd, &i);
1043 if (err) {
1044 printf("Failed delete sockmap %i '%i:%i'\n",
1045 err, i, sfd[i]);
1046 }
1047 }
1048
1049 /* Test having multiple maps open and set with programs on same fds */
1050 err = bpf_prog_attach(parse_prog, fd,
1051 BPF_SK_SKB_STREAM_PARSER, 0);
1052 if (err) {
1053 printf("Failed fd bpf parse prog attach\n");
1054 goto out_sockmap;
1055 }
1056 err = bpf_prog_attach(verdict_prog, fd,
1057 BPF_SK_SKB_STREAM_VERDICT, 0);
1058 if (err) {
1059 printf("Failed fd bpf verdict prog attach\n");
1060 goto out_sockmap;
1061 }
1062
1063 for (i = 4; i < 6; i++) {
1064 err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_ANY);
1065 if (!err) {
1066 printf("Failed allowed duplicate programs in update ANY sockmap %i '%i:%i'\n",
1067 err, i, sfd[i]);
1068 goto out_sockmap;
1069 }
1070 err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_NOEXIST);
1071 if (!err) {
1072 printf("Failed allowed duplicate program in update NOEXIST sockmap %i '%i:%i'\n",
1073 err, i, sfd[i]);
1074 goto out_sockmap;
1075 }
1076 err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_EXIST);
1077 if (!err) {
1078 printf("Failed allowed duplicate program in update EXIST sockmap %i '%i:%i'\n",
1079 err, i, sfd[i]);
1080 goto out_sockmap;
1081 }
1082 }
1083
1084 /* Test tasks number of forked operations */
1085 for (i = 0; i < tasks; i++) {
1086 pid[i] = fork();
1087 if (pid[i] == 0) {
1088 for (i = 0; i < 6; i++) {
1089 bpf_map_delete_elem(map_fd_tx, &i);
1090 bpf_map_delete_elem(map_fd_rx, &i);
1091 bpf_map_update_elem(map_fd_tx, &i,
1092 &sfd[i], BPF_ANY);
1093 bpf_map_update_elem(map_fd_rx, &i,
1094 &sfd[i], BPF_ANY);
1095 }
1096 exit(0);
1097 } else if (pid[i] == -1) {
1098 printf("Couldn't spawn #%d process!\n", i);
1099 exit(1);
1100 }
1101 }
1102
1103 for (i = 0; i < tasks; i++) {
1104 int status;
1105
1106 assert(waitpid(pid[i], &status, 0) == pid[i]);
1107 assert(status == 0);
1108 }
1109
1110 err = bpf_prog_detach2(parse_prog, map_fd_rx, __MAX_BPF_ATTACH_TYPE);
1111 if (!err) {
1112 printf("Detached an invalid prog type.\n");
1113 goto out_sockmap;
1114 }
1115
1116 err = bpf_prog_detach2(parse_prog, map_fd_rx, BPF_SK_SKB_STREAM_PARSER);
1117 if (err) {
1118 printf("Failed parser prog detach\n");
1119 goto out_sockmap;
1120 }
1121
1122 err = bpf_prog_detach2(verdict_prog, map_fd_rx, BPF_SK_SKB_STREAM_VERDICT);
1123 if (err) {
1124 printf("Failed parser prog detach\n");
1125 goto out_sockmap;
1126 }
1127
1128 /* Test map close sockets and empty maps */
1129 for (i = 0; i < 6; i++) {
1130 bpf_map_delete_elem(map_fd_tx, &i);
1131 bpf_map_delete_elem(map_fd_rx, &i);
1132 close(sfd[i]);
1133 }
1134 close(fd);
1135 close(map_fd_rx);
1136 bpf_object__close(obj);
1137 return;
1138out:
1139 for (i = 0; i < 6; i++)
1140 close(sfd[i]);
1141 printf("Failed to create sockmap '%i:%s'!\n", i, strerror(errno));
1142 exit(1);
1143out_sockmap:
1144 for (i = 0; i < 6; i++) {
1145 if (map_fd_tx)
1146 bpf_map_delete_elem(map_fd_tx, &i);
1147 if (map_fd_rx)
1148 bpf_map_delete_elem(map_fd_rx, &i);
1149 close(sfd[i]);
1150 }
1151 close(fd);
1152 exit(1);
1153}
1154
1155#define MAPINMAP_PROG "./test_map_in_map.o"
1156static void test_map_in_map(void)
1157{
1158 struct bpf_object *obj;
1159 struct bpf_map *map;
1160 int mim_fd, fd, err;
1161 int pos = 0;
1162
1163 obj = bpf_object__open(MAPINMAP_PROG);
1164
1165 fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(int), sizeof(int),
1166 2, 0);
1167 if (fd < 0) {
1168 printf("Failed to create hashmap '%s'!\n", strerror(errno));
1169 exit(1);
1170 }
1171
1172 map = bpf_object__find_map_by_name(obj, "mim_array");
1173 if (!map) {
1174 printf("Failed to load array of maps from test prog\n");
1175 goto out_map_in_map;
1176 }
1177 err = bpf_map__set_inner_map_fd(map, fd);
1178 if (err) {
1179 printf("Failed to set inner_map_fd for array of maps\n");
1180 goto out_map_in_map;
1181 }
1182
1183 map = bpf_object__find_map_by_name(obj, "mim_hash");
1184 if (!map) {
1185 printf("Failed to load hash of maps from test prog\n");
1186 goto out_map_in_map;
1187 }
1188 err = bpf_map__set_inner_map_fd(map, fd);
1189 if (err) {
1190 printf("Failed to set inner_map_fd for hash of maps\n");
1191 goto out_map_in_map;
1192 }
1193
1194 bpf_object__load(obj);
1195
1196 map = bpf_object__find_map_by_name(obj, "mim_array");
1197 if (!map) {
1198 printf("Failed to load array of maps from test prog\n");
1199 goto out_map_in_map;
1200 }
1201 mim_fd = bpf_map__fd(map);
1202 if (mim_fd < 0) {
1203 printf("Failed to get descriptor for array of maps\n");
1204 goto out_map_in_map;
1205 }
1206
1207 err = bpf_map_update_elem(mim_fd, &pos, &fd, 0);
1208 if (err) {
1209 printf("Failed to update array of maps\n");
1210 goto out_map_in_map;
1211 }
1212
1213 map = bpf_object__find_map_by_name(obj, "mim_hash");
1214 if (!map) {
1215 printf("Failed to load hash of maps from test prog\n");
1216 goto out_map_in_map;
1217 }
1218 mim_fd = bpf_map__fd(map);
1219 if (mim_fd < 0) {
1220 printf("Failed to get descriptor for hash of maps\n");
1221 goto out_map_in_map;
1222 }
1223
1224 err = bpf_map_update_elem(mim_fd, &pos, &fd, 0);
1225 if (err) {
1226 printf("Failed to update hash of maps\n");
1227 goto out_map_in_map;
1228 }
1229
1230 close(fd);
1231 bpf_object__close(obj);
1232 return;
1233
1234out_map_in_map:
1235 close(fd);
1236 exit(1);
1237}
1238
1239#define MAP_SIZE (32 * 1024)
1240
1241static void test_map_large(void)
1242{
1243
1244 struct bigkey {
1245 int a;
1246 char b[4096];
1247 long long c;
1248 } key;
1249 int fd, i, value;
1250
1251 fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value),
1252 MAP_SIZE, map_flags);
1253 if (fd < 0) {
1254 printf("Failed to create large map '%s'!\n", strerror(errno));
1255 exit(1);
1256 }
1257
1258 for (i = 0; i < MAP_SIZE; i++) {
1259 key = (struct bigkey) { .c = i };
1260 value = i;
1261
1262 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == 0);
1263 }
1264
1265 key.c = -1;
1266 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) < 0 &&
1267 errno == E2BIG);
1268
1269 /* Iterate through all elements. */
1270 assert(bpf_map_get_next_key(fd, NULL, &key) == 0);
1271 key.c = -1;
1272 for (i = 0; i < MAP_SIZE; i++)
1273 assert(bpf_map_get_next_key(fd, &key, &key) == 0);
1274 assert(bpf_map_get_next_key(fd, &key, &key) < 0 && errno == ENOENT);
1275
1276 key.c = 0;
1277 assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && value == 0);
1278 key.a = 1;
1279 assert(bpf_map_lookup_elem(fd, &key, &value) < 0 && errno == ENOENT);
1280
1281 close(fd);
1282}
1283
1284#define run_parallel(N, FN, DATA) \
1285 printf("Fork %u tasks to '" #FN "'\n", N); \
1286 __run_parallel(N, FN, DATA)
1287
1288static void __run_parallel(unsigned int tasks,
1289 void (*fn)(unsigned int task, void *data),
1290 void *data)
1291{
1292 pid_t pid[tasks];
1293 int i;
1294
1295 fflush(stdout);
1296
1297 for (i = 0; i < tasks; i++) {
1298 pid[i] = fork();
1299 if (pid[i] == 0) {
1300 fn(i, data);
1301 exit(0);
1302 } else if (pid[i] == -1) {
1303 printf("Couldn't spawn #%d process!\n", i);
1304 exit(1);
1305 }
1306 }
1307
1308 for (i = 0; i < tasks; i++) {
1309 int status;
1310
1311 assert(waitpid(pid[i], &status, 0) == pid[i]);
1312 assert(status == 0);
1313 }
1314}
1315
1316static void test_map_stress(void)
1317{
1318 run_parallel(100, test_hashmap, NULL);
1319 run_parallel(100, test_hashmap_percpu, NULL);
1320 run_parallel(100, test_hashmap_sizes, NULL);
1321 run_parallel(100, test_hashmap_walk, NULL);
1322
1323 run_parallel(100, test_arraymap, NULL);
1324 run_parallel(100, test_arraymap_percpu, NULL);
1325}
1326
1327#define TASKS 1024
1328
1329#define DO_UPDATE 1
1330#define DO_DELETE 0
1331
1332#define MAP_RETRIES 20
1333
1334static int map_update_retriable(int map_fd, const void *key, const void *value,
1335 int flags, int attempts)
1336{
1337 while (bpf_map_update_elem(map_fd, key, value, flags)) {
1338 if (!attempts || (errno != EAGAIN && errno != EBUSY))
1339 return -errno;
1340
1341 usleep(1);
1342 attempts--;
1343 }
1344
1345 return 0;
1346}
1347
1348static int map_delete_retriable(int map_fd, const void *key, int attempts)
1349{
1350 while (bpf_map_delete_elem(map_fd, key)) {
1351 if (!attempts || (errno != EAGAIN && errno != EBUSY))
1352 return -errno;
1353
1354 usleep(1);
1355 attempts--;
1356 }
1357
1358 return 0;
1359}
1360
1361static void test_update_delete(unsigned int fn, void *data)
1362{
1363 int do_update = ((int *)data)[1];
1364 int fd = ((int *)data)[0];
1365 int i, key, value, err;
1366
1367 for (i = fn; i < MAP_SIZE; i += TASKS) {
1368 key = value = i;
1369
1370 if (do_update) {
1371 err = map_update_retriable(fd, &key, &value, BPF_NOEXIST, MAP_RETRIES);
1372 if (err)
1373 printf("error %d %d\n", err, errno);
1374 assert(err == 0);
1375 err = map_update_retriable(fd, &key, &value, BPF_EXIST, MAP_RETRIES);
1376 if (err)
1377 printf("error %d %d\n", err, errno);
1378 assert(err == 0);
1379 } else {
1380 err = map_delete_retriable(fd, &key, MAP_RETRIES);
1381 if (err)
1382 printf("error %d %d\n", err, errno);
1383 assert(err == 0);
1384 }
1385 }
1386}
1387
1388static void test_map_parallel(void)
1389{
1390 int i, fd, key = 0, value = 0;
1391 int data[2];
1392
1393 fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value),
1394 MAP_SIZE, map_flags);
1395 if (fd < 0) {
1396 printf("Failed to create map for parallel test '%s'!\n",
1397 strerror(errno));
1398 exit(1);
1399 }
1400
1401 /* Use the same fd in children to add elements to this map:
1402 * child_0 adds key=0, key=1024, key=2048, ...
1403 * child_1 adds key=1, key=1025, key=2049, ...
1404 * child_1023 adds key=1023, ...
1405 */
1406 data[0] = fd;
1407 data[1] = DO_UPDATE;
1408 run_parallel(TASKS, test_update_delete, data);
1409
1410 /* Check that key=0 is already there. */
1411 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) < 0 &&
1412 errno == EEXIST);
1413
1414 /* Check that all elements were inserted. */
1415 assert(bpf_map_get_next_key(fd, NULL, &key) == 0);
1416 key = -1;
1417 for (i = 0; i < MAP_SIZE; i++)
1418 assert(bpf_map_get_next_key(fd, &key, &key) == 0);
1419 assert(bpf_map_get_next_key(fd, &key, &key) < 0 && errno == ENOENT);
1420
1421 /* Another check for all elements */
1422 for (i = 0; i < MAP_SIZE; i++) {
1423 key = MAP_SIZE - i - 1;
1424
1425 assert(bpf_map_lookup_elem(fd, &key, &value) == 0 &&
1426 value == key);
1427 }
1428
1429 /* Now let's delete all elemenets in parallel. */
1430 data[1] = DO_DELETE;
1431 run_parallel(TASKS, test_update_delete, data);
1432
1433 /* Nothing should be left. */
1434 key = -1;
1435 assert(bpf_map_get_next_key(fd, NULL, &key) < 0 && errno == ENOENT);
1436 assert(bpf_map_get_next_key(fd, &key, &key) < 0 && errno == ENOENT);
1437}
1438
1439static void test_map_rdonly(void)
1440{
1441 int fd, key = 0, value = 0;
1442
1443 fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value),
1444 MAP_SIZE, map_flags | BPF_F_RDONLY);
1445 if (fd < 0) {
1446 printf("Failed to create map for read only test '%s'!\n",
1447 strerror(errno));
1448 exit(1);
1449 }
1450
1451 key = 1;
1452 value = 1234;
1453 /* Try to insert key=1 element. */
1454 assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) < 0 &&
1455 errno == EPERM);
1456
1457 /* Check that key=1 is not found. */
1458 assert(bpf_map_lookup_elem(fd, &key, &value) < 0 && errno == ENOENT);
1459 assert(bpf_map_get_next_key(fd, &key, &value) < 0 && errno == ENOENT);
1460
1461 close(fd);
1462}
1463
1464static void test_map_wronly_hash(void)
1465{
1466 int fd, key = 0, value = 0;
1467
1468 fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value),
1469 MAP_SIZE, map_flags | BPF_F_WRONLY);
1470 if (fd < 0) {
1471 printf("Failed to create map for write only test '%s'!\n",
1472 strerror(errno));
1473 exit(1);
1474 }
1475
1476 key = 1;
1477 value = 1234;
1478 /* Insert key=1 element. */
1479 assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == 0);
1480
1481 /* Check that reading elements and keys from the map is not allowed. */
1482 assert(bpf_map_lookup_elem(fd, &key, &value) < 0 && errno == EPERM);
1483 assert(bpf_map_get_next_key(fd, &key, &value) < 0 && errno == EPERM);
1484
1485 close(fd);
1486}
1487
1488static void test_map_wronly_stack_or_queue(enum bpf_map_type map_type)
1489{
1490 int fd, value = 0;
1491
1492 assert(map_type == BPF_MAP_TYPE_QUEUE ||
1493 map_type == BPF_MAP_TYPE_STACK);
1494 fd = bpf_create_map(map_type, 0, sizeof(value), MAP_SIZE,
1495 map_flags | BPF_F_WRONLY);
1496 /* Stack/Queue maps do not support BPF_F_NO_PREALLOC */
1497 if (map_flags & BPF_F_NO_PREALLOC) {
1498 assert(fd < 0 && errno == EINVAL);
1499 return;
1500 }
1501 if (fd < 0) {
1502 printf("Failed to create map '%s'!\n", strerror(errno));
1503 exit(1);
1504 }
1505
1506 value = 1234;
1507 assert(bpf_map_update_elem(fd, NULL, &value, BPF_ANY) == 0);
1508
1509 /* Peek element should fail */
1510 assert(bpf_map_lookup_elem(fd, NULL, &value) < 0 && errno == EPERM);
1511
1512 /* Pop element should fail */
1513 assert(bpf_map_lookup_and_delete_elem(fd, NULL, &value) < 0 &&
1514 errno == EPERM);
1515
1516 close(fd);
1517}
1518
1519static void test_map_wronly(void)
1520{
1521 test_map_wronly_hash();
1522 test_map_wronly_stack_or_queue(BPF_MAP_TYPE_STACK);
1523 test_map_wronly_stack_or_queue(BPF_MAP_TYPE_QUEUE);
1524}
1525
1526static void prepare_reuseport_grp(int type, int map_fd, size_t map_elem_size,
1527 __s64 *fds64, __u64 *sk_cookies,
1528 unsigned int n)
1529{
1530 socklen_t optlen, addrlen;
1531 struct sockaddr_in6 s6;
1532 const __u32 index0 = 0;
1533 const int optval = 1;
1534 unsigned int i;
1535 u64 sk_cookie;
1536 void *value;
1537 __s32 fd32;
1538 __s64 fd64;
1539 int err;
1540
1541 s6.sin6_family = AF_INET6;
1542 s6.sin6_addr = in6addr_any;
1543 s6.sin6_port = 0;
1544 addrlen = sizeof(s6);
1545 optlen = sizeof(sk_cookie);
1546
1547 for (i = 0; i < n; i++) {
1548 fd64 = socket(AF_INET6, type, 0);
1549 CHECK(fd64 == -1, "socket()",
1550 "sock_type:%d fd64:%lld errno:%d\n",
1551 type, fd64, errno);
1552
1553 err = setsockopt(fd64, SOL_SOCKET, SO_REUSEPORT,
1554 &optval, sizeof(optval));
1555 CHECK(err == -1, "setsockopt(SO_REUSEPORT)",
1556 "err:%d errno:%d\n", err, errno);
1557
1558 /* reuseport_array does not allow unbound sk */
1559 if (map_elem_size == sizeof(__u64))
1560 value = &fd64;
1561 else {
1562 assert(map_elem_size == sizeof(__u32));
1563 fd32 = (__s32)fd64;
1564 value = &fd32;
1565 }
1566 err = bpf_map_update_elem(map_fd, &index0, value, BPF_ANY);
1567 CHECK(err >= 0 || errno != EINVAL,
1568 "reuseport array update unbound sk",
1569 "sock_type:%d err:%d errno:%d\n",
1570 type, err, errno);
1571
1572 err = bind(fd64, (struct sockaddr *)&s6, sizeof(s6));
1573 CHECK(err == -1, "bind()",
1574 "sock_type:%d err:%d errno:%d\n", type, err, errno);
1575
1576 if (i == 0) {
1577 err = getsockname(fd64, (struct sockaddr *)&s6,
1578 &addrlen);
1579 CHECK(err == -1, "getsockname()",
1580 "sock_type:%d err:%d errno:%d\n",
1581 type, err, errno);
1582 }
1583
1584 err = getsockopt(fd64, SOL_SOCKET, SO_COOKIE, &sk_cookie,
1585 &optlen);
1586 CHECK(err == -1, "getsockopt(SO_COOKIE)",
1587 "sock_type:%d err:%d errno:%d\n", type, err, errno);
1588
1589 if (type == SOCK_STREAM) {
1590 /*
1591 * reuseport_array does not allow
1592 * non-listening tcp sk.
1593 */
1594 err = bpf_map_update_elem(map_fd, &index0, value,
1595 BPF_ANY);
1596 CHECK(err >= 0 || errno != EINVAL,
1597 "reuseport array update non-listening sk",
1598 "sock_type:%d err:%d errno:%d\n",
1599 type, err, errno);
1600 err = listen(fd64, 0);
1601 CHECK(err == -1, "listen()",
1602 "sock_type:%d, err:%d errno:%d\n",
1603 type, err, errno);
1604 }
1605
1606 fds64[i] = fd64;
1607 sk_cookies[i] = sk_cookie;
1608 }
1609}
1610
1611static void test_reuseport_array(void)
1612{
1613#define REUSEPORT_FD_IDX(err, last) ({ (err) ? last : !last; })
1614
1615 const __u32 array_size = 4, index0 = 0, index3 = 3;
1616 int types[2] = { SOCK_STREAM, SOCK_DGRAM }, type;
1617 __u64 grpa_cookies[2], sk_cookie, map_cookie;
1618 __s64 grpa_fds64[2] = { -1, -1 }, fd64 = -1;
1619 const __u32 bad_index = array_size;
1620 int map_fd, err, t, f;
1621 __u32 fds_idx = 0;
1622 int fd;
1623
1624 map_fd = bpf_create_map(BPF_MAP_TYPE_REUSEPORT_SOCKARRAY,
1625 sizeof(__u32), sizeof(__u64), array_size, 0);
1626 CHECK(map_fd < 0, "reuseport array create",
1627 "map_fd:%d, errno:%d\n", map_fd, errno);
1628
1629 /* Test lookup/update/delete with invalid index */
1630 err = bpf_map_delete_elem(map_fd, &bad_index);
1631 CHECK(err >= 0 || errno != E2BIG, "reuseport array del >=max_entries",
1632 "err:%d errno:%d\n", err, errno);
1633
1634 err = bpf_map_update_elem(map_fd, &bad_index, &fd64, BPF_ANY);
1635 CHECK(err >= 0 || errno != E2BIG,
1636 "reuseport array update >=max_entries",
1637 "err:%d errno:%d\n", err, errno);
1638
1639 err = bpf_map_lookup_elem(map_fd, &bad_index, &map_cookie);
1640 CHECK(err >= 0 || errno != ENOENT,
1641 "reuseport array update >=max_entries",
1642 "err:%d errno:%d\n", err, errno);
1643
1644 /* Test lookup/delete non existence elem */
1645 err = bpf_map_lookup_elem(map_fd, &index3, &map_cookie);
1646 CHECK(err >= 0 || errno != ENOENT,
1647 "reuseport array lookup not-exist elem",
1648 "err:%d errno:%d\n", err, errno);
1649 err = bpf_map_delete_elem(map_fd, &index3);
1650 CHECK(err >= 0 || errno != ENOENT,
1651 "reuseport array del not-exist elem",
1652 "err:%d errno:%d\n", err, errno);
1653
1654 for (t = 0; t < ARRAY_SIZE(types); t++) {
1655 type = types[t];
1656
1657 prepare_reuseport_grp(type, map_fd, sizeof(__u64), grpa_fds64,
1658 grpa_cookies, ARRAY_SIZE(grpa_fds64));
1659
1660 /* Test BPF_* update flags */
1661 /* BPF_EXIST failure case */
1662 err = bpf_map_update_elem(map_fd, &index3, &grpa_fds64[fds_idx],
1663 BPF_EXIST);
1664 CHECK(err >= 0 || errno != ENOENT,
1665 "reuseport array update empty elem BPF_EXIST",
1666 "sock_type:%d err:%d errno:%d\n",
1667 type, err, errno);
1668 fds_idx = REUSEPORT_FD_IDX(err, fds_idx);
1669
1670 /* BPF_NOEXIST success case */
1671 err = bpf_map_update_elem(map_fd, &index3, &grpa_fds64[fds_idx],
1672 BPF_NOEXIST);
1673 CHECK(err < 0,
1674 "reuseport array update empty elem BPF_NOEXIST",
1675 "sock_type:%d err:%d errno:%d\n",
1676 type, err, errno);
1677 fds_idx = REUSEPORT_FD_IDX(err, fds_idx);
1678
1679 /* BPF_EXIST success case. */
1680 err = bpf_map_update_elem(map_fd, &index3, &grpa_fds64[fds_idx],
1681 BPF_EXIST);
1682 CHECK(err < 0,
1683 "reuseport array update same elem BPF_EXIST",
1684 "sock_type:%d err:%d errno:%d\n", type, err, errno);
1685 fds_idx = REUSEPORT_FD_IDX(err, fds_idx);
1686
1687 /* BPF_NOEXIST failure case */
1688 err = bpf_map_update_elem(map_fd, &index3, &grpa_fds64[fds_idx],
1689 BPF_NOEXIST);
1690 CHECK(err >= 0 || errno != EEXIST,
1691 "reuseport array update non-empty elem BPF_NOEXIST",
1692 "sock_type:%d err:%d errno:%d\n",
1693 type, err, errno);
1694 fds_idx = REUSEPORT_FD_IDX(err, fds_idx);
1695
1696 /* BPF_ANY case (always succeed) */
1697 err = bpf_map_update_elem(map_fd, &index3, &grpa_fds64[fds_idx],
1698 BPF_ANY);
1699 CHECK(err < 0,
1700 "reuseport array update same sk with BPF_ANY",
1701 "sock_type:%d err:%d errno:%d\n", type, err, errno);
1702
1703 fd64 = grpa_fds64[fds_idx];
1704 sk_cookie = grpa_cookies[fds_idx];
1705
1706 /* The same sk cannot be added to reuseport_array twice */
1707 err = bpf_map_update_elem(map_fd, &index3, &fd64, BPF_ANY);
1708 CHECK(err >= 0 || errno != EBUSY,
1709 "reuseport array update same sk with same index",
1710 "sock_type:%d err:%d errno:%d\n",
1711 type, err, errno);
1712
1713 err = bpf_map_update_elem(map_fd, &index0, &fd64, BPF_ANY);
1714 CHECK(err >= 0 || errno != EBUSY,
1715 "reuseport array update same sk with different index",
1716 "sock_type:%d err:%d errno:%d\n",
1717 type, err, errno);
1718
1719 /* Test delete elem */
1720 err = bpf_map_delete_elem(map_fd, &index3);
1721 CHECK(err < 0, "reuseport array delete sk",
1722 "sock_type:%d err:%d errno:%d\n",
1723 type, err, errno);
1724
1725 /* Add it back with BPF_NOEXIST */
1726 err = bpf_map_update_elem(map_fd, &index3, &fd64, BPF_NOEXIST);
1727 CHECK(err < 0,
1728 "reuseport array re-add with BPF_NOEXIST after del",
1729 "sock_type:%d err:%d errno:%d\n", type, err, errno);
1730
1731 /* Test cookie */
1732 err = bpf_map_lookup_elem(map_fd, &index3, &map_cookie);
1733 CHECK(err < 0 || sk_cookie != map_cookie,
1734 "reuseport array lookup re-added sk",
1735 "sock_type:%d err:%d errno:%d sk_cookie:0x%llx map_cookie:0x%llxn",
1736 type, err, errno, sk_cookie, map_cookie);
1737
1738 /* Test elem removed by close() */
1739 for (f = 0; f < ARRAY_SIZE(grpa_fds64); f++)
1740 close(grpa_fds64[f]);
1741 err = bpf_map_lookup_elem(map_fd, &index3, &map_cookie);
1742 CHECK(err >= 0 || errno != ENOENT,
1743 "reuseport array lookup after close()",
1744 "sock_type:%d err:%d errno:%d\n",
1745 type, err, errno);
1746 }
1747
1748 /* Test SOCK_RAW */
1749 fd64 = socket(AF_INET6, SOCK_RAW, IPPROTO_UDP);
1750 CHECK(fd64 == -1, "socket(SOCK_RAW)", "err:%d errno:%d\n",
1751 err, errno);
1752 err = bpf_map_update_elem(map_fd, &index3, &fd64, BPF_NOEXIST);
1753 CHECK(err >= 0 || errno != ENOTSUPP, "reuseport array update SOCK_RAW",
1754 "err:%d errno:%d\n", err, errno);
1755 close(fd64);
1756
1757 /* Close the 64 bit value map */
1758 close(map_fd);
1759
1760 /* Test 32 bit fd */
1761 map_fd = bpf_create_map(BPF_MAP_TYPE_REUSEPORT_SOCKARRAY,
1762 sizeof(__u32), sizeof(__u32), array_size, 0);
1763 CHECK(map_fd < 0, "reuseport array create",
1764 "map_fd:%d, errno:%d\n", map_fd, errno);
1765 prepare_reuseport_grp(SOCK_STREAM, map_fd, sizeof(__u32), &fd64,
1766 &sk_cookie, 1);
1767 fd = fd64;
1768 err = bpf_map_update_elem(map_fd, &index3, &fd, BPF_NOEXIST);
1769 CHECK(err < 0, "reuseport array update 32 bit fd",
1770 "err:%d errno:%d\n", err, errno);
1771 err = bpf_map_lookup_elem(map_fd, &index3, &map_cookie);
1772 CHECK(err >= 0 || errno != ENOSPC,
1773 "reuseport array lookup 32 bit fd",
1774 "err:%d errno:%d\n", err, errno);
1775 close(fd);
1776 close(map_fd);
1777}
1778
1779static void run_all_tests(void)
1780{
1781 test_hashmap(0, NULL);
1782 test_hashmap_percpu(0, NULL);
1783 test_hashmap_walk(0, NULL);
1784 test_hashmap_zero_seed();
1785
1786 test_arraymap(0, NULL);
1787 test_arraymap_percpu(0, NULL);
1788
1789 test_arraymap_percpu_many_keys();
1790
1791 test_devmap(0, NULL);
1792 test_devmap_hash(0, NULL);
1793 test_sockmap(0, NULL);
1794
1795 test_map_large();
1796 test_map_parallel();
1797 test_map_stress();
1798
1799 test_map_rdonly();
1800 test_map_wronly();
1801
1802 test_reuseport_array();
1803
1804 test_queuemap(0, NULL);
1805 test_stackmap(0, NULL);
1806
1807 test_map_in_map();
1808}
1809
1810#define DEFINE_TEST(name) extern void test_##name(void);
1811#include <map_tests/tests.h>
1812#undef DEFINE_TEST
1813
1814int main(void)
1815{
1816 srand(time(NULL));
1817
1818 libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
1819
1820 map_flags = 0;
1821 run_all_tests();
1822
1823 map_flags = BPF_F_NO_PREALLOC;
1824 run_all_tests();
1825
1826#define DEFINE_TEST(name) test_##name();
1827#include <map_tests/tests.h>
1828#undef DEFINE_TEST
1829
1830 printf("test_maps: OK, %d SKIPPED\n", skips);
1831 return 0;
1832}