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) < 0 &&
57 /* key=1 already exists. */
60 /* -1 is an invalid flag. */
61 assert(bpf_map_update_elem(fd, &key, &value, -1) < 0 &&
64 /* Check that key=1 can be found. */
65 assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && value == 1234);
69 /* Insert key=2 element. */
70 assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == 0);
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);
75 /* Check that key=2 is not found. */
76 assert(bpf_map_lookup_elem(fd, &key, &value) < 0 && errno == ENOENT);
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. */
83 /* Insert key=2 element. */
84 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == 0);
86 /* key=1 and key=2 were inserted, check that key=0 cannot be
87 * inserted due to max_entries limit.
90 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) < 0 &&
93 /* Update existing element, though the map is full. */
95 assert(bpf_map_update_elem(fd, &key, &value, BPF_EXIST) == 0);
97 assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == 0);
99 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) < 0 &&
102 /* Check that key = 0 doesn't exist. */
104 assert(bpf_map_delete_elem(fd, &key) < 0 && errno == ENOENT);
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 &&
117 /* Delete both elements. */
119 assert(bpf_map_delete_elem(fd, &key) == 0);
121 assert(bpf_map_delete_elem(fd, &key) == 0);
122 assert(bpf_map_delete_elem(fd, &key) < 0 && errno == ENOENT);
125 /* Check that map is empty. */
126 assert(bpf_map_get_next_key(fd, NULL, &next_key) < 0 &&
128 assert(bpf_map_get_next_key(fd, &key, &next_key) < 0 &&
134 static void test_hashmap_sizes(unsigned int task, void *data)
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,
145 printf("Failed to create hashmap key=%d value=%d '%s'\n",
146 i, j, strerror(errno));
150 usleep(10); /* give kernel time to destroy */
154 static void test_hashmap_percpu(unsigned int task, void *data)
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;
162 fd = bpf_create_map(BPF_MAP_TYPE_PERCPU_HASH, sizeof(key),
163 sizeof(bpf_percpu(value, 0)), 2, map_flags);
165 printf("Failed to create hashmap '%s'!\n", strerror(errno));
169 for (i = 0; i < nr_cpus; i++)
170 bpf_percpu(value, i) = i + 100;
173 /* Insert key=1 element. */
174 assert(!(expected_key_mask & key));
175 assert(bpf_map_update_elem(fd, &key, value, BPF_ANY) == 0);
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);
181 for (i = 0; i < nr_cpus; i++)
182 bpf_percpu(value,i) = i + 100;
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;
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. */
193 /* -1 is an invalid flag. */
194 assert(bpf_map_update_elem(fd, &key, value, -1) < 0 &&
197 /* Check that key=1 can be found. Value could be 0 if the lookup
198 * was run from a different CPU.
200 bpf_percpu(value, 0) = 1;
201 assert(bpf_map_lookup_elem(fd, &key, value) == 0 &&
202 bpf_percpu(value, 0) == 100);
205 /* Check that key=2 is not found. */
206 assert(bpf_map_lookup_elem(fd, &key, value) < 0 && errno == ENOENT);
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. */
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;
218 /* key=1 and key=2 were inserted, check that key=0 cannot be
219 * inserted due to max_entries limit.
222 assert(bpf_map_update_elem(fd, &key, value, BPF_NOEXIST) < 0 &&
225 /* Check that key = 0 doesn't exist. */
226 assert(bpf_map_delete_elem(fd, &key) < 0 && errno == ENOENT);
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)) {
233 assert(next_key == first_key);
236 assert((expected_key_mask & next_key) == next_key);
237 expected_key_mask &= ~next_key;
239 assert(bpf_map_lookup_elem(fd, &next_key, value) == 0);
241 for (i = 0; i < nr_cpus; i++)
242 assert(bpf_percpu(value, i) == i + 100);
246 assert(errno == ENOENT);
248 /* Update with BPF_EXIST. */
250 assert(bpf_map_update_elem(fd, &key, value, BPF_EXIST) == 0);
252 /* Delete both elements. */
254 assert(bpf_map_delete_elem(fd, &key) == 0);
256 assert(bpf_map_delete_elem(fd, &key) == 0);
257 assert(bpf_map_delete_elem(fd, &key) < 0 && errno == ENOENT);
260 /* Check that map is empty. */
261 assert(bpf_map_get_next_key(fd, NULL, &next_key) < 0 &&
263 assert(bpf_map_get_next_key(fd, &key, &next_key) < 0 &&
269 static int helper_fill_hashmap(int max_entries)
272 long long key, value;
274 fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value),
275 max_entries, map_flags);
277 "failed to create hashmap",
278 "err: %s, flags: 0x%x\n", strerror(errno), map_flags);
280 for (i = 0; i < max_entries; i++) {
281 key = i; value = key;
282 ret = bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST);
284 "can't update hashmap",
285 "err: %s\n", strerror(ret));
291 static void test_hashmap_walk(unsigned int task, void *data)
293 int fd, i, max_entries = 1000;
294 long long key, value, next_key;
295 bool next_key_valid = true;
297 fd = helper_fill_hashmap(max_entries);
299 for (i = 0; bpf_map_get_next_key(fd, !i ? NULL : &key,
300 &next_key) == 0; i++) {
302 assert(bpf_map_lookup_elem(fd, &key, &value) == 0);
305 assert(i == max_entries);
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);
312 assert(bpf_map_update_elem(fd, &key, &value, BPF_EXIST) == 0);
316 assert(i == max_entries);
318 for (i = 0; bpf_map_get_next_key(fd, !i ? NULL : &key,
319 &next_key) == 0; i++) {
321 assert(bpf_map_lookup_elem(fd, &key, &value) == 0);
322 assert(value - 1 == key);
325 assert(i == max_entries);
329 static void test_hashmap_zero_seed(void)
331 int i, first, second, old_flags;
332 long long key, next_first, next_second;
334 old_flags = map_flags;
335 map_flags |= BPF_F_ZERO_SEED;
337 first = helper_fill_hashmap(3);
338 second = helper_fill_hashmap(3);
341 void *key_ptr = !i ? NULL : &key;
343 if (bpf_map_get_next_key(first, key_ptr, &next_first) != 0)
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,
351 "i: %d first: %lld second: %lld\n", i,
352 next_first, next_second);
357 map_flags = old_flags;
362 static void test_arraymap(unsigned int task, void *data)
364 int key, next_key, fd;
367 fd = bpf_create_map(BPF_MAP_TYPE_ARRAY, sizeof(key), sizeof(value),
370 printf("Failed to create arraymap '%s'!\n", strerror(errno));
376 /* Insert key=1 element. */
377 assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == 0);
380 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) < 0 &&
383 /* Check that key=1 can be found. */
384 assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && value == 1234);
387 /* Check that key=0 is also found and zero initialized. */
388 assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && value == 0);
390 /* key=0 and key=1 were inserted, check that key=2 cannot be inserted
391 * due to max_entries limit.
394 assert(bpf_map_update_elem(fd, &key, &value, BPF_EXIST) < 0 &&
397 /* Check that key = 2 doesn't exist. */
398 assert(bpf_map_lookup_elem(fd, &key, &value) < 0 && errno == ENOENT);
400 /* Iterate over two elements. */
401 assert(bpf_map_get_next_key(fd, NULL, &next_key) == 0 &&
403 assert(bpf_map_get_next_key(fd, &key, &next_key) == 0 &&
405 assert(bpf_map_get_next_key(fd, &next_key, &next_key) == 0 &&
407 assert(bpf_map_get_next_key(fd, &next_key, &next_key) < 0 &&
410 /* Delete shouldn't succeed. */
412 assert(bpf_map_delete_elem(fd, &key) < 0 && errno == EINVAL);
417 static void test_arraymap_percpu(unsigned int task, void *data)
419 unsigned int nr_cpus = bpf_num_possible_cpus();
420 BPF_DECLARE_PERCPU(long, values);
421 int key, next_key, fd, i;
423 fd = bpf_create_map(BPF_MAP_TYPE_PERCPU_ARRAY, sizeof(key),
424 sizeof(bpf_percpu(values, 0)), 2, 0);
426 printf("Failed to create arraymap '%s'!\n", strerror(errno));
430 for (i = 0; i < nr_cpus; i++)
431 bpf_percpu(values, i) = i + 100;
434 /* Insert key=1 element. */
435 assert(bpf_map_update_elem(fd, &key, values, BPF_ANY) == 0);
437 bpf_percpu(values, 0) = 0;
438 assert(bpf_map_update_elem(fd, &key, values, BPF_NOEXIST) < 0 &&
441 /* Check that key=1 can be found. */
442 assert(bpf_map_lookup_elem(fd, &key, values) == 0 &&
443 bpf_percpu(values, 0) == 100);
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);
451 /* Check that key=2 cannot be inserted due to max_entries limit. */
453 assert(bpf_map_update_elem(fd, &key, values, BPF_EXIST) < 0 &&
456 /* Check that key = 2 doesn't exist. */
457 assert(bpf_map_lookup_elem(fd, &key, values) < 0 && errno == ENOENT);
459 /* Iterate over two elements. */
460 assert(bpf_map_get_next_key(fd, NULL, &next_key) == 0 &&
462 assert(bpf_map_get_next_key(fd, &key, &next_key) == 0 &&
464 assert(bpf_map_get_next_key(fd, &next_key, &next_key) == 0 &&
466 assert(bpf_map_get_next_key(fd, &next_key, &next_key) < 0 &&
469 /* Delete shouldn't succeed. */
471 assert(bpf_map_delete_elem(fd, &key) < 0 && errno == EINVAL);
476 static void test_arraymap_percpu_many_keys(void)
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
483 unsigned int nr_keys = 2000;
486 fd = bpf_create_map(BPF_MAP_TYPE_PERCPU_ARRAY, sizeof(key),
487 sizeof(bpf_percpu(values, 0)), nr_keys, 0);
489 printf("Failed to create per-cpu arraymap '%s'!\n",
494 for (i = 0; i < nr_cpus; i++)
495 bpf_percpu(values, i) = i + 10;
497 for (key = 0; key < nr_keys; key++)
498 assert(bpf_map_update_elem(fd, &key, values, BPF_ANY) == 0);
500 for (key = 0; key < nr_keys; key++) {
501 for (i = 0; i < nr_cpus; i++)
502 bpf_percpu(values, i) = 0;
504 assert(bpf_map_lookup_elem(fd, &key, values) == 0);
506 for (i = 0; i < nr_cpus; i++)
507 assert(bpf_percpu(values, i) == i + 10);
513 static void test_devmap(unsigned int task, void *data)
518 fd = bpf_create_map(BPF_MAP_TYPE_DEVMAP, sizeof(key), sizeof(value),
521 printf("Failed to create devmap '%s'!\n", strerror(errno));
528 static void test_devmap_hash(unsigned int task, void *data)
533 fd = bpf_create_map(BPF_MAP_TYPE_DEVMAP_HASH, sizeof(key), sizeof(value),
536 printf("Failed to create devmap_hash '%s'!\n", strerror(errno));
543 static void test_queuemap(unsigned int task, void *data)
545 const int MAP_SIZE = 32;
546 __u32 vals[MAP_SIZE + MAP_SIZE/2], val;
549 /* Fill test values to be used */
550 for (i = 0; i < MAP_SIZE + MAP_SIZE/2; i++)
553 /* Invalid key size */
554 fd = bpf_create_map(BPF_MAP_TYPE_QUEUE, 4, sizeof(val), MAP_SIZE,
556 assert(fd < 0 && errno == EINVAL);
558 fd = bpf_create_map(BPF_MAP_TYPE_QUEUE, 0, sizeof(val), MAP_SIZE,
560 /* Queue map does not support BPF_F_NO_PREALLOC */
561 if (map_flags & BPF_F_NO_PREALLOC) {
562 assert(fd < 0 && errno == EINVAL);
566 printf("Failed to create queuemap '%s'!\n", strerror(errno));
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);
574 /* Check that element cannot be pushed due to max_entries limit */
575 assert(bpf_map_update_elem(fd, NULL, &val, 0) < 0 &&
579 assert(bpf_map_lookup_elem(fd, NULL, &val) == 0 && val == vals[0]);
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);
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 &&
590 /* Check that there are not elements left */
591 assert(bpf_map_lookup_and_delete_elem(fd, NULL, &val) < 0 &&
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);
601 static void test_stackmap(unsigned int task, void *data)
603 const int MAP_SIZE = 32;
604 __u32 vals[MAP_SIZE + MAP_SIZE/2], val;
607 /* Fill test values to be used */
608 for (i = 0; i < MAP_SIZE + MAP_SIZE/2; i++)
611 /* Invalid key size */
612 fd = bpf_create_map(BPF_MAP_TYPE_STACK, 4, sizeof(val), MAP_SIZE,
614 assert(fd < 0 && errno == EINVAL);
616 fd = bpf_create_map(BPF_MAP_TYPE_STACK, 0, sizeof(val), MAP_SIZE,
618 /* Stack map does not support BPF_F_NO_PREALLOC */
619 if (map_flags & BPF_F_NO_PREALLOC) {
620 assert(fd < 0 && errno == EINVAL);
624 printf("Failed to create stackmap '%s'!\n", strerror(errno));
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);
632 /* Check that element cannot be pushed due to max_entries limit */
633 assert(bpf_map_update_elem(fd, NULL, &val, 0) < 0 &&
637 assert(bpf_map_lookup_elem(fd, NULL, &val) == 0 && val == vals[i - 1]);
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);
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 &&
648 /* Check that there are not elements left */
649 assert(bpf_map_lookup_and_delete_elem(fd, NULL, &val) < 0 &&
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);
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"
666 static void test_sockmap(unsigned int tasks, void *data)
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;
682 /* Create some sockets to use with sockmap */
683 for (i = 0; i < 2; i++) {
684 sfd[i] = socket(AF_INET, SOCK_STREAM, 0);
687 err = setsockopt(sfd[i], SOL_SOCKET, SO_REUSEADDR,
688 (char *)&one, sizeof(one));
690 printf("failed to setsockopt\n");
693 err = ioctl(sfd[i], FIONBIO, (char *)&one);
695 printf("failed to ioctl\n");
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));
704 printf("failed to bind: err %i: %i:%i\n",
708 err = listen(sfd[i], 32);
710 printf("failed to listen\n");
715 for (i = 2; i < 4; i++) {
716 sfd[i] = socket(AF_INET, SOCK_STREAM, 0);
719 err = setsockopt(sfd[i], SOL_SOCKET, SO_REUSEADDR,
720 (char *)&one, sizeof(one));
722 printf("set sock opt\n");
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));
731 printf("failed to connect\n");
737 for (i = 4; i < 6; i++) {
738 sfd[i] = accept(sfd[i - 4], NULL, NULL);
740 printf("accept failed\n");
745 /* Test sockmap with connected sockets */
746 fd = bpf_create_map(BPF_MAP_TYPE_SOCKMAP,
747 sizeof(key), sizeof(value),
750 if (!bpf_probe_map_type(BPF_MAP_TYPE_SOCKMAP, 0)) {
751 printf("%s SKIP (unsupported map type BPF_MAP_TYPE_SOCKMAP)\n",
754 for (i = 0; i < 6; i++)
759 printf("Failed to create sockmap %i\n", fd);
763 /* Test update with unsupported UDP socket */
764 udp = socket(AF_INET, SOCK_DGRAM, 0);
766 err = bpf_map_update_elem(fd, &i, &udp, BPF_ANY);
768 printf("Failed socket update SOCK_DGRAM '%i:%i'\n",
773 /* Test update without programs */
774 for (i = 0; i < 6; i++) {
775 err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_ANY);
777 printf("Failed noprog update sockmap '%i:%i'\n",
783 /* Test attaching/detaching bad fds */
784 err = bpf_prog_attach(-1, fd, BPF_SK_SKB_STREAM_PARSER, 0);
786 printf("Failed invalid parser prog attach\n");
790 err = bpf_prog_attach(-1, fd, BPF_SK_SKB_STREAM_VERDICT, 0);
792 printf("Failed invalid verdict prog attach\n");
796 err = bpf_prog_attach(-1, fd, BPF_SK_MSG_VERDICT, 0);
798 printf("Failed invalid msg verdict prog attach\n");
802 err = bpf_prog_attach(-1, fd, __MAX_BPF_ATTACH_TYPE, 0);
804 printf("Failed unknown prog attach\n");
808 err = bpf_prog_detach(fd, BPF_SK_SKB_STREAM_PARSER);
810 printf("Failed empty parser prog detach\n");
814 err = bpf_prog_detach(fd, BPF_SK_SKB_STREAM_VERDICT);
816 printf("Failed empty verdict prog detach\n");
820 err = bpf_prog_detach(fd, BPF_SK_MSG_VERDICT);
822 printf("Failed empty msg verdict prog detach\n");
826 err = bpf_prog_detach(fd, __MAX_BPF_ATTACH_TYPE);
828 printf("Detach invalid prog successful\n");
832 /* Load SK_SKB program and Attach */
833 err = bpf_prog_load(SOCKMAP_PARSE_PROG,
834 BPF_PROG_TYPE_SK_SKB, &obj, &parse_prog);
836 printf("Failed to load SK_SKB parse prog\n");
840 err = bpf_prog_load(SOCKMAP_TCP_MSG_PROG,
841 BPF_PROG_TYPE_SK_MSG, &obj, &msg_prog);
843 printf("Failed to load SK_SKB msg prog\n");
847 err = bpf_prog_load(SOCKMAP_VERDICT_PROG,
848 BPF_PROG_TYPE_SK_SKB, &obj, &verdict_prog);
850 printf("Failed to load SK_SKB verdict prog\n");
854 bpf_map_rx = bpf_object__find_map_by_name(obj, "sock_map_rx");
856 printf("Failed to load map rx from verdict prog\n");
860 map_fd_rx = bpf_map__fd(bpf_map_rx);
862 printf("Failed to get map rx fd\n");
866 bpf_map_tx = bpf_object__find_map_by_name(obj, "sock_map_tx");
868 printf("Failed to load map tx from verdict prog\n");
872 map_fd_tx = bpf_map__fd(bpf_map_tx);
874 printf("Failed to get map tx fd\n");
878 bpf_map_msg = bpf_object__find_map_by_name(obj, "sock_map_msg");
880 printf("Failed to load map msg from msg_verdict prog\n");
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");
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");
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");
902 err = bpf_prog_attach(parse_prog, map_fd_break,
903 BPF_SK_SKB_STREAM_PARSER, 0);
905 printf("Allowed attaching SK_SKB program to invalid map\n");
909 err = bpf_prog_attach(parse_prog, map_fd_rx,
910 BPF_SK_SKB_STREAM_PARSER, 0);
912 printf("Failed stream parser bpf prog attach\n");
916 err = bpf_prog_attach(verdict_prog, map_fd_rx,
917 BPF_SK_SKB_STREAM_VERDICT, 0);
919 printf("Failed stream verdict bpf prog attach\n");
923 err = bpf_prog_attach(msg_prog, map_fd_msg, BPF_SK_MSG_VERDICT, 0);
925 printf("Failed msg verdict bpf prog attach\n");
929 err = bpf_prog_attach(verdict_prog, map_fd_rx,
930 __MAX_BPF_ATTACH_TYPE, 0);
932 printf("Attached unknown bpf prog\n");
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);
940 printf("Failed map_fd_rx update sockmap %i '%i:%i'\n",
944 err = bpf_map_update_elem(map_fd_tx, &i, &sfd[i], BPF_ANY);
946 printf("Failed map_fd_tx update sockmap %i '%i:%i'\n",
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);
956 printf("Failed delete sockmap rx %i '%i:%i'\n",
960 err = bpf_map_delete_elem(map_fd_tx, &i);
962 printf("Failed delete sockmap tx %i '%i:%i'\n",
968 /* Put sfd[2] (sending fd below) into msg map to test sendmsg bpf */
970 err = bpf_map_update_elem(map_fd_msg, &i, &sfd[2], BPF_ANY);
972 printf("Failed map_fd_msg update sockmap %i\n", err);
976 /* Test map send/recv */
977 for (i = 0; i < 2; i++) {
980 sc = send(sfd[2], buf, 20, 0);
982 printf("Failed sockmap send\n");
990 s = select(sfd[3] + 1, &w, NULL, NULL, &to);
992 perror("Failed sockmap select()");
995 printf("Failed sockmap unexpected timeout\n");
999 if (!FD_ISSET(sfd[3], &w)) {
1000 printf("Failed sockmap select/recv\n");
1004 rc = recv(sfd[3], buf, sizeof(buf), 0);
1006 printf("Failed sockmap recv\n");
1011 /* Negative null entry lookup from datapath should be dropped */
1014 sc = send(sfd[2], buf, 20, 0);
1016 printf("Failed sockmap send\n");
1020 /* Push fd into same slot */
1022 err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_NOEXIST);
1024 printf("Failed allowed sockmap dup slot BPF_NOEXIST\n");
1028 err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_ANY);
1030 printf("Failed sockmap update new slot BPF_ANY\n");
1034 err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_EXIST);
1036 printf("Failed sockmap update new slot BPF_EXIST\n");
1040 /* Delete the elems without programs */
1041 for (i = 2; i < 6; i++) {
1042 err = bpf_map_delete_elem(fd, &i);
1044 printf("Failed delete sockmap %i '%i:%i'\n",
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);
1053 printf("Failed fd bpf parse prog attach\n");
1056 err = bpf_prog_attach(verdict_prog, fd,
1057 BPF_SK_SKB_STREAM_VERDICT, 0);
1059 printf("Failed fd bpf verdict prog attach\n");
1063 for (i = 4; i < 6; i++) {
1064 err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_ANY);
1066 printf("Failed allowed duplicate programs in update ANY sockmap %i '%i:%i'\n",
1070 err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_NOEXIST);
1072 printf("Failed allowed duplicate program in update NOEXIST sockmap %i '%i:%i'\n",
1076 err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_EXIST);
1078 printf("Failed allowed duplicate program in update EXIST sockmap %i '%i:%i'\n",
1084 /* Test tasks number of forked operations */
1085 for (i = 0; i < tasks; i++) {
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,
1093 bpf_map_update_elem(map_fd_rx, &i,
1097 } else if (pid[i] == -1) {
1098 printf("Couldn't spawn #%d process!\n", i);
1103 for (i = 0; i < tasks; i++) {
1106 assert(waitpid(pid[i], &status, 0) == pid[i]);
1107 assert(status == 0);
1110 err = bpf_prog_detach2(parse_prog, map_fd_rx, __MAX_BPF_ATTACH_TYPE);
1112 printf("Detached an invalid prog type.\n");
1116 err = bpf_prog_detach2(parse_prog, map_fd_rx, BPF_SK_SKB_STREAM_PARSER);
1118 printf("Failed parser prog detach\n");
1122 err = bpf_prog_detach2(verdict_prog, map_fd_rx, BPF_SK_SKB_STREAM_VERDICT);
1124 printf("Failed parser prog detach\n");
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);
1136 bpf_object__close(obj);
1139 for (i = 0; i < 6; i++)
1141 printf("Failed to create sockmap '%i:%s'!\n", i, strerror(errno));
1144 for (i = 0; i < 6; i++) {
1146 bpf_map_delete_elem(map_fd_tx, &i);
1148 bpf_map_delete_elem(map_fd_rx, &i);
1155 #define MAPINMAP_PROG "./test_map_in_map.o"
1156 #define MAPINMAP_INVALID_PROG "./test_map_in_map_invalid.o"
1157 static void test_map_in_map(void)
1159 struct bpf_object *obj;
1160 struct bpf_map *map;
1161 int mim_fd, fd, err;
1163 struct bpf_map_info info = {};
1164 __u32 len = sizeof(info);
1166 libbpf_print_fn_t old_print_fn;
1168 obj = bpf_object__open(MAPINMAP_PROG);
1170 fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(int), sizeof(int),
1173 printf("Failed to create hashmap '%s'!\n", strerror(errno));
1177 map = bpf_object__find_map_by_name(obj, "mim_array");
1179 printf("Failed to load array of maps from test prog\n");
1180 goto out_map_in_map;
1182 err = bpf_map__set_inner_map_fd(map, fd);
1184 printf("Failed to set inner_map_fd for array of maps\n");
1185 goto out_map_in_map;
1188 map = bpf_object__find_map_by_name(obj, "mim_hash");
1190 printf("Failed to load hash of maps from test prog\n");
1191 goto out_map_in_map;
1193 err = bpf_map__set_inner_map_fd(map, fd);
1195 printf("Failed to set inner_map_fd for hash of maps\n");
1196 goto out_map_in_map;
1199 bpf_object__load(obj);
1201 map = bpf_object__find_map_by_name(obj, "mim_array");
1203 printf("Failed to load array of maps from test prog\n");
1204 goto out_map_in_map;
1206 mim_fd = bpf_map__fd(map);
1208 printf("Failed to get descriptor for array of maps\n");
1209 goto out_map_in_map;
1212 err = bpf_map_update_elem(mim_fd, &pos, &fd, 0);
1214 printf("Failed to update array of maps\n");
1215 goto out_map_in_map;
1218 map = bpf_object__find_map_by_name(obj, "mim_hash");
1220 printf("Failed to load hash of maps from test prog\n");
1221 goto out_map_in_map;
1223 mim_fd = bpf_map__fd(map);
1225 printf("Failed to get descriptor for hash of maps\n");
1226 goto out_map_in_map;
1229 err = bpf_map_update_elem(mim_fd, &pos, &fd, 0);
1231 printf("Failed to update hash of maps\n");
1232 goto out_map_in_map;
1237 bpf_object__close(obj);
1239 /* Test that failing bpf_object__create_map() destroys the inner map */
1240 obj = bpf_object__open(MAPINMAP_INVALID_PROG);
1241 err = libbpf_get_error(obj);
1243 printf("Failed to load %s program: %d %d",
1244 MAPINMAP_INVALID_PROG, err, errno);
1245 goto out_map_in_map;
1248 map = bpf_object__find_map_by_name(obj, "mim");
1250 printf("Failed to load array of maps from test prog\n");
1251 goto out_map_in_map;
1254 old_print_fn = libbpf_set_print(NULL);
1256 err = bpf_object__load(obj);
1258 printf("Loading obj supposed to fail\n");
1259 goto out_map_in_map;
1262 libbpf_set_print(old_print_fn);
1264 /* Iterate over all maps to check whether the internal map
1265 * ("mim.internal") has been destroyed.
1268 err = bpf_map_get_next_id(id, &id);
1270 if (errno == ENOENT)
1272 printf("Failed to get next map: %d", errno);
1273 goto out_map_in_map;
1276 fd = bpf_map_get_fd_by_id(id);
1278 if (errno == ENOENT)
1280 printf("Failed to get map by id %u: %d", id, errno);
1281 goto out_map_in_map;
1284 err = bpf_obj_get_info_by_fd(fd, &info, &len);
1286 printf("Failed to get map info by fd %d: %d", fd,
1288 goto out_map_in_map;
1291 if (!strcmp(info.name, "mim.inner")) {
1292 printf("Inner map mim.inner was not destroyed\n");
1293 goto out_map_in_map;
1305 #define MAP_SIZE (32 * 1024)
1307 static void test_map_large(void)
1317 fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value),
1318 MAP_SIZE, map_flags);
1320 printf("Failed to create large map '%s'!\n", strerror(errno));
1324 for (i = 0; i < MAP_SIZE; i++) {
1325 key = (struct bigkey) { .c = i };
1328 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == 0);
1332 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) < 0 &&
1335 /* Iterate through all elements. */
1336 assert(bpf_map_get_next_key(fd, NULL, &key) == 0);
1338 for (i = 0; i < MAP_SIZE; i++)
1339 assert(bpf_map_get_next_key(fd, &key, &key) == 0);
1340 assert(bpf_map_get_next_key(fd, &key, &key) < 0 && errno == ENOENT);
1343 assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && value == 0);
1345 assert(bpf_map_lookup_elem(fd, &key, &value) < 0 && errno == ENOENT);
1350 #define run_parallel(N, FN, DATA) \
1351 printf("Fork %u tasks to '" #FN "'\n", N); \
1352 __run_parallel(N, FN, DATA)
1354 static void __run_parallel(unsigned int tasks,
1355 void (*fn)(unsigned int task, void *data),
1363 for (i = 0; i < tasks; i++) {
1368 } else if (pid[i] == -1) {
1369 printf("Couldn't spawn #%d process!\n", i);
1374 for (i = 0; i < tasks; i++) {
1377 assert(waitpid(pid[i], &status, 0) == pid[i]);
1378 assert(status == 0);
1382 static void test_map_stress(void)
1384 run_parallel(100, test_hashmap, NULL);
1385 run_parallel(100, test_hashmap_percpu, NULL);
1386 run_parallel(100, test_hashmap_sizes, NULL);
1387 run_parallel(100, test_hashmap_walk, NULL);
1389 run_parallel(100, test_arraymap, NULL);
1390 run_parallel(100, test_arraymap_percpu, NULL);
1398 #define MAP_RETRIES 20
1400 static int map_update_retriable(int map_fd, const void *key, const void *value,
1401 int flags, int attempts)
1403 while (bpf_map_update_elem(map_fd, key, value, flags)) {
1404 if (!attempts || (errno != EAGAIN && errno != EBUSY))
1414 static int map_delete_retriable(int map_fd, const void *key, int attempts)
1416 while (bpf_map_delete_elem(map_fd, key)) {
1417 if (!attempts || (errno != EAGAIN && errno != EBUSY))
1427 static void test_update_delete(unsigned int fn, void *data)
1429 int do_update = ((int *)data)[1];
1430 int fd = ((int *)data)[0];
1431 int i, key, value, err;
1433 for (i = fn; i < MAP_SIZE; i += TASKS) {
1437 err = map_update_retriable(fd, &key, &value, BPF_NOEXIST, MAP_RETRIES);
1439 printf("error %d %d\n", err, errno);
1441 err = map_update_retriable(fd, &key, &value, BPF_EXIST, MAP_RETRIES);
1443 printf("error %d %d\n", err, errno);
1446 err = map_delete_retriable(fd, &key, MAP_RETRIES);
1448 printf("error %d %d\n", err, errno);
1454 static void test_map_parallel(void)
1456 int i, fd, key = 0, value = 0;
1459 fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value),
1460 MAP_SIZE, map_flags);
1462 printf("Failed to create map for parallel test '%s'!\n",
1467 /* Use the same fd in children to add elements to this map:
1468 * child_0 adds key=0, key=1024, key=2048, ...
1469 * child_1 adds key=1, key=1025, key=2049, ...
1470 * child_1023 adds key=1023, ...
1473 data[1] = DO_UPDATE;
1474 run_parallel(TASKS, test_update_delete, data);
1476 /* Check that key=0 is already there. */
1477 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) < 0 &&
1480 /* Check that all elements were inserted. */
1481 assert(bpf_map_get_next_key(fd, NULL, &key) == 0);
1483 for (i = 0; i < MAP_SIZE; i++)
1484 assert(bpf_map_get_next_key(fd, &key, &key) == 0);
1485 assert(bpf_map_get_next_key(fd, &key, &key) < 0 && errno == ENOENT);
1487 /* Another check for all elements */
1488 for (i = 0; i < MAP_SIZE; i++) {
1489 key = MAP_SIZE - i - 1;
1491 assert(bpf_map_lookup_elem(fd, &key, &value) == 0 &&
1495 /* Now let's delete all elemenets in parallel. */
1496 data[1] = DO_DELETE;
1497 run_parallel(TASKS, test_update_delete, data);
1499 /* Nothing should be left. */
1501 assert(bpf_map_get_next_key(fd, NULL, &key) < 0 && errno == ENOENT);
1502 assert(bpf_map_get_next_key(fd, &key, &key) < 0 && errno == ENOENT);
1505 static void test_map_rdonly(void)
1507 int fd, key = 0, value = 0;
1509 fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value),
1510 MAP_SIZE, map_flags | BPF_F_RDONLY);
1512 printf("Failed to create map for read only test '%s'!\n",
1519 /* Try to insert key=1 element. */
1520 assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) < 0 &&
1523 /* Check that key=1 is not found. */
1524 assert(bpf_map_lookup_elem(fd, &key, &value) < 0 && errno == ENOENT);
1525 assert(bpf_map_get_next_key(fd, &key, &value) < 0 && errno == ENOENT);
1530 static void test_map_wronly_hash(void)
1532 int fd, key = 0, value = 0;
1534 fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value),
1535 MAP_SIZE, map_flags | BPF_F_WRONLY);
1537 printf("Failed to create map for write only test '%s'!\n",
1544 /* Insert key=1 element. */
1545 assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == 0);
1547 /* Check that reading elements and keys from the map is not allowed. */
1548 assert(bpf_map_lookup_elem(fd, &key, &value) < 0 && errno == EPERM);
1549 assert(bpf_map_get_next_key(fd, &key, &value) < 0 && errno == EPERM);
1554 static void test_map_wronly_stack_or_queue(enum bpf_map_type map_type)
1558 assert(map_type == BPF_MAP_TYPE_QUEUE ||
1559 map_type == BPF_MAP_TYPE_STACK);
1560 fd = bpf_create_map(map_type, 0, sizeof(value), MAP_SIZE,
1561 map_flags | BPF_F_WRONLY);
1562 /* Stack/Queue maps do not support BPF_F_NO_PREALLOC */
1563 if (map_flags & BPF_F_NO_PREALLOC) {
1564 assert(fd < 0 && errno == EINVAL);
1568 printf("Failed to create map '%s'!\n", strerror(errno));
1573 assert(bpf_map_update_elem(fd, NULL, &value, BPF_ANY) == 0);
1575 /* Peek element should fail */
1576 assert(bpf_map_lookup_elem(fd, NULL, &value) < 0 && errno == EPERM);
1578 /* Pop element should fail */
1579 assert(bpf_map_lookup_and_delete_elem(fd, NULL, &value) < 0 &&
1585 static void test_map_wronly(void)
1587 test_map_wronly_hash();
1588 test_map_wronly_stack_or_queue(BPF_MAP_TYPE_STACK);
1589 test_map_wronly_stack_or_queue(BPF_MAP_TYPE_QUEUE);
1592 static void prepare_reuseport_grp(int type, int map_fd, size_t map_elem_size,
1593 __s64 *fds64, __u64 *sk_cookies,
1596 socklen_t optlen, addrlen;
1597 struct sockaddr_in6 s6;
1598 const __u32 index0 = 0;
1599 const int optval = 1;
1607 s6.sin6_family = AF_INET6;
1608 s6.sin6_addr = in6addr_any;
1610 addrlen = sizeof(s6);
1611 optlen = sizeof(sk_cookie);
1613 for (i = 0; i < n; i++) {
1614 fd64 = socket(AF_INET6, type, 0);
1615 CHECK(fd64 == -1, "socket()",
1616 "sock_type:%d fd64:%lld errno:%d\n",
1619 err = setsockopt(fd64, SOL_SOCKET, SO_REUSEPORT,
1620 &optval, sizeof(optval));
1621 CHECK(err == -1, "setsockopt(SO_REUSEPORT)",
1622 "err:%d errno:%d\n", err, errno);
1624 /* reuseport_array does not allow unbound sk */
1625 if (map_elem_size == sizeof(__u64))
1628 assert(map_elem_size == sizeof(__u32));
1632 err = bpf_map_update_elem(map_fd, &index0, value, BPF_ANY);
1633 CHECK(err >= 0 || errno != EINVAL,
1634 "reuseport array update unbound sk",
1635 "sock_type:%d err:%d errno:%d\n",
1638 err = bind(fd64, (struct sockaddr *)&s6, sizeof(s6));
1639 CHECK(err == -1, "bind()",
1640 "sock_type:%d err:%d errno:%d\n", type, err, errno);
1643 err = getsockname(fd64, (struct sockaddr *)&s6,
1645 CHECK(err == -1, "getsockname()",
1646 "sock_type:%d err:%d errno:%d\n",
1650 err = getsockopt(fd64, SOL_SOCKET, SO_COOKIE, &sk_cookie,
1652 CHECK(err == -1, "getsockopt(SO_COOKIE)",
1653 "sock_type:%d err:%d errno:%d\n", type, err, errno);
1655 if (type == SOCK_STREAM) {
1657 * reuseport_array does not allow
1658 * non-listening tcp sk.
1660 err = bpf_map_update_elem(map_fd, &index0, value,
1662 CHECK(err >= 0 || errno != EINVAL,
1663 "reuseport array update non-listening sk",
1664 "sock_type:%d err:%d errno:%d\n",
1666 err = listen(fd64, 0);
1667 CHECK(err == -1, "listen()",
1668 "sock_type:%d, err:%d errno:%d\n",
1673 sk_cookies[i] = sk_cookie;
1677 static void test_reuseport_array(void)
1679 #define REUSEPORT_FD_IDX(err, last) ({ (err) ? last : !last; })
1681 const __u32 array_size = 4, index0 = 0, index3 = 3;
1682 int types[2] = { SOCK_STREAM, SOCK_DGRAM }, type;
1683 __u64 grpa_cookies[2], sk_cookie, map_cookie;
1684 __s64 grpa_fds64[2] = { -1, -1 }, fd64 = -1;
1685 const __u32 bad_index = array_size;
1686 int map_fd, err, t, f;
1690 map_fd = bpf_create_map(BPF_MAP_TYPE_REUSEPORT_SOCKARRAY,
1691 sizeof(__u32), sizeof(__u64), array_size, 0);
1692 CHECK(map_fd < 0, "reuseport array create",
1693 "map_fd:%d, errno:%d\n", map_fd, errno);
1695 /* Test lookup/update/delete with invalid index */
1696 err = bpf_map_delete_elem(map_fd, &bad_index);
1697 CHECK(err >= 0 || errno != E2BIG, "reuseport array del >=max_entries",
1698 "err:%d errno:%d\n", err, errno);
1700 err = bpf_map_update_elem(map_fd, &bad_index, &fd64, BPF_ANY);
1701 CHECK(err >= 0 || errno != E2BIG,
1702 "reuseport array update >=max_entries",
1703 "err:%d errno:%d\n", err, errno);
1705 err = bpf_map_lookup_elem(map_fd, &bad_index, &map_cookie);
1706 CHECK(err >= 0 || errno != ENOENT,
1707 "reuseport array update >=max_entries",
1708 "err:%d errno:%d\n", err, errno);
1710 /* Test lookup/delete non existence elem */
1711 err = bpf_map_lookup_elem(map_fd, &index3, &map_cookie);
1712 CHECK(err >= 0 || errno != ENOENT,
1713 "reuseport array lookup not-exist elem",
1714 "err:%d errno:%d\n", err, errno);
1715 err = bpf_map_delete_elem(map_fd, &index3);
1716 CHECK(err >= 0 || errno != ENOENT,
1717 "reuseport array del not-exist elem",
1718 "err:%d errno:%d\n", err, errno);
1720 for (t = 0; t < ARRAY_SIZE(types); t++) {
1723 prepare_reuseport_grp(type, map_fd, sizeof(__u64), grpa_fds64,
1724 grpa_cookies, ARRAY_SIZE(grpa_fds64));
1726 /* Test BPF_* update flags */
1727 /* BPF_EXIST failure case */
1728 err = bpf_map_update_elem(map_fd, &index3, &grpa_fds64[fds_idx],
1730 CHECK(err >= 0 || errno != ENOENT,
1731 "reuseport array update empty elem BPF_EXIST",
1732 "sock_type:%d err:%d errno:%d\n",
1734 fds_idx = REUSEPORT_FD_IDX(err, fds_idx);
1736 /* BPF_NOEXIST success case */
1737 err = bpf_map_update_elem(map_fd, &index3, &grpa_fds64[fds_idx],
1740 "reuseport array update empty elem BPF_NOEXIST",
1741 "sock_type:%d err:%d errno:%d\n",
1743 fds_idx = REUSEPORT_FD_IDX(err, fds_idx);
1745 /* BPF_EXIST success case. */
1746 err = bpf_map_update_elem(map_fd, &index3, &grpa_fds64[fds_idx],
1749 "reuseport array update same elem BPF_EXIST",
1750 "sock_type:%d err:%d errno:%d\n", type, err, errno);
1751 fds_idx = REUSEPORT_FD_IDX(err, fds_idx);
1753 /* BPF_NOEXIST failure case */
1754 err = bpf_map_update_elem(map_fd, &index3, &grpa_fds64[fds_idx],
1756 CHECK(err >= 0 || errno != EEXIST,
1757 "reuseport array update non-empty elem BPF_NOEXIST",
1758 "sock_type:%d err:%d errno:%d\n",
1760 fds_idx = REUSEPORT_FD_IDX(err, fds_idx);
1762 /* BPF_ANY case (always succeed) */
1763 err = bpf_map_update_elem(map_fd, &index3, &grpa_fds64[fds_idx],
1766 "reuseport array update same sk with BPF_ANY",
1767 "sock_type:%d err:%d errno:%d\n", type, err, errno);
1769 fd64 = grpa_fds64[fds_idx];
1770 sk_cookie = grpa_cookies[fds_idx];
1772 /* The same sk cannot be added to reuseport_array twice */
1773 err = bpf_map_update_elem(map_fd, &index3, &fd64, BPF_ANY);
1774 CHECK(err >= 0 || errno != EBUSY,
1775 "reuseport array update same sk with same index",
1776 "sock_type:%d err:%d errno:%d\n",
1779 err = bpf_map_update_elem(map_fd, &index0, &fd64, BPF_ANY);
1780 CHECK(err >= 0 || errno != EBUSY,
1781 "reuseport array update same sk with different index",
1782 "sock_type:%d err:%d errno:%d\n",
1785 /* Test delete elem */
1786 err = bpf_map_delete_elem(map_fd, &index3);
1787 CHECK(err < 0, "reuseport array delete sk",
1788 "sock_type:%d err:%d errno:%d\n",
1791 /* Add it back with BPF_NOEXIST */
1792 err = bpf_map_update_elem(map_fd, &index3, &fd64, BPF_NOEXIST);
1794 "reuseport array re-add with BPF_NOEXIST after del",
1795 "sock_type:%d err:%d errno:%d\n", type, err, errno);
1798 err = bpf_map_lookup_elem(map_fd, &index3, &map_cookie);
1799 CHECK(err < 0 || sk_cookie != map_cookie,
1800 "reuseport array lookup re-added sk",
1801 "sock_type:%d err:%d errno:%d sk_cookie:0x%llx map_cookie:0x%llxn",
1802 type, err, errno, sk_cookie, map_cookie);
1804 /* Test elem removed by close() */
1805 for (f = 0; f < ARRAY_SIZE(grpa_fds64); f++)
1806 close(grpa_fds64[f]);
1807 err = bpf_map_lookup_elem(map_fd, &index3, &map_cookie);
1808 CHECK(err >= 0 || errno != ENOENT,
1809 "reuseport array lookup after close()",
1810 "sock_type:%d err:%d errno:%d\n",
1815 fd64 = socket(AF_INET6, SOCK_RAW, IPPROTO_UDP);
1816 CHECK(fd64 == -1, "socket(SOCK_RAW)", "err:%d errno:%d\n",
1818 err = bpf_map_update_elem(map_fd, &index3, &fd64, BPF_NOEXIST);
1819 CHECK(err >= 0 || errno != ENOTSUPP, "reuseport array update SOCK_RAW",
1820 "err:%d errno:%d\n", err, errno);
1823 /* Close the 64 bit value map */
1826 /* Test 32 bit fd */
1827 map_fd = bpf_create_map(BPF_MAP_TYPE_REUSEPORT_SOCKARRAY,
1828 sizeof(__u32), sizeof(__u32), array_size, 0);
1829 CHECK(map_fd < 0, "reuseport array create",
1830 "map_fd:%d, errno:%d\n", map_fd, errno);
1831 prepare_reuseport_grp(SOCK_STREAM, map_fd, sizeof(__u32), &fd64,
1834 err = bpf_map_update_elem(map_fd, &index3, &fd, BPF_NOEXIST);
1835 CHECK(err < 0, "reuseport array update 32 bit fd",
1836 "err:%d errno:%d\n", err, errno);
1837 err = bpf_map_lookup_elem(map_fd, &index3, &map_cookie);
1838 CHECK(err >= 0 || errno != ENOSPC,
1839 "reuseport array lookup 32 bit fd",
1840 "err:%d errno:%d\n", err, errno);
1845 static void run_all_tests(void)
1847 test_hashmap(0, NULL);
1848 test_hashmap_percpu(0, NULL);
1849 test_hashmap_walk(0, NULL);
1850 test_hashmap_zero_seed();
1852 test_arraymap(0, NULL);
1853 test_arraymap_percpu(0, NULL);
1855 test_arraymap_percpu_many_keys();
1857 test_devmap(0, NULL);
1858 test_devmap_hash(0, NULL);
1859 test_sockmap(0, NULL);
1862 test_map_parallel();
1868 test_reuseport_array();
1870 test_queuemap(0, NULL);
1871 test_stackmap(0, NULL);
1876 #define DEFINE_TEST(name) extern void test_##name(void);
1877 #include <map_tests/tests.h>
1884 libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
1889 map_flags = BPF_F_NO_PREALLOC;
1892 #define DEFINE_TEST(name) test_##name();
1893 #include <map_tests/tests.h>
1896 printf("test_maps: OK, %d SKIPPED\n", skips);