1 // SPDX-License-Identifier: GPL-2.0-only
3 * Testsuite for eBPF maps
5 * Copyright (c) 2014 PLUMgrid, http://plumgrid.com
6 * Copyright (c) 2016 Facebook
18 #include <sys/socket.h>
19 #include <netinet/in.h>
20 #include <linux/bpf.h>
23 #include <bpf/libbpf.h>
26 #include "bpf_rlimit.h"
27 #include "test_maps.h"
37 static void test_hashmap(unsigned int task
, void *data
)
39 long long key
, next_key
, first_key
, value
;
42 fd
= bpf_create_map(BPF_MAP_TYPE_HASH
, sizeof(key
), sizeof(value
),
45 printf("Failed to create hashmap '%s'!\n", strerror(errno
));
51 /* Insert key=1 element. */
52 assert(bpf_map_update_elem(fd
, &key
, &value
, BPF_ANY
) == 0);
55 /* BPF_NOEXIST means add new element if it doesn't exist. */
56 assert(bpf_map_update_elem(fd
, &key
, &value
, BPF_NOEXIST
) == -1 &&
57 /* key=1 already exists. */
60 /* -1 is an invalid flag. */
61 assert(bpf_map_update_elem(fd
, &key
, &value
, -1) == -1 &&
64 /* Check that key=1 can be found. */
65 assert(bpf_map_lookup_elem(fd
, &key
, &value
) == 0 && value
== 1234);
68 /* Check that key=2 is not found. */
69 assert(bpf_map_lookup_elem(fd
, &key
, &value
) == -1 && errno
== ENOENT
);
71 /* BPF_EXIST means update existing element. */
72 assert(bpf_map_update_elem(fd
, &key
, &value
, BPF_EXIST
) == -1 &&
73 /* key=2 is not there. */
76 /* Insert key=2 element. */
77 assert(bpf_map_update_elem(fd
, &key
, &value
, BPF_NOEXIST
) == 0);
79 /* key=1 and key=2 were inserted, check that key=0 cannot be
80 * inserted due to max_entries limit.
83 assert(bpf_map_update_elem(fd
, &key
, &value
, BPF_NOEXIST
) == -1 &&
86 /* Update existing element, though the map is full. */
88 assert(bpf_map_update_elem(fd
, &key
, &value
, BPF_EXIST
) == 0);
90 assert(bpf_map_update_elem(fd
, &key
, &value
, BPF_ANY
) == 0);
92 assert(bpf_map_update_elem(fd
, &key
, &value
, BPF_NOEXIST
) == -1 &&
95 /* Check that key = 0 doesn't exist. */
97 assert(bpf_map_delete_elem(fd
, &key
) == -1 && errno
== ENOENT
);
99 /* Iterate over two elements. */
100 assert(bpf_map_get_next_key(fd
, NULL
, &first_key
) == 0 &&
101 (first_key
== 1 || first_key
== 2));
102 assert(bpf_map_get_next_key(fd
, &key
, &next_key
) == 0 &&
103 (next_key
== first_key
));
104 assert(bpf_map_get_next_key(fd
, &next_key
, &next_key
) == 0 &&
105 (next_key
== 1 || next_key
== 2) &&
106 (next_key
!= first_key
));
107 assert(bpf_map_get_next_key(fd
, &next_key
, &next_key
) == -1 &&
110 /* Delete both elements. */
112 assert(bpf_map_delete_elem(fd
, &key
) == 0);
114 assert(bpf_map_delete_elem(fd
, &key
) == 0);
115 assert(bpf_map_delete_elem(fd
, &key
) == -1 && errno
== ENOENT
);
118 /* Check that map is empty. */
119 assert(bpf_map_get_next_key(fd
, NULL
, &next_key
) == -1 &&
121 assert(bpf_map_get_next_key(fd
, &key
, &next_key
) == -1 &&
127 static void test_hashmap_sizes(unsigned int task
, void *data
)
131 for (i
= 1; i
<= 512; i
<<= 1)
132 for (j
= 1; j
<= 1 << 18; j
<<= 1) {
133 fd
= bpf_create_map(BPF_MAP_TYPE_HASH
, i
, j
,
138 printf("Failed to create hashmap key=%d value=%d '%s'\n",
139 i
, j
, strerror(errno
));
143 usleep(10); /* give kernel time to destroy */
147 static void test_hashmap_percpu(unsigned int task
, void *data
)
149 unsigned int nr_cpus
= bpf_num_possible_cpus();
150 BPF_DECLARE_PERCPU(long, value
);
151 long long key
, next_key
, first_key
;
152 int expected_key_mask
= 0;
155 fd
= bpf_create_map(BPF_MAP_TYPE_PERCPU_HASH
, sizeof(key
),
156 sizeof(bpf_percpu(value
, 0)), 2, map_flags
);
158 printf("Failed to create hashmap '%s'!\n", strerror(errno
));
162 for (i
= 0; i
< nr_cpus
; i
++)
163 bpf_percpu(value
, i
) = i
+ 100;
166 /* Insert key=1 element. */
167 assert(!(expected_key_mask
& key
));
168 assert(bpf_map_update_elem(fd
, &key
, value
, BPF_ANY
) == 0);
169 expected_key_mask
|= key
;
171 /* BPF_NOEXIST means add new element if it doesn't exist. */
172 assert(bpf_map_update_elem(fd
, &key
, value
, BPF_NOEXIST
) == -1 &&
173 /* key=1 already exists. */
176 /* -1 is an invalid flag. */
177 assert(bpf_map_update_elem(fd
, &key
, value
, -1) == -1 &&
180 /* Check that key=1 can be found. Value could be 0 if the lookup
181 * was run from a different CPU.
183 bpf_percpu(value
, 0) = 1;
184 assert(bpf_map_lookup_elem(fd
, &key
, value
) == 0 &&
185 bpf_percpu(value
, 0) == 100);
188 /* Check that key=2 is not found. */
189 assert(bpf_map_lookup_elem(fd
, &key
, value
) == -1 && errno
== ENOENT
);
191 /* BPF_EXIST means update existing element. */
192 assert(bpf_map_update_elem(fd
, &key
, value
, BPF_EXIST
) == -1 &&
193 /* key=2 is not there. */
196 /* Insert key=2 element. */
197 assert(!(expected_key_mask
& key
));
198 assert(bpf_map_update_elem(fd
, &key
, value
, BPF_NOEXIST
) == 0);
199 expected_key_mask
|= key
;
201 /* key=1 and key=2 were inserted, check that key=0 cannot be
202 * inserted due to max_entries limit.
205 assert(bpf_map_update_elem(fd
, &key
, value
, BPF_NOEXIST
) == -1 &&
208 /* Check that key = 0 doesn't exist. */
209 assert(bpf_map_delete_elem(fd
, &key
) == -1 && errno
== ENOENT
);
211 /* Iterate over two elements. */
212 assert(bpf_map_get_next_key(fd
, NULL
, &first_key
) == 0 &&
213 ((expected_key_mask
& first_key
) == first_key
));
214 while (!bpf_map_get_next_key(fd
, &key
, &next_key
)) {
216 assert(next_key
== first_key
);
219 assert((expected_key_mask
& next_key
) == next_key
);
220 expected_key_mask
&= ~next_key
;
222 assert(bpf_map_lookup_elem(fd
, &next_key
, value
) == 0);
224 for (i
= 0; i
< nr_cpus
; i
++)
225 assert(bpf_percpu(value
, i
) == i
+ 100);
229 assert(errno
== ENOENT
);
231 /* Update with BPF_EXIST. */
233 assert(bpf_map_update_elem(fd
, &key
, value
, BPF_EXIST
) == 0);
235 /* Delete both elements. */
237 assert(bpf_map_delete_elem(fd
, &key
) == 0);
239 assert(bpf_map_delete_elem(fd
, &key
) == 0);
240 assert(bpf_map_delete_elem(fd
, &key
) == -1 && errno
== ENOENT
);
243 /* Check that map is empty. */
244 assert(bpf_map_get_next_key(fd
, NULL
, &next_key
) == -1 &&
246 assert(bpf_map_get_next_key(fd
, &key
, &next_key
) == -1 &&
252 static int helper_fill_hashmap(int max_entries
)
255 long long key
, value
;
257 fd
= bpf_create_map(BPF_MAP_TYPE_HASH
, sizeof(key
), sizeof(value
),
258 max_entries
, map_flags
);
260 "failed to create hashmap",
261 "err: %s, flags: 0x%x\n", strerror(errno
), map_flags
);
263 for (i
= 0; i
< max_entries
; i
++) {
264 key
= i
; value
= key
;
265 ret
= bpf_map_update_elem(fd
, &key
, &value
, BPF_NOEXIST
);
267 "can't update hashmap",
268 "err: %s\n", strerror(ret
));
274 static void test_hashmap_walk(unsigned int task
, void *data
)
276 int fd
, i
, max_entries
= 1000;
277 long long key
, value
, next_key
;
278 bool next_key_valid
= true;
280 fd
= helper_fill_hashmap(max_entries
);
282 for (i
= 0; bpf_map_get_next_key(fd
, !i
? NULL
: &key
,
283 &next_key
) == 0; i
++) {
285 assert(bpf_map_lookup_elem(fd
, &key
, &value
) == 0);
288 assert(i
== max_entries
);
290 assert(bpf_map_get_next_key(fd
, NULL
, &key
) == 0);
291 for (i
= 0; next_key_valid
; i
++) {
292 next_key_valid
= bpf_map_get_next_key(fd
, &key
, &next_key
) == 0;
293 assert(bpf_map_lookup_elem(fd
, &key
, &value
) == 0);
295 assert(bpf_map_update_elem(fd
, &key
, &value
, BPF_EXIST
) == 0);
299 assert(i
== max_entries
);
301 for (i
= 0; bpf_map_get_next_key(fd
, !i
? NULL
: &key
,
302 &next_key
) == 0; i
++) {
304 assert(bpf_map_lookup_elem(fd
, &key
, &value
) == 0);
305 assert(value
- 1 == key
);
308 assert(i
== max_entries
);
312 static void test_hashmap_zero_seed(void)
314 int i
, first
, second
, old_flags
;
315 long long key
, next_first
, next_second
;
317 old_flags
= map_flags
;
318 map_flags
|= BPF_F_ZERO_SEED
;
320 first
= helper_fill_hashmap(3);
321 second
= helper_fill_hashmap(3);
324 void *key_ptr
= !i
? NULL
: &key
;
326 if (bpf_map_get_next_key(first
, key_ptr
, &next_first
) != 0)
329 CHECK(bpf_map_get_next_key(second
, key_ptr
, &next_second
) != 0,
330 "next_key for second map must succeed",
331 "key_ptr: %p", key_ptr
);
332 CHECK(next_first
!= next_second
,
334 "i: %d first: %lld second: %lld\n", i
,
335 next_first
, next_second
);
340 map_flags
= old_flags
;
345 static void test_arraymap(unsigned int task
, void *data
)
347 int key
, next_key
, fd
;
350 fd
= bpf_create_map(BPF_MAP_TYPE_ARRAY
, sizeof(key
), sizeof(value
),
353 printf("Failed to create arraymap '%s'!\n", strerror(errno
));
359 /* Insert key=1 element. */
360 assert(bpf_map_update_elem(fd
, &key
, &value
, BPF_ANY
) == 0);
363 assert(bpf_map_update_elem(fd
, &key
, &value
, BPF_NOEXIST
) == -1 &&
366 /* Check that key=1 can be found. */
367 assert(bpf_map_lookup_elem(fd
, &key
, &value
) == 0 && value
== 1234);
370 /* Check that key=0 is also found and zero initialized. */
371 assert(bpf_map_lookup_elem(fd
, &key
, &value
) == 0 && value
== 0);
373 /* key=0 and key=1 were inserted, check that key=2 cannot be inserted
374 * due to max_entries limit.
377 assert(bpf_map_update_elem(fd
, &key
, &value
, BPF_EXIST
) == -1 &&
380 /* Check that key = 2 doesn't exist. */
381 assert(bpf_map_lookup_elem(fd
, &key
, &value
) == -1 && errno
== ENOENT
);
383 /* Iterate over two elements. */
384 assert(bpf_map_get_next_key(fd
, NULL
, &next_key
) == 0 &&
386 assert(bpf_map_get_next_key(fd
, &key
, &next_key
) == 0 &&
388 assert(bpf_map_get_next_key(fd
, &next_key
, &next_key
) == 0 &&
390 assert(bpf_map_get_next_key(fd
, &next_key
, &next_key
) == -1 &&
393 /* Delete shouldn't succeed. */
395 assert(bpf_map_delete_elem(fd
, &key
) == -1 && errno
== EINVAL
);
400 static void test_arraymap_percpu(unsigned int task
, void *data
)
402 unsigned int nr_cpus
= bpf_num_possible_cpus();
403 BPF_DECLARE_PERCPU(long, values
);
404 int key
, next_key
, fd
, i
;
406 fd
= bpf_create_map(BPF_MAP_TYPE_PERCPU_ARRAY
, sizeof(key
),
407 sizeof(bpf_percpu(values
, 0)), 2, 0);
409 printf("Failed to create arraymap '%s'!\n", strerror(errno
));
413 for (i
= 0; i
< nr_cpus
; i
++)
414 bpf_percpu(values
, i
) = i
+ 100;
417 /* Insert key=1 element. */
418 assert(bpf_map_update_elem(fd
, &key
, values
, BPF_ANY
) == 0);
420 bpf_percpu(values
, 0) = 0;
421 assert(bpf_map_update_elem(fd
, &key
, values
, BPF_NOEXIST
) == -1 &&
424 /* Check that key=1 can be found. */
425 assert(bpf_map_lookup_elem(fd
, &key
, values
) == 0 &&
426 bpf_percpu(values
, 0) == 100);
429 /* Check that key=0 is also found and zero initialized. */
430 assert(bpf_map_lookup_elem(fd
, &key
, values
) == 0 &&
431 bpf_percpu(values
, 0) == 0 &&
432 bpf_percpu(values
, nr_cpus
- 1) == 0);
434 /* Check that key=2 cannot be inserted due to max_entries limit. */
436 assert(bpf_map_update_elem(fd
, &key
, values
, BPF_EXIST
) == -1 &&
439 /* Check that key = 2 doesn't exist. */
440 assert(bpf_map_lookup_elem(fd
, &key
, values
) == -1 && errno
== ENOENT
);
442 /* Iterate over two elements. */
443 assert(bpf_map_get_next_key(fd
, NULL
, &next_key
) == 0 &&
445 assert(bpf_map_get_next_key(fd
, &key
, &next_key
) == 0 &&
447 assert(bpf_map_get_next_key(fd
, &next_key
, &next_key
) == 0 &&
449 assert(bpf_map_get_next_key(fd
, &next_key
, &next_key
) == -1 &&
452 /* Delete shouldn't succeed. */
454 assert(bpf_map_delete_elem(fd
, &key
) == -1 && errno
== EINVAL
);
459 static void test_arraymap_percpu_many_keys(void)
461 unsigned int nr_cpus
= bpf_num_possible_cpus();
462 BPF_DECLARE_PERCPU(long, values
);
463 /* nr_keys is not too large otherwise the test stresses percpu
464 * allocator more than anything else
466 unsigned int nr_keys
= 2000;
469 fd
= bpf_create_map(BPF_MAP_TYPE_PERCPU_ARRAY
, sizeof(key
),
470 sizeof(bpf_percpu(values
, 0)), nr_keys
, 0);
472 printf("Failed to create per-cpu arraymap '%s'!\n",
477 for (i
= 0; i
< nr_cpus
; i
++)
478 bpf_percpu(values
, i
) = i
+ 10;
480 for (key
= 0; key
< nr_keys
; key
++)
481 assert(bpf_map_update_elem(fd
, &key
, values
, BPF_ANY
) == 0);
483 for (key
= 0; key
< nr_keys
; key
++) {
484 for (i
= 0; i
< nr_cpus
; i
++)
485 bpf_percpu(values
, i
) = 0;
487 assert(bpf_map_lookup_elem(fd
, &key
, values
) == 0);
489 for (i
= 0; i
< nr_cpus
; i
++)
490 assert(bpf_percpu(values
, i
) == i
+ 10);
496 static void test_devmap(unsigned int task
, void *data
)
501 fd
= bpf_create_map(BPF_MAP_TYPE_DEVMAP
, sizeof(key
), sizeof(value
),
504 printf("Failed to create devmap '%s'!\n", strerror(errno
));
511 static void test_devmap_hash(unsigned int task
, void *data
)
516 fd
= bpf_create_map(BPF_MAP_TYPE_DEVMAP_HASH
, sizeof(key
), sizeof(value
),
519 printf("Failed to create devmap_hash '%s'!\n", strerror(errno
));
526 static void test_queuemap(unsigned int task
, void *data
)
528 const int MAP_SIZE
= 32;
529 __u32 vals
[MAP_SIZE
+ MAP_SIZE
/2], val
;
532 /* Fill test values to be used */
533 for (i
= 0; i
< MAP_SIZE
+ MAP_SIZE
/2; i
++)
536 /* Invalid key size */
537 fd
= bpf_create_map(BPF_MAP_TYPE_QUEUE
, 4, sizeof(val
), MAP_SIZE
,
539 assert(fd
< 0 && errno
== EINVAL
);
541 fd
= bpf_create_map(BPF_MAP_TYPE_QUEUE
, 0, sizeof(val
), MAP_SIZE
,
543 /* Queue map does not support BPF_F_NO_PREALLOC */
544 if (map_flags
& BPF_F_NO_PREALLOC
) {
545 assert(fd
< 0 && errno
== EINVAL
);
549 printf("Failed to create queuemap '%s'!\n", strerror(errno
));
553 /* Push MAP_SIZE elements */
554 for (i
= 0; i
< MAP_SIZE
; i
++)
555 assert(bpf_map_update_elem(fd
, NULL
, &vals
[i
], 0) == 0);
557 /* Check that element cannot be pushed due to max_entries limit */
558 assert(bpf_map_update_elem(fd
, NULL
, &val
, 0) == -1 &&
562 assert(bpf_map_lookup_elem(fd
, NULL
, &val
) == 0 && val
== vals
[0]);
564 /* Replace half elements */
565 for (i
= MAP_SIZE
; i
< MAP_SIZE
+ MAP_SIZE
/2; i
++)
566 assert(bpf_map_update_elem(fd
, NULL
, &vals
[i
], BPF_EXIST
) == 0);
568 /* Pop all elements */
569 for (i
= MAP_SIZE
/2; i
< MAP_SIZE
+ MAP_SIZE
/2; i
++)
570 assert(bpf_map_lookup_and_delete_elem(fd
, NULL
, &val
) == 0 &&
573 /* Check that there are not elements left */
574 assert(bpf_map_lookup_and_delete_elem(fd
, NULL
, &val
) == -1 &&
577 /* Check that non supported functions set errno to EINVAL */
578 assert(bpf_map_delete_elem(fd
, NULL
) == -1 && errno
== EINVAL
);
579 assert(bpf_map_get_next_key(fd
, NULL
, NULL
) == -1 && errno
== EINVAL
);
584 static void test_stackmap(unsigned int task
, void *data
)
586 const int MAP_SIZE
= 32;
587 __u32 vals
[MAP_SIZE
+ MAP_SIZE
/2], val
;
590 /* Fill test values to be used */
591 for (i
= 0; i
< MAP_SIZE
+ MAP_SIZE
/2; i
++)
594 /* Invalid key size */
595 fd
= bpf_create_map(BPF_MAP_TYPE_STACK
, 4, sizeof(val
), MAP_SIZE
,
597 assert(fd
< 0 && errno
== EINVAL
);
599 fd
= bpf_create_map(BPF_MAP_TYPE_STACK
, 0, sizeof(val
), MAP_SIZE
,
601 /* Stack map does not support BPF_F_NO_PREALLOC */
602 if (map_flags
& BPF_F_NO_PREALLOC
) {
603 assert(fd
< 0 && errno
== EINVAL
);
607 printf("Failed to create stackmap '%s'!\n", strerror(errno
));
611 /* Push MAP_SIZE elements */
612 for (i
= 0; i
< MAP_SIZE
; i
++)
613 assert(bpf_map_update_elem(fd
, NULL
, &vals
[i
], 0) == 0);
615 /* Check that element cannot be pushed due to max_entries limit */
616 assert(bpf_map_update_elem(fd
, NULL
, &val
, 0) == -1 &&
620 assert(bpf_map_lookup_elem(fd
, NULL
, &val
) == 0 && val
== vals
[i
- 1]);
622 /* Replace half elements */
623 for (i
= MAP_SIZE
; i
< MAP_SIZE
+ MAP_SIZE
/2; i
++)
624 assert(bpf_map_update_elem(fd
, NULL
, &vals
[i
], BPF_EXIST
) == 0);
626 /* Pop all elements */
627 for (i
= MAP_SIZE
+ MAP_SIZE
/2 - 1; i
>= MAP_SIZE
/2; i
--)
628 assert(bpf_map_lookup_and_delete_elem(fd
, NULL
, &val
) == 0 &&
631 /* Check that there are not elements left */
632 assert(bpf_map_lookup_and_delete_elem(fd
, NULL
, &val
) == -1 &&
635 /* Check that non supported functions set errno to EINVAL */
636 assert(bpf_map_delete_elem(fd
, NULL
) == -1 && errno
== EINVAL
);
637 assert(bpf_map_get_next_key(fd
, NULL
, NULL
) == -1 && errno
== EINVAL
);
642 #include <sys/ioctl.h>
643 #include <arpa/inet.h>
644 #include <sys/select.h>
645 #include <linux/err.h>
646 #define SOCKMAP_PARSE_PROG "./sockmap_parse_prog.o"
647 #define SOCKMAP_VERDICT_PROG "./sockmap_verdict_prog.o"
648 #define SOCKMAP_TCP_MSG_PROG "./sockmap_tcp_msg_prog.o"
649 static void test_sockmap(unsigned int tasks
, void *data
)
651 struct bpf_map
*bpf_map_rx
, *bpf_map_tx
, *bpf_map_msg
, *bpf_map_break
;
652 int map_fd_msg
= 0, map_fd_rx
= 0, map_fd_tx
= 0, map_fd_break
;
653 int ports
[] = {50200, 50201, 50202, 50204};
654 int err
, i
, fd
, udp
, sfd
[6] = {0xdeadbeef};
655 u8 buf
[20] = {0x0, 0x5, 0x3, 0x2, 0x1, 0x0};
656 int parse_prog
, verdict_prog
, msg_prog
;
657 struct sockaddr_in addr
;
658 int one
= 1, s
, sc
, rc
;
659 struct bpf_object
*obj
;
665 /* Create some sockets to use with sockmap */
666 for (i
= 0; i
< 2; i
++) {
667 sfd
[i
] = socket(AF_INET
, SOCK_STREAM
, 0);
670 err
= setsockopt(sfd
[i
], SOL_SOCKET
, SO_REUSEADDR
,
671 (char *)&one
, sizeof(one
));
673 printf("failed to setsockopt\n");
676 err
= ioctl(sfd
[i
], FIONBIO
, (char *)&one
);
678 printf("failed to ioctl\n");
681 memset(&addr
, 0, sizeof(struct sockaddr_in
));
682 addr
.sin_family
= AF_INET
;
683 addr
.sin_addr
.s_addr
= inet_addr("127.0.0.1");
684 addr
.sin_port
= htons(ports
[i
]);
685 err
= bind(sfd
[i
], (struct sockaddr
*)&addr
, sizeof(addr
));
687 printf("failed to bind: err %i: %i:%i\n",
691 err
= listen(sfd
[i
], 32);
693 printf("failed to listen\n");
698 for (i
= 2; i
< 4; i
++) {
699 sfd
[i
] = socket(AF_INET
, SOCK_STREAM
, 0);
702 err
= setsockopt(sfd
[i
], SOL_SOCKET
, SO_REUSEADDR
,
703 (char *)&one
, sizeof(one
));
705 printf("set sock opt\n");
708 memset(&addr
, 0, sizeof(struct sockaddr_in
));
709 addr
.sin_family
= AF_INET
;
710 addr
.sin_addr
.s_addr
= inet_addr("127.0.0.1");
711 addr
.sin_port
= htons(ports
[i
- 2]);
712 err
= connect(sfd
[i
], (struct sockaddr
*)&addr
, sizeof(addr
));
714 printf("failed to connect\n");
720 for (i
= 4; i
< 6; i
++) {
721 sfd
[i
] = accept(sfd
[i
- 4], NULL
, NULL
);
723 printf("accept failed\n");
728 /* Test sockmap with connected sockets */
729 fd
= bpf_create_map(BPF_MAP_TYPE_SOCKMAP
,
730 sizeof(key
), sizeof(value
),
733 if (!bpf_probe_map_type(BPF_MAP_TYPE_SOCKMAP
, 0)) {
734 printf("%s SKIP (unsupported map type BPF_MAP_TYPE_SOCKMAP)\n",
737 for (i
= 0; i
< 6; i
++)
742 printf("Failed to create sockmap %i\n", fd
);
746 /* Test update with unsupported UDP socket */
747 udp
= socket(AF_INET
, SOCK_DGRAM
, 0);
749 err
= bpf_map_update_elem(fd
, &i
, &udp
, BPF_ANY
);
751 printf("Failed socket SOCK_DGRAM allowed '%i:%i'\n",
756 /* Test update without programs */
757 for (i
= 0; i
< 6; i
++) {
758 err
= bpf_map_update_elem(fd
, &i
, &sfd
[i
], BPF_ANY
);
760 printf("Allowed update sockmap '%i:%i' not in ESTABLISHED\n",
763 } else if (i
>= 2 && err
) {
764 printf("Failed noprog update sockmap '%i:%i'\n",
770 /* Test attaching/detaching bad fds */
771 err
= bpf_prog_attach(-1, fd
, BPF_SK_SKB_STREAM_PARSER
, 0);
773 printf("Failed invalid parser prog attach\n");
777 err
= bpf_prog_attach(-1, fd
, BPF_SK_SKB_STREAM_VERDICT
, 0);
779 printf("Failed invalid verdict prog attach\n");
783 err
= bpf_prog_attach(-1, fd
, BPF_SK_MSG_VERDICT
, 0);
785 printf("Failed invalid msg verdict prog attach\n");
789 err
= bpf_prog_attach(-1, fd
, __MAX_BPF_ATTACH_TYPE
, 0);
791 printf("Failed unknown prog attach\n");
795 err
= bpf_prog_detach(fd
, BPF_SK_SKB_STREAM_PARSER
);
797 printf("Failed empty parser prog detach\n");
801 err
= bpf_prog_detach(fd
, BPF_SK_SKB_STREAM_VERDICT
);
803 printf("Failed empty verdict prog detach\n");
807 err
= bpf_prog_detach(fd
, BPF_SK_MSG_VERDICT
);
809 printf("Failed empty msg verdict prog detach\n");
813 err
= bpf_prog_detach(fd
, __MAX_BPF_ATTACH_TYPE
);
815 printf("Detach invalid prog successful\n");
819 /* Load SK_SKB program and Attach */
820 err
= bpf_prog_load(SOCKMAP_PARSE_PROG
,
821 BPF_PROG_TYPE_SK_SKB
, &obj
, &parse_prog
);
823 printf("Failed to load SK_SKB parse prog\n");
827 err
= bpf_prog_load(SOCKMAP_TCP_MSG_PROG
,
828 BPF_PROG_TYPE_SK_MSG
, &obj
, &msg_prog
);
830 printf("Failed to load SK_SKB msg prog\n");
834 err
= bpf_prog_load(SOCKMAP_VERDICT_PROG
,
835 BPF_PROG_TYPE_SK_SKB
, &obj
, &verdict_prog
);
837 printf("Failed to load SK_SKB verdict prog\n");
841 bpf_map_rx
= bpf_object__find_map_by_name(obj
, "sock_map_rx");
842 if (IS_ERR(bpf_map_rx
)) {
843 printf("Failed to load map rx from verdict prog\n");
847 map_fd_rx
= bpf_map__fd(bpf_map_rx
);
849 printf("Failed to get map rx fd\n");
853 bpf_map_tx
= bpf_object__find_map_by_name(obj
, "sock_map_tx");
854 if (IS_ERR(bpf_map_tx
)) {
855 printf("Failed to load map tx from verdict prog\n");
859 map_fd_tx
= bpf_map__fd(bpf_map_tx
);
861 printf("Failed to get map tx fd\n");
865 bpf_map_msg
= bpf_object__find_map_by_name(obj
, "sock_map_msg");
866 if (IS_ERR(bpf_map_msg
)) {
867 printf("Failed to load map msg from msg_verdict prog\n");
871 map_fd_msg
= bpf_map__fd(bpf_map_msg
);
872 if (map_fd_msg
< 0) {
873 printf("Failed to get map msg fd\n");
877 bpf_map_break
= bpf_object__find_map_by_name(obj
, "sock_map_break");
878 if (IS_ERR(bpf_map_break
)) {
879 printf("Failed to load map tx from verdict prog\n");
883 map_fd_break
= bpf_map__fd(bpf_map_break
);
884 if (map_fd_break
< 0) {
885 printf("Failed to get map tx fd\n");
889 err
= bpf_prog_attach(parse_prog
, map_fd_break
,
890 BPF_SK_SKB_STREAM_PARSER
, 0);
892 printf("Allowed attaching SK_SKB program to invalid map\n");
896 err
= bpf_prog_attach(parse_prog
, map_fd_rx
,
897 BPF_SK_SKB_STREAM_PARSER
, 0);
899 printf("Failed stream parser bpf prog attach\n");
903 err
= bpf_prog_attach(verdict_prog
, map_fd_rx
,
904 BPF_SK_SKB_STREAM_VERDICT
, 0);
906 printf("Failed stream verdict bpf prog attach\n");
910 err
= bpf_prog_attach(msg_prog
, map_fd_msg
, BPF_SK_MSG_VERDICT
, 0);
912 printf("Failed msg verdict bpf prog attach\n");
916 err
= bpf_prog_attach(verdict_prog
, map_fd_rx
,
917 __MAX_BPF_ATTACH_TYPE
, 0);
919 printf("Attached unknown bpf prog\n");
923 /* Test map update elem afterwards fd lives in fd and map_fd */
924 for (i
= 2; i
< 6; i
++) {
925 err
= bpf_map_update_elem(map_fd_rx
, &i
, &sfd
[i
], BPF_ANY
);
927 printf("Failed map_fd_rx update sockmap %i '%i:%i'\n",
931 err
= bpf_map_update_elem(map_fd_tx
, &i
, &sfd
[i
], BPF_ANY
);
933 printf("Failed map_fd_tx update sockmap %i '%i:%i'\n",
939 /* Test map delete elem and remove send/recv sockets */
940 for (i
= 2; i
< 4; i
++) {
941 err
= bpf_map_delete_elem(map_fd_rx
, &i
);
943 printf("Failed delete sockmap rx %i '%i:%i'\n",
947 err
= bpf_map_delete_elem(map_fd_tx
, &i
);
949 printf("Failed delete sockmap tx %i '%i:%i'\n",
955 /* Put sfd[2] (sending fd below) into msg map to test sendmsg bpf */
957 err
= bpf_map_update_elem(map_fd_msg
, &i
, &sfd
[2], BPF_ANY
);
959 printf("Failed map_fd_msg update sockmap %i\n", err
);
963 /* Test map send/recv */
964 for (i
= 0; i
< 2; i
++) {
967 sc
= send(sfd
[2], buf
, 20, 0);
969 printf("Failed sockmap send\n");
977 s
= select(sfd
[3] + 1, &w
, NULL
, NULL
, &to
);
979 perror("Failed sockmap select()");
982 printf("Failed sockmap unexpected timeout\n");
986 if (!FD_ISSET(sfd
[3], &w
)) {
987 printf("Failed sockmap select/recv\n");
991 rc
= recv(sfd
[3], buf
, sizeof(buf
), 0);
993 printf("Failed sockmap recv\n");
998 /* Negative null entry lookup from datapath should be dropped */
1001 sc
= send(sfd
[2], buf
, 20, 0);
1003 printf("Failed sockmap send\n");
1007 /* Push fd into same slot */
1009 err
= bpf_map_update_elem(fd
, &i
, &sfd
[i
], BPF_NOEXIST
);
1011 printf("Failed allowed sockmap dup slot BPF_NOEXIST\n");
1015 err
= bpf_map_update_elem(fd
, &i
, &sfd
[i
], BPF_ANY
);
1017 printf("Failed sockmap update new slot BPF_ANY\n");
1021 err
= bpf_map_update_elem(fd
, &i
, &sfd
[i
], BPF_EXIST
);
1023 printf("Failed sockmap update new slot BPF_EXIST\n");
1027 /* Delete the elems without programs */
1028 for (i
= 2; i
< 6; i
++) {
1029 err
= bpf_map_delete_elem(fd
, &i
);
1031 printf("Failed delete sockmap %i '%i:%i'\n",
1036 /* Test having multiple maps open and set with programs on same fds */
1037 err
= bpf_prog_attach(parse_prog
, fd
,
1038 BPF_SK_SKB_STREAM_PARSER
, 0);
1040 printf("Failed fd bpf parse prog attach\n");
1043 err
= bpf_prog_attach(verdict_prog
, fd
,
1044 BPF_SK_SKB_STREAM_VERDICT
, 0);
1046 printf("Failed fd bpf verdict prog attach\n");
1050 for (i
= 4; i
< 6; i
++) {
1051 err
= bpf_map_update_elem(fd
, &i
, &sfd
[i
], BPF_ANY
);
1053 printf("Failed allowed duplicate programs in update ANY sockmap %i '%i:%i'\n",
1057 err
= bpf_map_update_elem(fd
, &i
, &sfd
[i
], BPF_NOEXIST
);
1059 printf("Failed allowed duplicate program in update NOEXIST sockmap %i '%i:%i'\n",
1063 err
= bpf_map_update_elem(fd
, &i
, &sfd
[i
], BPF_EXIST
);
1065 printf("Failed allowed duplicate program in update EXIST sockmap %i '%i:%i'\n",
1071 /* Test tasks number of forked operations */
1072 for (i
= 0; i
< tasks
; i
++) {
1075 for (i
= 0; i
< 6; i
++) {
1076 bpf_map_delete_elem(map_fd_tx
, &i
);
1077 bpf_map_delete_elem(map_fd_rx
, &i
);
1078 bpf_map_update_elem(map_fd_tx
, &i
,
1080 bpf_map_update_elem(map_fd_rx
, &i
,
1084 } else if (pid
[i
] == -1) {
1085 printf("Couldn't spawn #%d process!\n", i
);
1090 for (i
= 0; i
< tasks
; i
++) {
1093 assert(waitpid(pid
[i
], &status
, 0) == pid
[i
]);
1094 assert(status
== 0);
1097 err
= bpf_prog_detach(map_fd_rx
, __MAX_BPF_ATTACH_TYPE
);
1099 printf("Detached an invalid prog type.\n");
1103 err
= bpf_prog_detach(map_fd_rx
, BPF_SK_SKB_STREAM_PARSER
);
1105 printf("Failed parser prog detach\n");
1109 err
= bpf_prog_detach(map_fd_rx
, BPF_SK_SKB_STREAM_VERDICT
);
1111 printf("Failed parser prog detach\n");
1115 /* Test map close sockets and empty maps */
1116 for (i
= 0; i
< 6; i
++) {
1117 bpf_map_delete_elem(map_fd_tx
, &i
);
1118 bpf_map_delete_elem(map_fd_rx
, &i
);
1123 bpf_object__close(obj
);
1126 for (i
= 0; i
< 6; i
++)
1128 printf("Failed to create sockmap '%i:%s'!\n", i
, strerror(errno
));
1131 for (i
= 0; i
< 6; i
++) {
1133 bpf_map_delete_elem(map_fd_tx
, &i
);
1135 bpf_map_delete_elem(map_fd_rx
, &i
);
1142 #define MAPINMAP_PROG "./test_map_in_map.o"
1143 static void test_map_in_map(void)
1145 struct bpf_object
*obj
;
1146 struct bpf_map
*map
;
1147 int mim_fd
, fd
, err
;
1150 obj
= bpf_object__open(MAPINMAP_PROG
);
1152 fd
= bpf_create_map(BPF_MAP_TYPE_HASH
, sizeof(int), sizeof(int),
1155 printf("Failed to create hashmap '%s'!\n", strerror(errno
));
1159 map
= bpf_object__find_map_by_name(obj
, "mim_array");
1161 printf("Failed to load array of maps from test prog\n");
1162 goto out_map_in_map
;
1164 err
= bpf_map__set_inner_map_fd(map
, fd
);
1166 printf("Failed to set inner_map_fd for array of maps\n");
1167 goto out_map_in_map
;
1170 map
= bpf_object__find_map_by_name(obj
, "mim_hash");
1172 printf("Failed to load hash of maps from test prog\n");
1173 goto out_map_in_map
;
1175 err
= bpf_map__set_inner_map_fd(map
, fd
);
1177 printf("Failed to set inner_map_fd for hash of maps\n");
1178 goto out_map_in_map
;
1181 bpf_object__load(obj
);
1183 map
= bpf_object__find_map_by_name(obj
, "mim_array");
1185 printf("Failed to load array of maps from test prog\n");
1186 goto out_map_in_map
;
1188 mim_fd
= bpf_map__fd(map
);
1190 printf("Failed to get descriptor for array of maps\n");
1191 goto out_map_in_map
;
1194 err
= bpf_map_update_elem(mim_fd
, &pos
, &fd
, 0);
1196 printf("Failed to update array of maps\n");
1197 goto out_map_in_map
;
1200 map
= bpf_object__find_map_by_name(obj
, "mim_hash");
1202 printf("Failed to load hash of maps from test prog\n");
1203 goto out_map_in_map
;
1205 mim_fd
= bpf_map__fd(map
);
1207 printf("Failed to get descriptor for hash of maps\n");
1208 goto out_map_in_map
;
1211 err
= bpf_map_update_elem(mim_fd
, &pos
, &fd
, 0);
1213 printf("Failed to update hash of maps\n");
1214 goto out_map_in_map
;
1218 bpf_object__close(obj
);
1226 #define MAP_SIZE (32 * 1024)
1228 static void test_map_large(void)
1237 fd
= bpf_create_map(BPF_MAP_TYPE_HASH
, sizeof(key
), sizeof(value
),
1238 MAP_SIZE
, map_flags
);
1240 printf("Failed to create large map '%s'!\n", strerror(errno
));
1244 for (i
= 0; i
< MAP_SIZE
; i
++) {
1245 key
= (struct bigkey
) { .c
= i
};
1248 assert(bpf_map_update_elem(fd
, &key
, &value
, BPF_NOEXIST
) == 0);
1252 assert(bpf_map_update_elem(fd
, &key
, &value
, BPF_NOEXIST
) == -1 &&
1255 /* Iterate through all elements. */
1256 assert(bpf_map_get_next_key(fd
, NULL
, &key
) == 0);
1258 for (i
= 0; i
< MAP_SIZE
; i
++)
1259 assert(bpf_map_get_next_key(fd
, &key
, &key
) == 0);
1260 assert(bpf_map_get_next_key(fd
, &key
, &key
) == -1 && errno
== ENOENT
);
1263 assert(bpf_map_lookup_elem(fd
, &key
, &value
) == 0 && value
== 0);
1265 assert(bpf_map_lookup_elem(fd
, &key
, &value
) == -1 && errno
== ENOENT
);
1270 #define run_parallel(N, FN, DATA) \
1271 printf("Fork %u tasks to '" #FN "'\n", N); \
1272 __run_parallel(N, FN, DATA)
1274 static void __run_parallel(unsigned int tasks
,
1275 void (*fn
)(unsigned int task
, void *data
),
1281 for (i
= 0; i
< tasks
; i
++) {
1286 } else if (pid
[i
] == -1) {
1287 printf("Couldn't spawn #%d process!\n", i
);
1292 for (i
= 0; i
< tasks
; i
++) {
1295 assert(waitpid(pid
[i
], &status
, 0) == pid
[i
]);
1296 assert(status
== 0);
1300 static void test_map_stress(void)
1302 run_parallel(100, test_hashmap
, NULL
);
1303 run_parallel(100, test_hashmap_percpu
, NULL
);
1304 run_parallel(100, test_hashmap_sizes
, NULL
);
1305 run_parallel(100, test_hashmap_walk
, NULL
);
1307 run_parallel(100, test_arraymap
, NULL
);
1308 run_parallel(100, test_arraymap_percpu
, NULL
);
1316 static void test_update_delete(unsigned int fn
, void *data
)
1318 int do_update
= ((int *)data
)[1];
1319 int fd
= ((int *)data
)[0];
1322 for (i
= fn
; i
< MAP_SIZE
; i
+= TASKS
) {
1326 assert(bpf_map_update_elem(fd
, &key
, &value
,
1328 assert(bpf_map_update_elem(fd
, &key
, &value
,
1331 assert(bpf_map_delete_elem(fd
, &key
) == 0);
1336 static void test_map_parallel(void)
1338 int i
, fd
, key
= 0, value
= 0;
1341 fd
= bpf_create_map(BPF_MAP_TYPE_HASH
, sizeof(key
), sizeof(value
),
1342 MAP_SIZE
, map_flags
);
1344 printf("Failed to create map for parallel test '%s'!\n",
1349 /* Use the same fd in children to add elements to this map:
1350 * child_0 adds key=0, key=1024, key=2048, ...
1351 * child_1 adds key=1, key=1025, key=2049, ...
1352 * child_1023 adds key=1023, ...
1355 data
[1] = DO_UPDATE
;
1356 run_parallel(TASKS
, test_update_delete
, data
);
1358 /* Check that key=0 is already there. */
1359 assert(bpf_map_update_elem(fd
, &key
, &value
, BPF_NOEXIST
) == -1 &&
1362 /* Check that all elements were inserted. */
1363 assert(bpf_map_get_next_key(fd
, NULL
, &key
) == 0);
1365 for (i
= 0; i
< MAP_SIZE
; i
++)
1366 assert(bpf_map_get_next_key(fd
, &key
, &key
) == 0);
1367 assert(bpf_map_get_next_key(fd
, &key
, &key
) == -1 && errno
== ENOENT
);
1369 /* Another check for all elements */
1370 for (i
= 0; i
< MAP_SIZE
; i
++) {
1371 key
= MAP_SIZE
- i
- 1;
1373 assert(bpf_map_lookup_elem(fd
, &key
, &value
) == 0 &&
1377 /* Now let's delete all elemenets in parallel. */
1378 data
[1] = DO_DELETE
;
1379 run_parallel(TASKS
, test_update_delete
, data
);
1381 /* Nothing should be left. */
1383 assert(bpf_map_get_next_key(fd
, NULL
, &key
) == -1 && errno
== ENOENT
);
1384 assert(bpf_map_get_next_key(fd
, &key
, &key
) == -1 && errno
== ENOENT
);
1387 static void test_map_rdonly(void)
1389 int fd
, key
= 0, value
= 0;
1391 fd
= bpf_create_map(BPF_MAP_TYPE_HASH
, sizeof(key
), sizeof(value
),
1392 MAP_SIZE
, map_flags
| BPF_F_RDONLY
);
1394 printf("Failed to create map for read only test '%s'!\n",
1401 /* Insert key=1 element. */
1402 assert(bpf_map_update_elem(fd
, &key
, &value
, BPF_ANY
) == -1 &&
1405 /* Check that key=2 is not found. */
1406 assert(bpf_map_lookup_elem(fd
, &key
, &value
) == -1 && errno
== ENOENT
);
1407 assert(bpf_map_get_next_key(fd
, &key
, &value
) == -1 && errno
== ENOENT
);
1410 static void test_map_wronly(void)
1412 int fd
, key
= 0, value
= 0;
1414 fd
= bpf_create_map(BPF_MAP_TYPE_HASH
, sizeof(key
), sizeof(value
),
1415 MAP_SIZE
, map_flags
| BPF_F_WRONLY
);
1417 printf("Failed to create map for read only test '%s'!\n",
1424 /* Insert key=1 element. */
1425 assert(bpf_map_update_elem(fd
, &key
, &value
, BPF_ANY
) == 0);
1427 /* Check that key=2 is not found. */
1428 assert(bpf_map_lookup_elem(fd
, &key
, &value
) == -1 && errno
== EPERM
);
1429 assert(bpf_map_get_next_key(fd
, &key
, &value
) == -1 && errno
== EPERM
);
1432 static void prepare_reuseport_grp(int type
, int map_fd
, size_t map_elem_size
,
1433 __s64
*fds64
, __u64
*sk_cookies
,
1436 socklen_t optlen
, addrlen
;
1437 struct sockaddr_in6 s6
;
1438 const __u32 index0
= 0;
1439 const int optval
= 1;
1447 s6
.sin6_family
= AF_INET6
;
1448 s6
.sin6_addr
= in6addr_any
;
1450 addrlen
= sizeof(s6
);
1451 optlen
= sizeof(sk_cookie
);
1453 for (i
= 0; i
< n
; i
++) {
1454 fd64
= socket(AF_INET6
, type
, 0);
1455 CHECK(fd64
== -1, "socket()",
1456 "sock_type:%d fd64:%lld errno:%d\n",
1459 err
= setsockopt(fd64
, SOL_SOCKET
, SO_REUSEPORT
,
1460 &optval
, sizeof(optval
));
1461 CHECK(err
== -1, "setsockopt(SO_REUSEPORT)",
1462 "err:%d errno:%d\n", err
, errno
);
1464 /* reuseport_array does not allow unbound sk */
1465 if (map_elem_size
== sizeof(__u64
))
1468 assert(map_elem_size
== sizeof(__u32
));
1472 err
= bpf_map_update_elem(map_fd
, &index0
, value
, BPF_ANY
);
1473 CHECK(err
!= -1 || errno
!= EINVAL
,
1474 "reuseport array update unbound sk",
1475 "sock_type:%d err:%d errno:%d\n",
1478 err
= bind(fd64
, (struct sockaddr
*)&s6
, sizeof(s6
));
1479 CHECK(err
== -1, "bind()",
1480 "sock_type:%d err:%d errno:%d\n", type
, err
, errno
);
1483 err
= getsockname(fd64
, (struct sockaddr
*)&s6
,
1485 CHECK(err
== -1, "getsockname()",
1486 "sock_type:%d err:%d errno:%d\n",
1490 err
= getsockopt(fd64
, SOL_SOCKET
, SO_COOKIE
, &sk_cookie
,
1492 CHECK(err
== -1, "getsockopt(SO_COOKIE)",
1493 "sock_type:%d err:%d errno:%d\n", type
, err
, errno
);
1495 if (type
== SOCK_STREAM
) {
1497 * reuseport_array does not allow
1498 * non-listening tcp sk.
1500 err
= bpf_map_update_elem(map_fd
, &index0
, value
,
1502 CHECK(err
!= -1 || errno
!= EINVAL
,
1503 "reuseport array update non-listening sk",
1504 "sock_type:%d err:%d errno:%d\n",
1506 err
= listen(fd64
, 0);
1507 CHECK(err
== -1, "listen()",
1508 "sock_type:%d, err:%d errno:%d\n",
1513 sk_cookies
[i
] = sk_cookie
;
1517 static void test_reuseport_array(void)
1519 #define REUSEPORT_FD_IDX(err, last) ({ (err) ? last : !last; })
1521 const __u32 array_size
= 4, index0
= 0, index3
= 3;
1522 int types
[2] = { SOCK_STREAM
, SOCK_DGRAM
}, type
;
1523 __u64 grpa_cookies
[2], sk_cookie
, map_cookie
;
1524 __s64 grpa_fds64
[2] = { -1, -1 }, fd64
= -1;
1525 const __u32 bad_index
= array_size
;
1526 int map_fd
, err
, t
, f
;
1530 map_fd
= bpf_create_map(BPF_MAP_TYPE_REUSEPORT_SOCKARRAY
,
1531 sizeof(__u32
), sizeof(__u64
), array_size
, 0);
1532 CHECK(map_fd
== -1, "reuseport array create",
1533 "map_fd:%d, errno:%d\n", map_fd
, errno
);
1535 /* Test lookup/update/delete with invalid index */
1536 err
= bpf_map_delete_elem(map_fd
, &bad_index
);
1537 CHECK(err
!= -1 || errno
!= E2BIG
, "reuseport array del >=max_entries",
1538 "err:%d errno:%d\n", err
, errno
);
1540 err
= bpf_map_update_elem(map_fd
, &bad_index
, &fd64
, BPF_ANY
);
1541 CHECK(err
!= -1 || errno
!= E2BIG
,
1542 "reuseport array update >=max_entries",
1543 "err:%d errno:%d\n", err
, errno
);
1545 err
= bpf_map_lookup_elem(map_fd
, &bad_index
, &map_cookie
);
1546 CHECK(err
!= -1 || errno
!= ENOENT
,
1547 "reuseport array update >=max_entries",
1548 "err:%d errno:%d\n", err
, errno
);
1550 /* Test lookup/delete non existence elem */
1551 err
= bpf_map_lookup_elem(map_fd
, &index3
, &map_cookie
);
1552 CHECK(err
!= -1 || errno
!= ENOENT
,
1553 "reuseport array lookup not-exist elem",
1554 "err:%d errno:%d\n", err
, errno
);
1555 err
= bpf_map_delete_elem(map_fd
, &index3
);
1556 CHECK(err
!= -1 || errno
!= ENOENT
,
1557 "reuseport array del not-exist elem",
1558 "err:%d errno:%d\n", err
, errno
);
1560 for (t
= 0; t
< ARRAY_SIZE(types
); t
++) {
1563 prepare_reuseport_grp(type
, map_fd
, sizeof(__u64
), grpa_fds64
,
1564 grpa_cookies
, ARRAY_SIZE(grpa_fds64
));
1566 /* Test BPF_* update flags */
1567 /* BPF_EXIST failure case */
1568 err
= bpf_map_update_elem(map_fd
, &index3
, &grpa_fds64
[fds_idx
],
1570 CHECK(err
!= -1 || errno
!= ENOENT
,
1571 "reuseport array update empty elem BPF_EXIST",
1572 "sock_type:%d err:%d errno:%d\n",
1574 fds_idx
= REUSEPORT_FD_IDX(err
, fds_idx
);
1576 /* BPF_NOEXIST success case */
1577 err
= bpf_map_update_elem(map_fd
, &index3
, &grpa_fds64
[fds_idx
],
1580 "reuseport array update empty elem BPF_NOEXIST",
1581 "sock_type:%d err:%d errno:%d\n",
1583 fds_idx
= REUSEPORT_FD_IDX(err
, fds_idx
);
1585 /* BPF_EXIST success case. */
1586 err
= bpf_map_update_elem(map_fd
, &index3
, &grpa_fds64
[fds_idx
],
1589 "reuseport array update same elem BPF_EXIST",
1590 "sock_type:%d err:%d errno:%d\n", type
, err
, errno
);
1591 fds_idx
= REUSEPORT_FD_IDX(err
, fds_idx
);
1593 /* BPF_NOEXIST failure case */
1594 err
= bpf_map_update_elem(map_fd
, &index3
, &grpa_fds64
[fds_idx
],
1596 CHECK(err
!= -1 || errno
!= EEXIST
,
1597 "reuseport array update non-empty elem BPF_NOEXIST",
1598 "sock_type:%d err:%d errno:%d\n",
1600 fds_idx
= REUSEPORT_FD_IDX(err
, fds_idx
);
1602 /* BPF_ANY case (always succeed) */
1603 err
= bpf_map_update_elem(map_fd
, &index3
, &grpa_fds64
[fds_idx
],
1606 "reuseport array update same sk with BPF_ANY",
1607 "sock_type:%d err:%d errno:%d\n", type
, err
, errno
);
1609 fd64
= grpa_fds64
[fds_idx
];
1610 sk_cookie
= grpa_cookies
[fds_idx
];
1612 /* The same sk cannot be added to reuseport_array twice */
1613 err
= bpf_map_update_elem(map_fd
, &index3
, &fd64
, BPF_ANY
);
1614 CHECK(err
!= -1 || errno
!= EBUSY
,
1615 "reuseport array update same sk with same index",
1616 "sock_type:%d err:%d errno:%d\n",
1619 err
= bpf_map_update_elem(map_fd
, &index0
, &fd64
, BPF_ANY
);
1620 CHECK(err
!= -1 || errno
!= EBUSY
,
1621 "reuseport array update same sk with different index",
1622 "sock_type:%d err:%d errno:%d\n",
1625 /* Test delete elem */
1626 err
= bpf_map_delete_elem(map_fd
, &index3
);
1627 CHECK(err
== -1, "reuseport array delete sk",
1628 "sock_type:%d err:%d errno:%d\n",
1631 /* Add it back with BPF_NOEXIST */
1632 err
= bpf_map_update_elem(map_fd
, &index3
, &fd64
, BPF_NOEXIST
);
1634 "reuseport array re-add with BPF_NOEXIST after del",
1635 "sock_type:%d err:%d errno:%d\n", type
, err
, errno
);
1638 err
= bpf_map_lookup_elem(map_fd
, &index3
, &map_cookie
);
1639 CHECK(err
== -1 || sk_cookie
!= map_cookie
,
1640 "reuseport array lookup re-added sk",
1641 "sock_type:%d err:%d errno:%d sk_cookie:0x%llx map_cookie:0x%llxn",
1642 type
, err
, errno
, sk_cookie
, map_cookie
);
1644 /* Test elem removed by close() */
1645 for (f
= 0; f
< ARRAY_SIZE(grpa_fds64
); f
++)
1646 close(grpa_fds64
[f
]);
1647 err
= bpf_map_lookup_elem(map_fd
, &index3
, &map_cookie
);
1648 CHECK(err
!= -1 || errno
!= ENOENT
,
1649 "reuseport array lookup after close()",
1650 "sock_type:%d err:%d errno:%d\n",
1655 fd64
= socket(AF_INET6
, SOCK_RAW
, IPPROTO_UDP
);
1656 CHECK(fd64
== -1, "socket(SOCK_RAW)", "err:%d errno:%d\n",
1658 err
= bpf_map_update_elem(map_fd
, &index3
, &fd64
, BPF_NOEXIST
);
1659 CHECK(err
!= -1 || errno
!= ENOTSUPP
, "reuseport array update SOCK_RAW",
1660 "err:%d errno:%d\n", err
, errno
);
1663 /* Close the 64 bit value map */
1666 /* Test 32 bit fd */
1667 map_fd
= bpf_create_map(BPF_MAP_TYPE_REUSEPORT_SOCKARRAY
,
1668 sizeof(__u32
), sizeof(__u32
), array_size
, 0);
1669 CHECK(map_fd
== -1, "reuseport array create",
1670 "map_fd:%d, errno:%d\n", map_fd
, errno
);
1671 prepare_reuseport_grp(SOCK_STREAM
, map_fd
, sizeof(__u32
), &fd64
,
1674 err
= bpf_map_update_elem(map_fd
, &index3
, &fd
, BPF_NOEXIST
);
1675 CHECK(err
== -1, "reuseport array update 32 bit fd",
1676 "err:%d errno:%d\n", err
, errno
);
1677 err
= bpf_map_lookup_elem(map_fd
, &index3
, &map_cookie
);
1678 CHECK(err
!= -1 || errno
!= ENOSPC
,
1679 "reuseport array lookup 32 bit fd",
1680 "err:%d errno:%d\n", err
, errno
);
1685 static void run_all_tests(void)
1687 test_hashmap(0, NULL
);
1688 test_hashmap_percpu(0, NULL
);
1689 test_hashmap_walk(0, NULL
);
1690 test_hashmap_zero_seed();
1692 test_arraymap(0, NULL
);
1693 test_arraymap_percpu(0, NULL
);
1695 test_arraymap_percpu_many_keys();
1697 test_devmap(0, NULL
);
1698 test_devmap_hash(0, NULL
);
1699 test_sockmap(0, NULL
);
1702 test_map_parallel();
1708 test_reuseport_array();
1710 test_queuemap(0, NULL
);
1711 test_stackmap(0, NULL
);
1716 #define DEFINE_TEST(name) extern void test_##name(void);
1717 #include <map_tests/tests.h>
1727 map_flags
= BPF_F_NO_PREALLOC
;
1730 #define DEFINE_TEST(name) test_##name();
1731 #include <map_tests/tests.h>
1734 printf("test_maps: OK, %d SKIPPED\n", skips
);