2 * Testsuite for eBPF maps
4 * Copyright (c) 2014 PLUMgrid, http://plumgrid.com
5 * Copyright (c) 2016 Facebook
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of version 2 of the GNU General Public
9 * License as published by the Free Software Foundation.
21 #include <sys/socket.h>
22 #include <netinet/in.h>
23 #include <linux/bpf.h>
26 #include <bpf/libbpf.h>
29 #include "bpf_rlimit.h"
37 #define CHECK(condition, tag, format...) ({ \
38 int __ret = !!(condition); \
40 printf("%s(%d):FAIL:%s ", __func__, __LINE__, tag); \
46 static void test_hashmap(int task, void *data)
48 long long key, next_key, first_key, value;
51 fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value),
54 printf("Failed to create hashmap '%s'!\n", strerror(errno));
60 /* Insert key=1 element. */
61 assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == 0);
64 /* BPF_NOEXIST means add new element if it doesn't exist. */
65 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == -1 &&
66 /* key=1 already exists. */
69 /* -1 is an invalid flag. */
70 assert(bpf_map_update_elem(fd, &key, &value, -1) == -1 &&
73 /* Check that key=1 can be found. */
74 assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && value == 1234);
77 /* Check that key=2 is not found. */
78 assert(bpf_map_lookup_elem(fd, &key, &value) == -1 && errno == ENOENT);
80 /* BPF_EXIST means update existing element. */
81 assert(bpf_map_update_elem(fd, &key, &value, BPF_EXIST) == -1 &&
82 /* key=2 is not there. */
85 /* Insert key=2 element. */
86 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == 0);
88 /* key=1 and key=2 were inserted, check that key=0 cannot be
89 * inserted due to max_entries limit.
92 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == -1 &&
95 /* Update existing element, though the map is full. */
97 assert(bpf_map_update_elem(fd, &key, &value, BPF_EXIST) == 0);
99 assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == 0);
101 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == -1 &&
104 /* Check that key = 0 doesn't exist. */
106 assert(bpf_map_delete_elem(fd, &key) == -1 && errno == ENOENT);
108 /* Iterate over two elements. */
109 assert(bpf_map_get_next_key(fd, NULL, &first_key) == 0 &&
110 (first_key == 1 || first_key == 2));
111 assert(bpf_map_get_next_key(fd, &key, &next_key) == 0 &&
112 (next_key == first_key));
113 assert(bpf_map_get_next_key(fd, &next_key, &next_key) == 0 &&
114 (next_key == 1 || next_key == 2) &&
115 (next_key != first_key));
116 assert(bpf_map_get_next_key(fd, &next_key, &next_key) == -1 &&
119 /* Delete both elements. */
121 assert(bpf_map_delete_elem(fd, &key) == 0);
123 assert(bpf_map_delete_elem(fd, &key) == 0);
124 assert(bpf_map_delete_elem(fd, &key) == -1 && errno == ENOENT);
127 /* Check that map is empty. */
128 assert(bpf_map_get_next_key(fd, NULL, &next_key) == -1 &&
130 assert(bpf_map_get_next_key(fd, &key, &next_key) == -1 &&
136 static void test_hashmap_sizes(int task, void *data)
140 for (i = 1; i <= 512; i <<= 1)
141 for (j = 1; j <= 1 << 18; j <<= 1) {
142 fd = bpf_create_map(BPF_MAP_TYPE_HASH, i, j,
147 printf("Failed to create hashmap key=%d value=%d '%s'\n",
148 i, j, strerror(errno));
152 usleep(10); /* give kernel time to destroy */
156 static void test_hashmap_percpu(int task, void *data)
158 unsigned int nr_cpus = bpf_num_possible_cpus();
159 BPF_DECLARE_PERCPU(long, value);
160 long long key, next_key, first_key;
161 int expected_key_mask = 0;
164 fd = bpf_create_map(BPF_MAP_TYPE_PERCPU_HASH, sizeof(key),
165 sizeof(bpf_percpu(value, 0)), 2, map_flags);
167 printf("Failed to create hashmap '%s'!\n", strerror(errno));
171 for (i = 0; i < nr_cpus; i++)
172 bpf_percpu(value, i) = i + 100;
175 /* Insert key=1 element. */
176 assert(!(expected_key_mask & key));
177 assert(bpf_map_update_elem(fd, &key, value, BPF_ANY) == 0);
178 expected_key_mask |= key;
180 /* BPF_NOEXIST means add new element if it doesn't exist. */
181 assert(bpf_map_update_elem(fd, &key, value, BPF_NOEXIST) == -1 &&
182 /* key=1 already exists. */
185 /* -1 is an invalid flag. */
186 assert(bpf_map_update_elem(fd, &key, value, -1) == -1 &&
189 /* Check that key=1 can be found. Value could be 0 if the lookup
190 * was run from a different CPU.
192 bpf_percpu(value, 0) = 1;
193 assert(bpf_map_lookup_elem(fd, &key, value) == 0 &&
194 bpf_percpu(value, 0) == 100);
197 /* Check that key=2 is not found. */
198 assert(bpf_map_lookup_elem(fd, &key, value) == -1 && errno == ENOENT);
200 /* BPF_EXIST means update existing element. */
201 assert(bpf_map_update_elem(fd, &key, value, BPF_EXIST) == -1 &&
202 /* key=2 is not there. */
205 /* Insert key=2 element. */
206 assert(!(expected_key_mask & key));
207 assert(bpf_map_update_elem(fd, &key, value, BPF_NOEXIST) == 0);
208 expected_key_mask |= key;
210 /* key=1 and key=2 were inserted, check that key=0 cannot be
211 * inserted due to max_entries limit.
214 assert(bpf_map_update_elem(fd, &key, value, BPF_NOEXIST) == -1 &&
217 /* Check that key = 0 doesn't exist. */
218 assert(bpf_map_delete_elem(fd, &key) == -1 && errno == ENOENT);
220 /* Iterate over two elements. */
221 assert(bpf_map_get_next_key(fd, NULL, &first_key) == 0 &&
222 ((expected_key_mask & first_key) == first_key));
223 while (!bpf_map_get_next_key(fd, &key, &next_key)) {
225 assert(next_key == first_key);
228 assert((expected_key_mask & next_key) == next_key);
229 expected_key_mask &= ~next_key;
231 assert(bpf_map_lookup_elem(fd, &next_key, value) == 0);
233 for (i = 0; i < nr_cpus; i++)
234 assert(bpf_percpu(value, i) == i + 100);
238 assert(errno == ENOENT);
240 /* Update with BPF_EXIST. */
242 assert(bpf_map_update_elem(fd, &key, value, BPF_EXIST) == 0);
244 /* Delete both elements. */
246 assert(bpf_map_delete_elem(fd, &key) == 0);
248 assert(bpf_map_delete_elem(fd, &key) == 0);
249 assert(bpf_map_delete_elem(fd, &key) == -1 && errno == ENOENT);
252 /* Check that map is empty. */
253 assert(bpf_map_get_next_key(fd, NULL, &next_key) == -1 &&
255 assert(bpf_map_get_next_key(fd, &key, &next_key) == -1 &&
261 static int helper_fill_hashmap(int max_entries)
264 long long key, value;
266 fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value),
267 max_entries, map_flags);
269 "failed to create hashmap",
270 "err: %s, flags: 0x%x\n", strerror(errno), map_flags);
272 for (i = 0; i < max_entries; i++) {
273 key = i; value = key;
274 ret = bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST);
276 "can't update hashmap",
277 "err: %s\n", strerror(ret));
283 static void test_hashmap_walk(int task, void *data)
285 int fd, i, max_entries = 1000;
286 long long key, value, next_key;
287 bool next_key_valid = true;
289 fd = helper_fill_hashmap(max_entries);
291 for (i = 0; bpf_map_get_next_key(fd, !i ? NULL : &key,
292 &next_key) == 0; i++) {
294 assert(bpf_map_lookup_elem(fd, &key, &value) == 0);
297 assert(i == max_entries);
299 assert(bpf_map_get_next_key(fd, NULL, &key) == 0);
300 for (i = 0; next_key_valid; i++) {
301 next_key_valid = bpf_map_get_next_key(fd, &key, &next_key) == 0;
302 assert(bpf_map_lookup_elem(fd, &key, &value) == 0);
304 assert(bpf_map_update_elem(fd, &key, &value, BPF_EXIST) == 0);
308 assert(i == max_entries);
310 for (i = 0; bpf_map_get_next_key(fd, !i ? NULL : &key,
311 &next_key) == 0; i++) {
313 assert(bpf_map_lookup_elem(fd, &key, &value) == 0);
314 assert(value - 1 == key);
317 assert(i == max_entries);
321 static void test_hashmap_zero_seed(void)
323 int i, first, second, old_flags;
324 long long key, next_first, next_second;
326 old_flags = map_flags;
327 map_flags |= BPF_F_ZERO_SEED;
329 first = helper_fill_hashmap(3);
330 second = helper_fill_hashmap(3);
333 void *key_ptr = !i ? NULL : &key;
335 if (bpf_map_get_next_key(first, key_ptr, &next_first) != 0)
338 CHECK(bpf_map_get_next_key(second, key_ptr, &next_second) != 0,
339 "next_key for second map must succeed",
340 "key_ptr: %p", key_ptr);
341 CHECK(next_first != next_second,
343 "i: %d first: %lld second: %lld\n", i,
344 next_first, next_second);
349 map_flags = old_flags;
354 static void test_arraymap(int task, void *data)
356 int key, next_key, fd;
359 fd = bpf_create_map(BPF_MAP_TYPE_ARRAY, sizeof(key), sizeof(value),
362 printf("Failed to create arraymap '%s'!\n", strerror(errno));
368 /* Insert key=1 element. */
369 assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == 0);
372 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == -1 &&
375 /* Check that key=1 can be found. */
376 assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && value == 1234);
379 /* Check that key=0 is also found and zero initialized. */
380 assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && value == 0);
382 /* key=0 and key=1 were inserted, check that key=2 cannot be inserted
383 * due to max_entries limit.
386 assert(bpf_map_update_elem(fd, &key, &value, BPF_EXIST) == -1 &&
389 /* Check that key = 2 doesn't exist. */
390 assert(bpf_map_lookup_elem(fd, &key, &value) == -1 && errno == ENOENT);
392 /* Iterate over two elements. */
393 assert(bpf_map_get_next_key(fd, NULL, &next_key) == 0 &&
395 assert(bpf_map_get_next_key(fd, &key, &next_key) == 0 &&
397 assert(bpf_map_get_next_key(fd, &next_key, &next_key) == 0 &&
399 assert(bpf_map_get_next_key(fd, &next_key, &next_key) == -1 &&
402 /* Delete shouldn't succeed. */
404 assert(bpf_map_delete_elem(fd, &key) == -1 && errno == EINVAL);
409 static void test_arraymap_percpu(int task, void *data)
411 unsigned int nr_cpus = bpf_num_possible_cpus();
412 BPF_DECLARE_PERCPU(long, values);
413 int key, next_key, fd, i;
415 fd = bpf_create_map(BPF_MAP_TYPE_PERCPU_ARRAY, sizeof(key),
416 sizeof(bpf_percpu(values, 0)), 2, 0);
418 printf("Failed to create arraymap '%s'!\n", strerror(errno));
422 for (i = 0; i < nr_cpus; i++)
423 bpf_percpu(values, i) = i + 100;
426 /* Insert key=1 element. */
427 assert(bpf_map_update_elem(fd, &key, values, BPF_ANY) == 0);
429 bpf_percpu(values, 0) = 0;
430 assert(bpf_map_update_elem(fd, &key, values, BPF_NOEXIST) == -1 &&
433 /* Check that key=1 can be found. */
434 assert(bpf_map_lookup_elem(fd, &key, values) == 0 &&
435 bpf_percpu(values, 0) == 100);
438 /* Check that key=0 is also found and zero initialized. */
439 assert(bpf_map_lookup_elem(fd, &key, values) == 0 &&
440 bpf_percpu(values, 0) == 0 &&
441 bpf_percpu(values, nr_cpus - 1) == 0);
443 /* Check that key=2 cannot be inserted due to max_entries limit. */
445 assert(bpf_map_update_elem(fd, &key, values, BPF_EXIST) == -1 &&
448 /* Check that key = 2 doesn't exist. */
449 assert(bpf_map_lookup_elem(fd, &key, values) == -1 && errno == ENOENT);
451 /* Iterate over two elements. */
452 assert(bpf_map_get_next_key(fd, NULL, &next_key) == 0 &&
454 assert(bpf_map_get_next_key(fd, &key, &next_key) == 0 &&
456 assert(bpf_map_get_next_key(fd, &next_key, &next_key) == 0 &&
458 assert(bpf_map_get_next_key(fd, &next_key, &next_key) == -1 &&
461 /* Delete shouldn't succeed. */
463 assert(bpf_map_delete_elem(fd, &key) == -1 && errno == EINVAL);
468 static void test_arraymap_percpu_many_keys(void)
470 unsigned int nr_cpus = bpf_num_possible_cpus();
471 BPF_DECLARE_PERCPU(long, values);
472 /* nr_keys is not too large otherwise the test stresses percpu
473 * allocator more than anything else
475 unsigned int nr_keys = 2000;
478 fd = bpf_create_map(BPF_MAP_TYPE_PERCPU_ARRAY, sizeof(key),
479 sizeof(bpf_percpu(values, 0)), nr_keys, 0);
481 printf("Failed to create per-cpu arraymap '%s'!\n",
486 for (i = 0; i < nr_cpus; i++)
487 bpf_percpu(values, i) = i + 10;
489 for (key = 0; key < nr_keys; key++)
490 assert(bpf_map_update_elem(fd, &key, values, BPF_ANY) == 0);
492 for (key = 0; key < nr_keys; key++) {
493 for (i = 0; i < nr_cpus; i++)
494 bpf_percpu(values, i) = 0;
496 assert(bpf_map_lookup_elem(fd, &key, values) == 0);
498 for (i = 0; i < nr_cpus; i++)
499 assert(bpf_percpu(values, i) == i + 10);
505 static void test_devmap(int task, void *data)
510 fd = bpf_create_map(BPF_MAP_TYPE_DEVMAP, sizeof(key), sizeof(value),
513 printf("Failed to create devmap '%s'!\n", strerror(errno));
520 static void test_queuemap(int task, void *data)
522 const int MAP_SIZE = 32;
523 __u32 vals[MAP_SIZE + MAP_SIZE/2], val;
526 /* Fill test values to be used */
527 for (i = 0; i < MAP_SIZE + MAP_SIZE/2; i++)
530 /* Invalid key size */
531 fd = bpf_create_map(BPF_MAP_TYPE_QUEUE, 4, sizeof(val), MAP_SIZE,
533 assert(fd < 0 && errno == EINVAL);
535 fd = bpf_create_map(BPF_MAP_TYPE_QUEUE, 0, sizeof(val), MAP_SIZE,
537 /* Queue map does not support BPF_F_NO_PREALLOC */
538 if (map_flags & BPF_F_NO_PREALLOC) {
539 assert(fd < 0 && errno == EINVAL);
543 printf("Failed to create queuemap '%s'!\n", strerror(errno));
547 /* Push MAP_SIZE elements */
548 for (i = 0; i < MAP_SIZE; i++)
549 assert(bpf_map_update_elem(fd, NULL, &vals[i], 0) == 0);
551 /* Check that element cannot be pushed due to max_entries limit */
552 assert(bpf_map_update_elem(fd, NULL, &val, 0) == -1 &&
556 assert(bpf_map_lookup_elem(fd, NULL, &val) == 0 && val == vals[0]);
558 /* Replace half elements */
559 for (i = MAP_SIZE; i < MAP_SIZE + MAP_SIZE/2; i++)
560 assert(bpf_map_update_elem(fd, NULL, &vals[i], BPF_EXIST) == 0);
562 /* Pop all elements */
563 for (i = MAP_SIZE/2; i < MAP_SIZE + MAP_SIZE/2; i++)
564 assert(bpf_map_lookup_and_delete_elem(fd, NULL, &val) == 0 &&
567 /* Check that there are not elements left */
568 assert(bpf_map_lookup_and_delete_elem(fd, NULL, &val) == -1 &&
571 /* Check that non supported functions set errno to EINVAL */
572 assert(bpf_map_delete_elem(fd, NULL) == -1 && errno == EINVAL);
573 assert(bpf_map_get_next_key(fd, NULL, NULL) == -1 && errno == EINVAL);
578 static void test_stackmap(int task, void *data)
580 const int MAP_SIZE = 32;
581 __u32 vals[MAP_SIZE + MAP_SIZE/2], val;
584 /* Fill test values to be used */
585 for (i = 0; i < MAP_SIZE + MAP_SIZE/2; i++)
588 /* Invalid key size */
589 fd = bpf_create_map(BPF_MAP_TYPE_STACK, 4, sizeof(val), MAP_SIZE,
591 assert(fd < 0 && errno == EINVAL);
593 fd = bpf_create_map(BPF_MAP_TYPE_STACK, 0, sizeof(val), MAP_SIZE,
595 /* Stack map does not support BPF_F_NO_PREALLOC */
596 if (map_flags & BPF_F_NO_PREALLOC) {
597 assert(fd < 0 && errno == EINVAL);
601 printf("Failed to create stackmap '%s'!\n", strerror(errno));
605 /* Push MAP_SIZE elements */
606 for (i = 0; i < MAP_SIZE; i++)
607 assert(bpf_map_update_elem(fd, NULL, &vals[i], 0) == 0);
609 /* Check that element cannot be pushed due to max_entries limit */
610 assert(bpf_map_update_elem(fd, NULL, &val, 0) == -1 &&
614 assert(bpf_map_lookup_elem(fd, NULL, &val) == 0 && val == vals[i - 1]);
616 /* Replace half elements */
617 for (i = MAP_SIZE; i < MAP_SIZE + MAP_SIZE/2; i++)
618 assert(bpf_map_update_elem(fd, NULL, &vals[i], BPF_EXIST) == 0);
620 /* Pop all elements */
621 for (i = MAP_SIZE + MAP_SIZE/2 - 1; i >= MAP_SIZE/2; i--)
622 assert(bpf_map_lookup_and_delete_elem(fd, NULL, &val) == 0 &&
625 /* Check that there are not elements left */
626 assert(bpf_map_lookup_and_delete_elem(fd, NULL, &val) == -1 &&
629 /* Check that non supported functions set errno to EINVAL */
630 assert(bpf_map_delete_elem(fd, NULL) == -1 && errno == EINVAL);
631 assert(bpf_map_get_next_key(fd, NULL, NULL) == -1 && errno == EINVAL);
636 #include <sys/socket.h>
637 #include <sys/ioctl.h>
638 #include <arpa/inet.h>
639 #include <sys/select.h>
640 #include <linux/err.h>
641 #define SOCKMAP_PARSE_PROG "./sockmap_parse_prog.o"
642 #define SOCKMAP_VERDICT_PROG "./sockmap_verdict_prog.o"
643 #define SOCKMAP_TCP_MSG_PROG "./sockmap_tcp_msg_prog.o"
644 static void test_sockmap(int tasks, void *data)
646 struct bpf_map *bpf_map_rx, *bpf_map_tx, *bpf_map_msg, *bpf_map_break;
647 int map_fd_msg = 0, map_fd_rx = 0, map_fd_tx = 0, map_fd_break;
648 int ports[] = {50200, 50201, 50202, 50204};
649 int err, i, fd, udp, sfd[6] = {0xdeadbeef};
650 u8 buf[20] = {0x0, 0x5, 0x3, 0x2, 0x1, 0x0};
651 int parse_prog, verdict_prog, msg_prog;
652 struct sockaddr_in addr;
653 int one = 1, s, sc, rc;
654 struct bpf_object *obj;
660 /* Create some sockets to use with sockmap */
661 for (i = 0; i < 2; i++) {
662 sfd[i] = socket(AF_INET, SOCK_STREAM, 0);
665 err = setsockopt(sfd[i], SOL_SOCKET, SO_REUSEADDR,
666 (char *)&one, sizeof(one));
668 printf("failed to setsockopt\n");
671 err = ioctl(sfd[i], FIONBIO, (char *)&one);
673 printf("failed to ioctl\n");
676 memset(&addr, 0, sizeof(struct sockaddr_in));
677 addr.sin_family = AF_INET;
678 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
679 addr.sin_port = htons(ports[i]);
680 err = bind(sfd[i], (struct sockaddr *)&addr, sizeof(addr));
682 printf("failed to bind: err %i: %i:%i\n",
686 err = listen(sfd[i], 32);
688 printf("failed to listen\n");
693 for (i = 2; i < 4; i++) {
694 sfd[i] = socket(AF_INET, SOCK_STREAM, 0);
697 err = setsockopt(sfd[i], SOL_SOCKET, SO_REUSEADDR,
698 (char *)&one, sizeof(one));
700 printf("set sock opt\n");
703 memset(&addr, 0, sizeof(struct sockaddr_in));
704 addr.sin_family = AF_INET;
705 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
706 addr.sin_port = htons(ports[i - 2]);
707 err = connect(sfd[i], (struct sockaddr *)&addr, sizeof(addr));
709 printf("failed to connect\n");
715 for (i = 4; i < 6; i++) {
716 sfd[i] = accept(sfd[i - 4], NULL, NULL);
718 printf("accept failed\n");
723 /* Test sockmap with connected sockets */
724 fd = bpf_create_map(BPF_MAP_TYPE_SOCKMAP,
725 sizeof(key), sizeof(value),
728 printf("Failed to create sockmap %i\n", fd);
732 /* Test update with unsupported UDP socket */
733 udp = socket(AF_INET, SOCK_DGRAM, 0);
735 err = bpf_map_update_elem(fd, &i, &udp, BPF_ANY);
737 printf("Failed socket SOCK_DGRAM allowed '%i:%i'\n",
742 /* Test update without programs */
743 for (i = 0; i < 6; i++) {
744 err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_ANY);
746 printf("Allowed update sockmap '%i:%i' not in ESTABLISHED\n",
749 } else if (i >= 2 && err) {
750 printf("Failed noprog update sockmap '%i:%i'\n",
756 /* Test attaching/detaching bad fds */
757 err = bpf_prog_attach(-1, fd, BPF_SK_SKB_STREAM_PARSER, 0);
759 printf("Failed invalid parser prog attach\n");
763 err = bpf_prog_attach(-1, fd, BPF_SK_SKB_STREAM_VERDICT, 0);
765 printf("Failed invalid verdict prog attach\n");
769 err = bpf_prog_attach(-1, fd, BPF_SK_MSG_VERDICT, 0);
771 printf("Failed invalid msg verdict prog attach\n");
775 err = bpf_prog_attach(-1, fd, __MAX_BPF_ATTACH_TYPE, 0);
777 printf("Failed unknown prog attach\n");
781 err = bpf_prog_detach(fd, BPF_SK_SKB_STREAM_PARSER);
783 printf("Failed empty parser prog detach\n");
787 err = bpf_prog_detach(fd, BPF_SK_SKB_STREAM_VERDICT);
789 printf("Failed empty verdict prog detach\n");
793 err = bpf_prog_detach(fd, BPF_SK_MSG_VERDICT);
795 printf("Failed empty msg verdict prog detach\n");
799 err = bpf_prog_detach(fd, __MAX_BPF_ATTACH_TYPE);
801 printf("Detach invalid prog successful\n");
805 /* Load SK_SKB program and Attach */
806 err = bpf_prog_load(SOCKMAP_PARSE_PROG,
807 BPF_PROG_TYPE_SK_SKB, &obj, &parse_prog);
809 printf("Failed to load SK_SKB parse prog\n");
813 err = bpf_prog_load(SOCKMAP_TCP_MSG_PROG,
814 BPF_PROG_TYPE_SK_MSG, &obj, &msg_prog);
816 printf("Failed to load SK_SKB msg prog\n");
820 err = bpf_prog_load(SOCKMAP_VERDICT_PROG,
821 BPF_PROG_TYPE_SK_SKB, &obj, &verdict_prog);
823 printf("Failed to load SK_SKB verdict prog\n");
827 bpf_map_rx = bpf_object__find_map_by_name(obj, "sock_map_rx");
828 if (IS_ERR(bpf_map_rx)) {
829 printf("Failed to load map rx from verdict prog\n");
833 map_fd_rx = bpf_map__fd(bpf_map_rx);
835 printf("Failed to get map rx fd\n");
839 bpf_map_tx = bpf_object__find_map_by_name(obj, "sock_map_tx");
840 if (IS_ERR(bpf_map_tx)) {
841 printf("Failed to load map tx from verdict prog\n");
845 map_fd_tx = bpf_map__fd(bpf_map_tx);
847 printf("Failed to get map tx fd\n");
851 bpf_map_msg = bpf_object__find_map_by_name(obj, "sock_map_msg");
852 if (IS_ERR(bpf_map_msg)) {
853 printf("Failed to load map msg from msg_verdict prog\n");
857 map_fd_msg = bpf_map__fd(bpf_map_msg);
858 if (map_fd_msg < 0) {
859 printf("Failed to get map msg fd\n");
863 bpf_map_break = bpf_object__find_map_by_name(obj, "sock_map_break");
864 if (IS_ERR(bpf_map_break)) {
865 printf("Failed to load map tx from verdict prog\n");
869 map_fd_break = bpf_map__fd(bpf_map_break);
870 if (map_fd_break < 0) {
871 printf("Failed to get map tx fd\n");
875 err = bpf_prog_attach(parse_prog, map_fd_break,
876 BPF_SK_SKB_STREAM_PARSER, 0);
878 printf("Allowed attaching SK_SKB program to invalid map\n");
882 err = bpf_prog_attach(parse_prog, map_fd_rx,
883 BPF_SK_SKB_STREAM_PARSER, 0);
885 printf("Failed stream parser bpf prog attach\n");
889 err = bpf_prog_attach(verdict_prog, map_fd_rx,
890 BPF_SK_SKB_STREAM_VERDICT, 0);
892 printf("Failed stream verdict bpf prog attach\n");
896 err = bpf_prog_attach(msg_prog, map_fd_msg, BPF_SK_MSG_VERDICT, 0);
898 printf("Failed msg verdict bpf prog attach\n");
902 err = bpf_prog_attach(verdict_prog, map_fd_rx,
903 __MAX_BPF_ATTACH_TYPE, 0);
905 printf("Attached unknown bpf prog\n");
909 /* Test map update elem afterwards fd lives in fd and map_fd */
910 for (i = 2; i < 6; i++) {
911 err = bpf_map_update_elem(map_fd_rx, &i, &sfd[i], BPF_ANY);
913 printf("Failed map_fd_rx update sockmap %i '%i:%i'\n",
917 err = bpf_map_update_elem(map_fd_tx, &i, &sfd[i], BPF_ANY);
919 printf("Failed map_fd_tx update sockmap %i '%i:%i'\n",
925 /* Test map delete elem and remove send/recv sockets */
926 for (i = 2; i < 4; i++) {
927 err = bpf_map_delete_elem(map_fd_rx, &i);
929 printf("Failed delete sockmap rx %i '%i:%i'\n",
933 err = bpf_map_delete_elem(map_fd_tx, &i);
935 printf("Failed delete sockmap tx %i '%i:%i'\n",
941 /* Put sfd[2] (sending fd below) into msg map to test sendmsg bpf */
943 err = bpf_map_update_elem(map_fd_msg, &i, &sfd[2], BPF_ANY);
945 printf("Failed map_fd_msg update sockmap %i\n", err);
949 /* Test map send/recv */
950 for (i = 0; i < 2; i++) {
953 sc = send(sfd[2], buf, 20, 0);
955 printf("Failed sockmap send\n");
963 s = select(sfd[3] + 1, &w, NULL, NULL, &to);
965 perror("Failed sockmap select()");
968 printf("Failed sockmap unexpected timeout\n");
972 if (!FD_ISSET(sfd[3], &w)) {
973 printf("Failed sockmap select/recv\n");
977 rc = recv(sfd[3], buf, sizeof(buf), 0);
979 printf("Failed sockmap recv\n");
984 /* Negative null entry lookup from datapath should be dropped */
987 sc = send(sfd[2], buf, 20, 0);
989 printf("Failed sockmap send\n");
993 /* Push fd into same slot */
995 err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_NOEXIST);
997 printf("Failed allowed sockmap dup slot BPF_NOEXIST\n");
1001 err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_ANY);
1003 printf("Failed sockmap update new slot BPF_ANY\n");
1007 err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_EXIST);
1009 printf("Failed sockmap update new slot BPF_EXIST\n");
1013 /* Delete the elems without programs */
1014 for (i = 2; i < 6; i++) {
1015 err = bpf_map_delete_elem(fd, &i);
1017 printf("Failed delete sockmap %i '%i:%i'\n",
1022 /* Test having multiple maps open and set with programs on same fds */
1023 err = bpf_prog_attach(parse_prog, fd,
1024 BPF_SK_SKB_STREAM_PARSER, 0);
1026 printf("Failed fd bpf parse prog attach\n");
1029 err = bpf_prog_attach(verdict_prog, fd,
1030 BPF_SK_SKB_STREAM_VERDICT, 0);
1032 printf("Failed fd bpf verdict prog attach\n");
1036 for (i = 4; i < 6; i++) {
1037 err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_ANY);
1039 printf("Failed allowed duplicate programs in update ANY sockmap %i '%i:%i'\n",
1043 err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_NOEXIST);
1045 printf("Failed allowed duplicate program in update NOEXIST sockmap %i '%i:%i'\n",
1049 err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_EXIST);
1051 printf("Failed allowed duplicate program in update EXIST sockmap %i '%i:%i'\n",
1057 /* Test tasks number of forked operations */
1058 for (i = 0; i < tasks; i++) {
1061 for (i = 0; i < 6; i++) {
1062 bpf_map_delete_elem(map_fd_tx, &i);
1063 bpf_map_delete_elem(map_fd_rx, &i);
1064 bpf_map_update_elem(map_fd_tx, &i,
1066 bpf_map_update_elem(map_fd_rx, &i,
1070 } else if (pid[i] == -1) {
1071 printf("Couldn't spawn #%d process!\n", i);
1076 for (i = 0; i < tasks; i++) {
1079 assert(waitpid(pid[i], &status, 0) == pid[i]);
1080 assert(status == 0);
1083 err = bpf_prog_detach(map_fd_rx, __MAX_BPF_ATTACH_TYPE);
1085 printf("Detached an invalid prog type.\n");
1089 err = bpf_prog_detach(map_fd_rx, BPF_SK_SKB_STREAM_PARSER);
1091 printf("Failed parser prog detach\n");
1095 err = bpf_prog_detach(map_fd_rx, BPF_SK_SKB_STREAM_VERDICT);
1097 printf("Failed parser prog detach\n");
1101 /* Test map close sockets and empty maps */
1102 for (i = 0; i < 6; i++) {
1103 bpf_map_delete_elem(map_fd_tx, &i);
1104 bpf_map_delete_elem(map_fd_rx, &i);
1109 bpf_object__close(obj);
1112 for (i = 0; i < 6; i++)
1114 printf("Failed to create sockmap '%i:%s'!\n", i, strerror(errno));
1117 for (i = 0; i < 6; i++) {
1119 bpf_map_delete_elem(map_fd_tx, &i);
1121 bpf_map_delete_elem(map_fd_rx, &i);
1128 #define MAPINMAP_PROG "./test_map_in_map.o"
1129 static void test_map_in_map(void)
1131 struct bpf_program *prog;
1132 struct bpf_object *obj;
1133 struct bpf_map *map;
1134 int mim_fd, fd, err;
1137 obj = bpf_object__open(MAPINMAP_PROG);
1139 fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(int), sizeof(int),
1142 printf("Failed to create hashmap '%s'!\n", strerror(errno));
1146 map = bpf_object__find_map_by_name(obj, "mim_array");
1148 printf("Failed to load array of maps from test prog\n");
1149 goto out_map_in_map;
1151 err = bpf_map__set_inner_map_fd(map, fd);
1153 printf("Failed to set inner_map_fd for array of maps\n");
1154 goto out_map_in_map;
1157 map = bpf_object__find_map_by_name(obj, "mim_hash");
1159 printf("Failed to load hash of maps from test prog\n");
1160 goto out_map_in_map;
1162 err = bpf_map__set_inner_map_fd(map, fd);
1164 printf("Failed to set inner_map_fd for hash of maps\n");
1165 goto out_map_in_map;
1168 bpf_object__for_each_program(prog, obj) {
1169 bpf_program__set_xdp(prog);
1171 bpf_object__load(obj);
1173 map = bpf_object__find_map_by_name(obj, "mim_array");
1175 printf("Failed to load array of maps from test prog\n");
1176 goto out_map_in_map;
1178 mim_fd = bpf_map__fd(map);
1180 printf("Failed to get descriptor for array of maps\n");
1181 goto out_map_in_map;
1184 err = bpf_map_update_elem(mim_fd, &pos, &fd, 0);
1186 printf("Failed to update array of maps\n");
1187 goto out_map_in_map;
1190 map = bpf_object__find_map_by_name(obj, "mim_hash");
1192 printf("Failed to load hash of maps from test prog\n");
1193 goto out_map_in_map;
1195 mim_fd = bpf_map__fd(map);
1197 printf("Failed to get descriptor for hash of maps\n");
1198 goto out_map_in_map;
1201 err = bpf_map_update_elem(mim_fd, &pos, &fd, 0);
1203 printf("Failed to update hash of maps\n");
1204 goto out_map_in_map;
1208 bpf_object__close(obj);
1216 #define MAP_SIZE (32 * 1024)
1218 static void test_map_large(void)
1227 fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value),
1228 MAP_SIZE, map_flags);
1230 printf("Failed to create large map '%s'!\n", strerror(errno));
1234 for (i = 0; i < MAP_SIZE; i++) {
1235 key = (struct bigkey) { .c = i };
1238 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == 0);
1242 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == -1 &&
1245 /* Iterate through all elements. */
1246 assert(bpf_map_get_next_key(fd, NULL, &key) == 0);
1248 for (i = 0; i < MAP_SIZE; i++)
1249 assert(bpf_map_get_next_key(fd, &key, &key) == 0);
1250 assert(bpf_map_get_next_key(fd, &key, &key) == -1 && errno == ENOENT);
1253 assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && value == 0);
1255 assert(bpf_map_lookup_elem(fd, &key, &value) == -1 && errno == ENOENT);
1260 #define run_parallel(N, FN, DATA) \
1261 printf("Fork %d tasks to '" #FN "'\n", N); \
1262 __run_parallel(N, FN, DATA)
1264 static void __run_parallel(int tasks, void (*fn)(int task, void *data),
1270 for (i = 0; i < tasks; i++) {
1275 } else if (pid[i] == -1) {
1276 printf("Couldn't spawn #%d process!\n", i);
1281 for (i = 0; i < tasks; i++) {
1284 assert(waitpid(pid[i], &status, 0) == pid[i]);
1285 assert(status == 0);
1289 static void test_map_stress(void)
1291 run_parallel(100, test_hashmap, NULL);
1292 run_parallel(100, test_hashmap_percpu, NULL);
1293 run_parallel(100, test_hashmap_sizes, NULL);
1294 run_parallel(100, test_hashmap_walk, NULL);
1296 run_parallel(100, test_arraymap, NULL);
1297 run_parallel(100, test_arraymap_percpu, NULL);
1305 static void test_update_delete(int fn, void *data)
1307 int do_update = ((int *)data)[1];
1308 int fd = ((int *)data)[0];
1311 for (i = fn; i < MAP_SIZE; i += TASKS) {
1315 assert(bpf_map_update_elem(fd, &key, &value,
1317 assert(bpf_map_update_elem(fd, &key, &value,
1320 assert(bpf_map_delete_elem(fd, &key) == 0);
1325 static void test_map_parallel(void)
1327 int i, fd, key = 0, value = 0;
1330 fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value),
1331 MAP_SIZE, map_flags);
1333 printf("Failed to create map for parallel test '%s'!\n",
1338 /* Use the same fd in children to add elements to this map:
1339 * child_0 adds key=0, key=1024, key=2048, ...
1340 * child_1 adds key=1, key=1025, key=2049, ...
1341 * child_1023 adds key=1023, ...
1344 data[1] = DO_UPDATE;
1345 run_parallel(TASKS, test_update_delete, data);
1347 /* Check that key=0 is already there. */
1348 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == -1 &&
1351 /* Check that all elements were inserted. */
1352 assert(bpf_map_get_next_key(fd, NULL, &key) == 0);
1354 for (i = 0; i < MAP_SIZE; i++)
1355 assert(bpf_map_get_next_key(fd, &key, &key) == 0);
1356 assert(bpf_map_get_next_key(fd, &key, &key) == -1 && errno == ENOENT);
1358 /* Another check for all elements */
1359 for (i = 0; i < MAP_SIZE; i++) {
1360 key = MAP_SIZE - i - 1;
1362 assert(bpf_map_lookup_elem(fd, &key, &value) == 0 &&
1366 /* Now let's delete all elemenets in parallel. */
1367 data[1] = DO_DELETE;
1368 run_parallel(TASKS, test_update_delete, data);
1370 /* Nothing should be left. */
1372 assert(bpf_map_get_next_key(fd, NULL, &key) == -1 && errno == ENOENT);
1373 assert(bpf_map_get_next_key(fd, &key, &key) == -1 && errno == ENOENT);
1376 static void test_map_rdonly(void)
1378 int fd, key = 0, value = 0;
1380 fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value),
1381 MAP_SIZE, map_flags | BPF_F_RDONLY);
1383 printf("Failed to create map for read only test '%s'!\n",
1390 /* Insert key=1 element. */
1391 assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == -1 &&
1394 /* Check that key=2 is not found. */
1395 assert(bpf_map_lookup_elem(fd, &key, &value) == -1 && errno == ENOENT);
1396 assert(bpf_map_get_next_key(fd, &key, &value) == -1 && errno == ENOENT);
1399 static void test_map_wronly(void)
1401 int fd, key = 0, value = 0;
1403 fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value),
1404 MAP_SIZE, map_flags | BPF_F_WRONLY);
1406 printf("Failed to create map for read only test '%s'!\n",
1413 /* Insert key=1 element. */
1414 assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == 0);
1416 /* Check that key=2 is not found. */
1417 assert(bpf_map_lookup_elem(fd, &key, &value) == -1 && errno == EPERM);
1418 assert(bpf_map_get_next_key(fd, &key, &value) == -1 && errno == EPERM);
1421 static void prepare_reuseport_grp(int type, int map_fd,
1422 __s64 *fds64, __u64 *sk_cookies,
1425 socklen_t optlen, addrlen;
1426 struct sockaddr_in6 s6;
1427 const __u32 index0 = 0;
1428 const int optval = 1;
1434 s6.sin6_family = AF_INET6;
1435 s6.sin6_addr = in6addr_any;
1437 addrlen = sizeof(s6);
1438 optlen = sizeof(sk_cookie);
1440 for (i = 0; i < n; i++) {
1441 fd64 = socket(AF_INET6, type, 0);
1442 CHECK(fd64 == -1, "socket()",
1443 "sock_type:%d fd64:%lld errno:%d\n",
1446 err = setsockopt(fd64, SOL_SOCKET, SO_REUSEPORT,
1447 &optval, sizeof(optval));
1448 CHECK(err == -1, "setsockopt(SO_REUSEPORT)",
1449 "err:%d errno:%d\n", err, errno);
1451 /* reuseport_array does not allow unbound sk */
1452 err = bpf_map_update_elem(map_fd, &index0, &fd64,
1454 CHECK(err != -1 || errno != EINVAL,
1455 "reuseport array update unbound sk",
1456 "sock_type:%d err:%d errno:%d\n",
1459 err = bind(fd64, (struct sockaddr *)&s6, sizeof(s6));
1460 CHECK(err == -1, "bind()",
1461 "sock_type:%d err:%d errno:%d\n", type, err, errno);
1464 err = getsockname(fd64, (struct sockaddr *)&s6,
1466 CHECK(err == -1, "getsockname()",
1467 "sock_type:%d err:%d errno:%d\n",
1471 err = getsockopt(fd64, SOL_SOCKET, SO_COOKIE, &sk_cookie,
1473 CHECK(err == -1, "getsockopt(SO_COOKIE)",
1474 "sock_type:%d err:%d errno:%d\n", type, err, errno);
1476 if (type == SOCK_STREAM) {
1478 * reuseport_array does not allow
1479 * non-listening tcp sk.
1481 err = bpf_map_update_elem(map_fd, &index0, &fd64,
1483 CHECK(err != -1 || errno != EINVAL,
1484 "reuseport array update non-listening sk",
1485 "sock_type:%d err:%d errno:%d\n",
1487 err = listen(fd64, 0);
1488 CHECK(err == -1, "listen()",
1489 "sock_type:%d, err:%d errno:%d\n",
1494 sk_cookies[i] = sk_cookie;
1498 static void test_reuseport_array(void)
1500 #define REUSEPORT_FD_IDX(err, last) ({ (err) ? last : !last; })
1502 const __u32 array_size = 4, index0 = 0, index3 = 3;
1503 int types[2] = { SOCK_STREAM, SOCK_DGRAM }, type;
1504 __u64 grpa_cookies[2], sk_cookie, map_cookie;
1505 __s64 grpa_fds64[2] = { -1, -1 }, fd64 = -1;
1506 const __u32 bad_index = array_size;
1507 int map_fd, err, t, f;
1511 map_fd = bpf_create_map(BPF_MAP_TYPE_REUSEPORT_SOCKARRAY,
1512 sizeof(__u32), sizeof(__u64), array_size, 0);
1513 CHECK(map_fd == -1, "reuseport array create",
1514 "map_fd:%d, errno:%d\n", map_fd, errno);
1516 /* Test lookup/update/delete with invalid index */
1517 err = bpf_map_delete_elem(map_fd, &bad_index);
1518 CHECK(err != -1 || errno != E2BIG, "reuseport array del >=max_entries",
1519 "err:%d errno:%d\n", err, errno);
1521 err = bpf_map_update_elem(map_fd, &bad_index, &fd64, BPF_ANY);
1522 CHECK(err != -1 || errno != E2BIG,
1523 "reuseport array update >=max_entries",
1524 "err:%d errno:%d\n", err, errno);
1526 err = bpf_map_lookup_elem(map_fd, &bad_index, &map_cookie);
1527 CHECK(err != -1 || errno != ENOENT,
1528 "reuseport array update >=max_entries",
1529 "err:%d errno:%d\n", err, errno);
1531 /* Test lookup/delete non existence elem */
1532 err = bpf_map_lookup_elem(map_fd, &index3, &map_cookie);
1533 CHECK(err != -1 || errno != ENOENT,
1534 "reuseport array lookup not-exist elem",
1535 "err:%d errno:%d\n", err, errno);
1536 err = bpf_map_delete_elem(map_fd, &index3);
1537 CHECK(err != -1 || errno != ENOENT,
1538 "reuseport array del not-exist elem",
1539 "err:%d errno:%d\n", err, errno);
1541 for (t = 0; t < ARRAY_SIZE(types); t++) {
1544 prepare_reuseport_grp(type, map_fd, grpa_fds64,
1545 grpa_cookies, ARRAY_SIZE(grpa_fds64));
1547 /* Test BPF_* update flags */
1548 /* BPF_EXIST failure case */
1549 err = bpf_map_update_elem(map_fd, &index3, &grpa_fds64[fds_idx],
1551 CHECK(err != -1 || errno != ENOENT,
1552 "reuseport array update empty elem BPF_EXIST",
1553 "sock_type:%d err:%d errno:%d\n",
1555 fds_idx = REUSEPORT_FD_IDX(err, fds_idx);
1557 /* BPF_NOEXIST success case */
1558 err = bpf_map_update_elem(map_fd, &index3, &grpa_fds64[fds_idx],
1561 "reuseport array update empty elem BPF_NOEXIST",
1562 "sock_type:%d err:%d errno:%d\n",
1564 fds_idx = REUSEPORT_FD_IDX(err, fds_idx);
1566 /* BPF_EXIST success case. */
1567 err = bpf_map_update_elem(map_fd, &index3, &grpa_fds64[fds_idx],
1570 "reuseport array update same elem BPF_EXIST",
1571 "sock_type:%d err:%d errno:%d\n", type, err, errno);
1572 fds_idx = REUSEPORT_FD_IDX(err, fds_idx);
1574 /* BPF_NOEXIST failure case */
1575 err = bpf_map_update_elem(map_fd, &index3, &grpa_fds64[fds_idx],
1577 CHECK(err != -1 || errno != EEXIST,
1578 "reuseport array update non-empty elem BPF_NOEXIST",
1579 "sock_type:%d err:%d errno:%d\n",
1581 fds_idx = REUSEPORT_FD_IDX(err, fds_idx);
1583 /* BPF_ANY case (always succeed) */
1584 err = bpf_map_update_elem(map_fd, &index3, &grpa_fds64[fds_idx],
1587 "reuseport array update same sk with BPF_ANY",
1588 "sock_type:%d err:%d errno:%d\n", type, err, errno);
1590 fd64 = grpa_fds64[fds_idx];
1591 sk_cookie = grpa_cookies[fds_idx];
1593 /* The same sk cannot be added to reuseport_array twice */
1594 err = bpf_map_update_elem(map_fd, &index3, &fd64, BPF_ANY);
1595 CHECK(err != -1 || errno != EBUSY,
1596 "reuseport array update same sk with same index",
1597 "sock_type:%d err:%d errno:%d\n",
1600 err = bpf_map_update_elem(map_fd, &index0, &fd64, BPF_ANY);
1601 CHECK(err != -1 || errno != EBUSY,
1602 "reuseport array update same sk with different index",
1603 "sock_type:%d err:%d errno:%d\n",
1606 /* Test delete elem */
1607 err = bpf_map_delete_elem(map_fd, &index3);
1608 CHECK(err == -1, "reuseport array delete sk",
1609 "sock_type:%d err:%d errno:%d\n",
1612 /* Add it back with BPF_NOEXIST */
1613 err = bpf_map_update_elem(map_fd, &index3, &fd64, BPF_NOEXIST);
1615 "reuseport array re-add with BPF_NOEXIST after del",
1616 "sock_type:%d err:%d errno:%d\n", type, err, errno);
1619 err = bpf_map_lookup_elem(map_fd, &index3, &map_cookie);
1620 CHECK(err == -1 || sk_cookie != map_cookie,
1621 "reuseport array lookup re-added sk",
1622 "sock_type:%d err:%d errno:%d sk_cookie:0x%llx map_cookie:0x%llxn",
1623 type, err, errno, sk_cookie, map_cookie);
1625 /* Test elem removed by close() */
1626 for (f = 0; f < ARRAY_SIZE(grpa_fds64); f++)
1627 close(grpa_fds64[f]);
1628 err = bpf_map_lookup_elem(map_fd, &index3, &map_cookie);
1629 CHECK(err != -1 || errno != ENOENT,
1630 "reuseport array lookup after close()",
1631 "sock_type:%d err:%d errno:%d\n",
1636 fd64 = socket(AF_INET6, SOCK_RAW, IPPROTO_UDP);
1637 CHECK(fd64 == -1, "socket(SOCK_RAW)", "err:%d errno:%d\n",
1639 err = bpf_map_update_elem(map_fd, &index3, &fd64, BPF_NOEXIST);
1640 CHECK(err != -1 || errno != ENOTSUPP, "reuseport array update SOCK_RAW",
1641 "err:%d errno:%d\n", err, errno);
1644 /* Close the 64 bit value map */
1647 /* Test 32 bit fd */
1648 map_fd = bpf_create_map(BPF_MAP_TYPE_REUSEPORT_SOCKARRAY,
1649 sizeof(__u32), sizeof(__u32), array_size, 0);
1650 CHECK(map_fd == -1, "reuseport array create",
1651 "map_fd:%d, errno:%d\n", map_fd, errno);
1652 prepare_reuseport_grp(SOCK_STREAM, map_fd, &fd64, &sk_cookie, 1);
1654 err = bpf_map_update_elem(map_fd, &index3, &fd, BPF_NOEXIST);
1655 CHECK(err == -1, "reuseport array update 32 bit fd",
1656 "err:%d errno:%d\n", err, errno);
1657 err = bpf_map_lookup_elem(map_fd, &index3, &map_cookie);
1658 CHECK(err != -1 || errno != ENOSPC,
1659 "reuseport array lookup 32 bit fd",
1660 "err:%d errno:%d\n", err, errno);
1665 static void run_all_tests(void)
1667 test_hashmap(0, NULL);
1668 test_hashmap_percpu(0, NULL);
1669 test_hashmap_walk(0, NULL);
1670 test_hashmap_zero_seed();
1672 test_arraymap(0, NULL);
1673 test_arraymap_percpu(0, NULL);
1675 test_arraymap_percpu_many_keys();
1677 test_devmap(0, NULL);
1678 test_sockmap(0, NULL);
1681 test_map_parallel();
1687 test_reuseport_array();
1689 test_queuemap(0, NULL);
1690 test_stackmap(0, NULL);
1702 map_flags = BPF_F_NO_PREALLOC;
1705 printf("test_maps: OK\n");