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.
20 #include <sys/socket.h>
21 #include <netinet/in.h>
22 #include <linux/bpf.h>
25 #include <bpf/libbpf.h>
28 #include "bpf_rlimit.h"
36 #define CHECK(condition, tag, format...) ({ \
37 int __ret = !!(condition); \
39 printf("%s(%d):FAIL:%s ", __func__, __LINE__, tag); \
45 static void test_hashmap(int task, void *data)
47 long long key, next_key, first_key, value;
50 fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value),
53 printf("Failed to create hashmap '%s'!\n", strerror(errno));
59 /* Insert key=1 element. */
60 assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == 0);
63 /* BPF_NOEXIST means add new element if it doesn't exist. */
64 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == -1 &&
65 /* key=1 already exists. */
68 /* -1 is an invalid flag. */
69 assert(bpf_map_update_elem(fd, &key, &value, -1) == -1 &&
72 /* Check that key=1 can be found. */
73 assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && value == 1234);
76 /* Check that key=2 is not found. */
77 assert(bpf_map_lookup_elem(fd, &key, &value) == -1 && errno == ENOENT);
79 /* BPF_EXIST means update existing element. */
80 assert(bpf_map_update_elem(fd, &key, &value, BPF_EXIST) == -1 &&
81 /* key=2 is not there. */
84 /* Insert key=2 element. */
85 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == 0);
87 /* key=1 and key=2 were inserted, check that key=0 cannot be
88 * inserted due to max_entries limit.
91 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == -1 &&
94 /* Update existing element, though the map is full. */
96 assert(bpf_map_update_elem(fd, &key, &value, BPF_EXIST) == 0);
98 assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == 0);
100 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == -1 &&
103 /* Check that key = 0 doesn't exist. */
105 assert(bpf_map_delete_elem(fd, &key) == -1 && errno == ENOENT);
107 /* Iterate over two elements. */
108 assert(bpf_map_get_next_key(fd, NULL, &first_key) == 0 &&
109 (first_key == 1 || first_key == 2));
110 assert(bpf_map_get_next_key(fd, &key, &next_key) == 0 &&
111 (next_key == first_key));
112 assert(bpf_map_get_next_key(fd, &next_key, &next_key) == 0 &&
113 (next_key == 1 || next_key == 2) &&
114 (next_key != first_key));
115 assert(bpf_map_get_next_key(fd, &next_key, &next_key) == -1 &&
118 /* Delete both elements. */
120 assert(bpf_map_delete_elem(fd, &key) == 0);
122 assert(bpf_map_delete_elem(fd, &key) == 0);
123 assert(bpf_map_delete_elem(fd, &key) == -1 && errno == ENOENT);
126 /* Check that map is empty. */
127 assert(bpf_map_get_next_key(fd, NULL, &next_key) == -1 &&
129 assert(bpf_map_get_next_key(fd, &key, &next_key) == -1 &&
135 static void test_hashmap_sizes(int task, void *data)
139 for (i = 1; i <= 512; i <<= 1)
140 for (j = 1; j <= 1 << 18; j <<= 1) {
141 fd = bpf_create_map(BPF_MAP_TYPE_HASH, i, j,
146 printf("Failed to create hashmap key=%d value=%d '%s'\n",
147 i, j, strerror(errno));
151 usleep(10); /* give kernel time to destroy */
155 static void test_hashmap_percpu(int task, void *data)
157 unsigned int nr_cpus = bpf_num_possible_cpus();
158 BPF_DECLARE_PERCPU(long, value);
159 long long key, next_key, first_key;
160 int expected_key_mask = 0;
163 fd = bpf_create_map(BPF_MAP_TYPE_PERCPU_HASH, sizeof(key),
164 sizeof(bpf_percpu(value, 0)), 2, map_flags);
166 printf("Failed to create hashmap '%s'!\n", strerror(errno));
170 for (i = 0; i < nr_cpus; i++)
171 bpf_percpu(value, i) = i + 100;
174 /* Insert key=1 element. */
175 assert(!(expected_key_mask & key));
176 assert(bpf_map_update_elem(fd, &key, value, BPF_ANY) == 0);
177 expected_key_mask |= key;
179 /* BPF_NOEXIST means add new element if it doesn't exist. */
180 assert(bpf_map_update_elem(fd, &key, value, BPF_NOEXIST) == -1 &&
181 /* key=1 already exists. */
184 /* -1 is an invalid flag. */
185 assert(bpf_map_update_elem(fd, &key, value, -1) == -1 &&
188 /* Check that key=1 can be found. Value could be 0 if the lookup
189 * was run from a different CPU.
191 bpf_percpu(value, 0) = 1;
192 assert(bpf_map_lookup_elem(fd, &key, value) == 0 &&
193 bpf_percpu(value, 0) == 100);
196 /* Check that key=2 is not found. */
197 assert(bpf_map_lookup_elem(fd, &key, value) == -1 && errno == ENOENT);
199 /* BPF_EXIST means update existing element. */
200 assert(bpf_map_update_elem(fd, &key, value, BPF_EXIST) == -1 &&
201 /* key=2 is not there. */
204 /* Insert key=2 element. */
205 assert(!(expected_key_mask & key));
206 assert(bpf_map_update_elem(fd, &key, value, BPF_NOEXIST) == 0);
207 expected_key_mask |= key;
209 /* key=1 and key=2 were inserted, check that key=0 cannot be
210 * inserted due to max_entries limit.
213 assert(bpf_map_update_elem(fd, &key, value, BPF_NOEXIST) == -1 &&
216 /* Check that key = 0 doesn't exist. */
217 assert(bpf_map_delete_elem(fd, &key) == -1 && errno == ENOENT);
219 /* Iterate over two elements. */
220 assert(bpf_map_get_next_key(fd, NULL, &first_key) == 0 &&
221 ((expected_key_mask & first_key) == first_key));
222 while (!bpf_map_get_next_key(fd, &key, &next_key)) {
224 assert(next_key == first_key);
227 assert((expected_key_mask & next_key) == next_key);
228 expected_key_mask &= ~next_key;
230 assert(bpf_map_lookup_elem(fd, &next_key, value) == 0);
232 for (i = 0; i < nr_cpus; i++)
233 assert(bpf_percpu(value, i) == i + 100);
237 assert(errno == ENOENT);
239 /* Update with BPF_EXIST. */
241 assert(bpf_map_update_elem(fd, &key, value, BPF_EXIST) == 0);
243 /* Delete both elements. */
245 assert(bpf_map_delete_elem(fd, &key) == 0);
247 assert(bpf_map_delete_elem(fd, &key) == 0);
248 assert(bpf_map_delete_elem(fd, &key) == -1 && errno == ENOENT);
251 /* Check that map is empty. */
252 assert(bpf_map_get_next_key(fd, NULL, &next_key) == -1 &&
254 assert(bpf_map_get_next_key(fd, &key, &next_key) == -1 &&
260 static void test_hashmap_walk(int task, void *data)
262 int fd, i, max_entries = 1000;
263 long long key, value, next_key;
264 bool next_key_valid = true;
266 fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value),
267 max_entries, map_flags);
269 printf("Failed to create hashmap '%s'!\n", strerror(errno));
273 for (i = 0; i < max_entries; i++) {
274 key = i; value = key;
275 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == 0);
278 for (i = 0; bpf_map_get_next_key(fd, !i ? NULL : &key,
279 &next_key) == 0; i++) {
281 assert(bpf_map_lookup_elem(fd, &key, &value) == 0);
284 assert(i == max_entries);
286 assert(bpf_map_get_next_key(fd, NULL, &key) == 0);
287 for (i = 0; next_key_valid; i++) {
288 next_key_valid = bpf_map_get_next_key(fd, &key, &next_key) == 0;
289 assert(bpf_map_lookup_elem(fd, &key, &value) == 0);
291 assert(bpf_map_update_elem(fd, &key, &value, BPF_EXIST) == 0);
295 assert(i == max_entries);
297 for (i = 0; bpf_map_get_next_key(fd, !i ? NULL : &key,
298 &next_key) == 0; i++) {
300 assert(bpf_map_lookup_elem(fd, &key, &value) == 0);
301 assert(value - 1 == key);
304 assert(i == max_entries);
308 static void test_arraymap(int task, void *data)
310 int key, next_key, fd;
313 fd = bpf_create_map(BPF_MAP_TYPE_ARRAY, sizeof(key), sizeof(value),
316 printf("Failed to create arraymap '%s'!\n", strerror(errno));
322 /* Insert key=1 element. */
323 assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == 0);
326 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == -1 &&
329 /* Check that key=1 can be found. */
330 assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && value == 1234);
333 /* Check that key=0 is also found and zero initialized. */
334 assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && value == 0);
336 /* key=0 and key=1 were inserted, check that key=2 cannot be inserted
337 * due to max_entries limit.
340 assert(bpf_map_update_elem(fd, &key, &value, BPF_EXIST) == -1 &&
343 /* Check that key = 2 doesn't exist. */
344 assert(bpf_map_lookup_elem(fd, &key, &value) == -1 && errno == ENOENT);
346 /* Iterate over two elements. */
347 assert(bpf_map_get_next_key(fd, NULL, &next_key) == 0 &&
349 assert(bpf_map_get_next_key(fd, &key, &next_key) == 0 &&
351 assert(bpf_map_get_next_key(fd, &next_key, &next_key) == 0 &&
353 assert(bpf_map_get_next_key(fd, &next_key, &next_key) == -1 &&
356 /* Delete shouldn't succeed. */
358 assert(bpf_map_delete_elem(fd, &key) == -1 && errno == EINVAL);
363 static void test_arraymap_percpu(int task, void *data)
365 unsigned int nr_cpus = bpf_num_possible_cpus();
366 BPF_DECLARE_PERCPU(long, values);
367 int key, next_key, fd, i;
369 fd = bpf_create_map(BPF_MAP_TYPE_PERCPU_ARRAY, sizeof(key),
370 sizeof(bpf_percpu(values, 0)), 2, 0);
372 printf("Failed to create arraymap '%s'!\n", strerror(errno));
376 for (i = 0; i < nr_cpus; i++)
377 bpf_percpu(values, i) = i + 100;
380 /* Insert key=1 element. */
381 assert(bpf_map_update_elem(fd, &key, values, BPF_ANY) == 0);
383 bpf_percpu(values, 0) = 0;
384 assert(bpf_map_update_elem(fd, &key, values, BPF_NOEXIST) == -1 &&
387 /* Check that key=1 can be found. */
388 assert(bpf_map_lookup_elem(fd, &key, values) == 0 &&
389 bpf_percpu(values, 0) == 100);
392 /* Check that key=0 is also found and zero initialized. */
393 assert(bpf_map_lookup_elem(fd, &key, values) == 0 &&
394 bpf_percpu(values, 0) == 0 &&
395 bpf_percpu(values, nr_cpus - 1) == 0);
397 /* Check that key=2 cannot be inserted due to max_entries limit. */
399 assert(bpf_map_update_elem(fd, &key, values, BPF_EXIST) == -1 &&
402 /* Check that key = 2 doesn't exist. */
403 assert(bpf_map_lookup_elem(fd, &key, values) == -1 && errno == ENOENT);
405 /* Iterate over two elements. */
406 assert(bpf_map_get_next_key(fd, NULL, &next_key) == 0 &&
408 assert(bpf_map_get_next_key(fd, &key, &next_key) == 0 &&
410 assert(bpf_map_get_next_key(fd, &next_key, &next_key) == 0 &&
412 assert(bpf_map_get_next_key(fd, &next_key, &next_key) == -1 &&
415 /* Delete shouldn't succeed. */
417 assert(bpf_map_delete_elem(fd, &key) == -1 && errno == EINVAL);
422 static void test_arraymap_percpu_many_keys(void)
424 unsigned int nr_cpus = bpf_num_possible_cpus();
425 BPF_DECLARE_PERCPU(long, values);
426 /* nr_keys is not too large otherwise the test stresses percpu
427 * allocator more than anything else
429 unsigned int nr_keys = 2000;
432 fd = bpf_create_map(BPF_MAP_TYPE_PERCPU_ARRAY, sizeof(key),
433 sizeof(bpf_percpu(values, 0)), nr_keys, 0);
435 printf("Failed to create per-cpu arraymap '%s'!\n",
440 for (i = 0; i < nr_cpus; i++)
441 bpf_percpu(values, i) = i + 10;
443 for (key = 0; key < nr_keys; key++)
444 assert(bpf_map_update_elem(fd, &key, values, BPF_ANY) == 0);
446 for (key = 0; key < nr_keys; key++) {
447 for (i = 0; i < nr_cpus; i++)
448 bpf_percpu(values, i) = 0;
450 assert(bpf_map_lookup_elem(fd, &key, values) == 0);
452 for (i = 0; i < nr_cpus; i++)
453 assert(bpf_percpu(values, i) == i + 10);
459 static void test_devmap(int task, void *data)
464 fd = bpf_create_map(BPF_MAP_TYPE_DEVMAP, sizeof(key), sizeof(value),
467 printf("Failed to create arraymap '%s'!\n", strerror(errno));
474 #include <sys/socket.h>
475 #include <sys/ioctl.h>
476 #include <arpa/inet.h>
477 #include <sys/select.h>
478 #include <linux/err.h>
479 #define SOCKMAP_PARSE_PROG "./sockmap_parse_prog.o"
480 #define SOCKMAP_VERDICT_PROG "./sockmap_verdict_prog.o"
481 #define SOCKMAP_TCP_MSG_PROG "./sockmap_tcp_msg_prog.o"
482 static void test_sockmap(int tasks, void *data)
484 struct bpf_map *bpf_map_rx, *bpf_map_tx, *bpf_map_msg, *bpf_map_break;
485 int map_fd_msg = 0, map_fd_rx = 0, map_fd_tx = 0, map_fd_break;
486 int ports[] = {50200, 50201, 50202, 50204};
487 int err, i, fd, udp, sfd[6] = {0xdeadbeef};
488 u8 buf[20] = {0x0, 0x5, 0x3, 0x2, 0x1, 0x0};
489 int parse_prog, verdict_prog, msg_prog;
490 struct sockaddr_in addr;
491 int one = 1, s, sc, rc;
492 struct bpf_object *obj;
498 /* Create some sockets to use with sockmap */
499 for (i = 0; i < 2; i++) {
500 sfd[i] = socket(AF_INET, SOCK_STREAM, 0);
503 err = setsockopt(sfd[i], SOL_SOCKET, SO_REUSEADDR,
504 (char *)&one, sizeof(one));
506 printf("failed to setsockopt\n");
509 err = ioctl(sfd[i], FIONBIO, (char *)&one);
511 printf("failed to ioctl\n");
514 memset(&addr, 0, sizeof(struct sockaddr_in));
515 addr.sin_family = AF_INET;
516 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
517 addr.sin_port = htons(ports[i]);
518 err = bind(sfd[i], (struct sockaddr *)&addr, sizeof(addr));
520 printf("failed to bind: err %i: %i:%i\n",
524 err = listen(sfd[i], 32);
526 printf("failed to listen\n");
531 for (i = 2; i < 4; i++) {
532 sfd[i] = socket(AF_INET, SOCK_STREAM, 0);
535 err = setsockopt(sfd[i], SOL_SOCKET, SO_REUSEADDR,
536 (char *)&one, sizeof(one));
538 printf("set sock opt\n");
541 memset(&addr, 0, sizeof(struct sockaddr_in));
542 addr.sin_family = AF_INET;
543 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
544 addr.sin_port = htons(ports[i - 2]);
545 err = connect(sfd[i], (struct sockaddr *)&addr, sizeof(addr));
547 printf("failed to connect\n");
553 for (i = 4; i < 6; i++) {
554 sfd[i] = accept(sfd[i - 4], NULL, NULL);
556 printf("accept failed\n");
561 /* Test sockmap with connected sockets */
562 fd = bpf_create_map(BPF_MAP_TYPE_SOCKMAP,
563 sizeof(key), sizeof(value),
566 printf("Failed to create sockmap %i\n", fd);
570 /* Test update with unsupported UDP socket */
571 udp = socket(AF_INET, SOCK_DGRAM, 0);
573 err = bpf_map_update_elem(fd, &i, &udp, BPF_ANY);
575 printf("Failed socket SOCK_DGRAM allowed '%i:%i'\n",
580 /* Test update without programs */
581 for (i = 0; i < 6; i++) {
582 err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_ANY);
584 printf("Allowed update sockmap '%i:%i' not in ESTABLISHED\n",
587 } else if (i >= 2 && err) {
588 printf("Failed noprog update sockmap '%i:%i'\n",
594 /* Test attaching/detaching bad fds */
595 err = bpf_prog_attach(-1, fd, BPF_SK_SKB_STREAM_PARSER, 0);
597 printf("Failed invalid parser prog attach\n");
601 err = bpf_prog_attach(-1, fd, BPF_SK_SKB_STREAM_VERDICT, 0);
603 printf("Failed invalid verdict prog attach\n");
607 err = bpf_prog_attach(-1, fd, BPF_SK_MSG_VERDICT, 0);
609 printf("Failed invalid msg verdict prog attach\n");
613 err = bpf_prog_attach(-1, fd, __MAX_BPF_ATTACH_TYPE, 0);
615 printf("Failed unknown prog attach\n");
619 err = bpf_prog_detach(fd, BPF_SK_SKB_STREAM_PARSER);
621 printf("Failed empty parser prog detach\n");
625 err = bpf_prog_detach(fd, BPF_SK_SKB_STREAM_VERDICT);
627 printf("Failed empty verdict prog detach\n");
631 err = bpf_prog_detach(fd, BPF_SK_MSG_VERDICT);
633 printf("Failed empty msg verdict prog detach\n");
637 err = bpf_prog_detach(fd, __MAX_BPF_ATTACH_TYPE);
639 printf("Detach invalid prog successful\n");
643 /* Load SK_SKB program and Attach */
644 err = bpf_prog_load(SOCKMAP_PARSE_PROG,
645 BPF_PROG_TYPE_SK_SKB, &obj, &parse_prog);
647 printf("Failed to load SK_SKB parse prog\n");
651 err = bpf_prog_load(SOCKMAP_TCP_MSG_PROG,
652 BPF_PROG_TYPE_SK_MSG, &obj, &msg_prog);
654 printf("Failed to load SK_SKB msg prog\n");
658 err = bpf_prog_load(SOCKMAP_VERDICT_PROG,
659 BPF_PROG_TYPE_SK_SKB, &obj, &verdict_prog);
661 printf("Failed to load SK_SKB verdict prog\n");
665 bpf_map_rx = bpf_object__find_map_by_name(obj, "sock_map_rx");
666 if (IS_ERR(bpf_map_rx)) {
667 printf("Failed to load map rx from verdict prog\n");
671 map_fd_rx = bpf_map__fd(bpf_map_rx);
673 printf("Failed to get map rx fd\n");
677 bpf_map_tx = bpf_object__find_map_by_name(obj, "sock_map_tx");
678 if (IS_ERR(bpf_map_tx)) {
679 printf("Failed to load map tx from verdict prog\n");
683 map_fd_tx = bpf_map__fd(bpf_map_tx);
685 printf("Failed to get map tx fd\n");
689 bpf_map_msg = bpf_object__find_map_by_name(obj, "sock_map_msg");
690 if (IS_ERR(bpf_map_msg)) {
691 printf("Failed to load map msg from msg_verdict prog\n");
695 map_fd_msg = bpf_map__fd(bpf_map_msg);
696 if (map_fd_msg < 0) {
697 printf("Failed to get map msg fd\n");
701 bpf_map_break = bpf_object__find_map_by_name(obj, "sock_map_break");
702 if (IS_ERR(bpf_map_break)) {
703 printf("Failed to load map tx from verdict prog\n");
707 map_fd_break = bpf_map__fd(bpf_map_break);
708 if (map_fd_break < 0) {
709 printf("Failed to get map tx fd\n");
713 err = bpf_prog_attach(parse_prog, map_fd_break,
714 BPF_SK_SKB_STREAM_PARSER, 0);
716 printf("Allowed attaching SK_SKB program to invalid map\n");
720 err = bpf_prog_attach(parse_prog, map_fd_rx,
721 BPF_SK_SKB_STREAM_PARSER, 0);
723 printf("Failed stream parser bpf prog attach\n");
727 err = bpf_prog_attach(verdict_prog, map_fd_rx,
728 BPF_SK_SKB_STREAM_VERDICT, 0);
730 printf("Failed stream verdict bpf prog attach\n");
734 err = bpf_prog_attach(msg_prog, map_fd_msg, BPF_SK_MSG_VERDICT, 0);
736 printf("Failed msg verdict bpf prog attach\n");
740 err = bpf_prog_attach(verdict_prog, map_fd_rx,
741 __MAX_BPF_ATTACH_TYPE, 0);
743 printf("Attached unknown bpf prog\n");
747 /* Test map update elem afterwards fd lives in fd and map_fd */
748 for (i = 2; i < 6; i++) {
749 err = bpf_map_update_elem(map_fd_rx, &i, &sfd[i], BPF_ANY);
751 printf("Failed map_fd_rx update sockmap %i '%i:%i'\n",
755 err = bpf_map_update_elem(map_fd_tx, &i, &sfd[i], BPF_ANY);
757 printf("Failed map_fd_tx update sockmap %i '%i:%i'\n",
763 /* Test map delete elem and remove send/recv sockets */
764 for (i = 2; i < 4; i++) {
765 err = bpf_map_delete_elem(map_fd_rx, &i);
767 printf("Failed delete sockmap rx %i '%i:%i'\n",
771 err = bpf_map_delete_elem(map_fd_tx, &i);
773 printf("Failed delete sockmap tx %i '%i:%i'\n",
779 /* Put sfd[2] (sending fd below) into msg map to test sendmsg bpf */
781 err = bpf_map_update_elem(map_fd_msg, &i, &sfd[2], BPF_ANY);
783 printf("Failed map_fd_msg update sockmap %i\n", err);
787 /* Test map send/recv */
788 for (i = 0; i < 2; i++) {
791 sc = send(sfd[2], buf, 20, 0);
793 printf("Failed sockmap send\n");
801 s = select(sfd[3] + 1, &w, NULL, NULL, &to);
803 perror("Failed sockmap select()");
806 printf("Failed sockmap unexpected timeout\n");
810 if (!FD_ISSET(sfd[3], &w)) {
811 printf("Failed sockmap select/recv\n");
815 rc = recv(sfd[3], buf, sizeof(buf), 0);
817 printf("Failed sockmap recv\n");
822 /* Negative null entry lookup from datapath should be dropped */
825 sc = send(sfd[2], buf, 20, 0);
827 printf("Failed sockmap send\n");
831 /* Push fd into same slot */
833 err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_NOEXIST);
835 printf("Failed allowed sockmap dup slot BPF_NOEXIST\n");
839 err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_ANY);
841 printf("Failed sockmap update new slot BPF_ANY\n");
845 err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_EXIST);
847 printf("Failed sockmap update new slot BPF_EXIST\n");
851 /* Delete the elems without programs */
852 for (i = 2; i < 6; i++) {
853 err = bpf_map_delete_elem(fd, &i);
855 printf("Failed delete sockmap %i '%i:%i'\n",
860 /* Test having multiple maps open and set with programs on same fds */
861 err = bpf_prog_attach(parse_prog, fd,
862 BPF_SK_SKB_STREAM_PARSER, 0);
864 printf("Failed fd bpf parse prog attach\n");
867 err = bpf_prog_attach(verdict_prog, fd,
868 BPF_SK_SKB_STREAM_VERDICT, 0);
870 printf("Failed fd bpf verdict prog attach\n");
874 for (i = 4; i < 6; i++) {
875 err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_ANY);
877 printf("Failed allowed duplicate programs in update ANY sockmap %i '%i:%i'\n",
881 err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_NOEXIST);
883 printf("Failed allowed duplicate program in update NOEXIST sockmap %i '%i:%i'\n",
887 err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_EXIST);
889 printf("Failed allowed duplicate program in update EXIST sockmap %i '%i:%i'\n",
895 /* Test tasks number of forked operations */
896 for (i = 0; i < tasks; i++) {
899 for (i = 0; i < 6; i++) {
900 bpf_map_delete_elem(map_fd_tx, &i);
901 bpf_map_delete_elem(map_fd_rx, &i);
902 bpf_map_update_elem(map_fd_tx, &i,
904 bpf_map_update_elem(map_fd_rx, &i,
908 } else if (pid[i] == -1) {
909 printf("Couldn't spawn #%d process!\n", i);
914 for (i = 0; i < tasks; i++) {
917 assert(waitpid(pid[i], &status, 0) == pid[i]);
921 err = bpf_prog_detach(map_fd_rx, __MAX_BPF_ATTACH_TYPE);
923 printf("Detached an invalid prog type.\n");
927 err = bpf_prog_detach(map_fd_rx, BPF_SK_SKB_STREAM_PARSER);
929 printf("Failed parser prog detach\n");
933 err = bpf_prog_detach(map_fd_rx, BPF_SK_SKB_STREAM_VERDICT);
935 printf("Failed parser prog detach\n");
939 /* Test map close sockets and empty maps */
940 for (i = 0; i < 6; i++) {
941 bpf_map_delete_elem(map_fd_tx, &i);
942 bpf_map_delete_elem(map_fd_rx, &i);
947 bpf_object__close(obj);
950 for (i = 0; i < 6; i++)
952 printf("Failed to create sockmap '%i:%s'!\n", i, strerror(errno));
955 for (i = 0; i < 6; i++) {
957 bpf_map_delete_elem(map_fd_tx, &i);
959 bpf_map_delete_elem(map_fd_rx, &i);
966 #define MAP_SIZE (32 * 1024)
968 static void test_map_large(void)
977 fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value),
978 MAP_SIZE, map_flags);
980 printf("Failed to create large map '%s'!\n", strerror(errno));
984 for (i = 0; i < MAP_SIZE; i++) {
985 key = (struct bigkey) { .c = i };
988 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == 0);
992 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == -1 &&
995 /* Iterate through all elements. */
996 assert(bpf_map_get_next_key(fd, NULL, &key) == 0);
998 for (i = 0; i < MAP_SIZE; i++)
999 assert(bpf_map_get_next_key(fd, &key, &key) == 0);
1000 assert(bpf_map_get_next_key(fd, &key, &key) == -1 && errno == ENOENT);
1003 assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && value == 0);
1005 assert(bpf_map_lookup_elem(fd, &key, &value) == -1 && errno == ENOENT);
1010 #define run_parallel(N, FN, DATA) \
1011 printf("Fork %d tasks to '" #FN "'\n", N); \
1012 __run_parallel(N, FN, DATA)
1014 static void __run_parallel(int tasks, void (*fn)(int task, void *data),
1022 for (i = 0; i < tasks; i++) {
1027 } else if (pid[i] == -1) {
1028 printf("Couldn't spawn #%d process!\n", i);
1033 for (i = 0; i < tasks; i++) {
1036 assert(waitpid(pid[i], &status, 0) == pid[i]);
1037 assert(status == 0);
1041 static void test_map_stress(void)
1043 run_parallel(100, test_hashmap, NULL);
1044 run_parallel(100, test_hashmap_percpu, NULL);
1045 run_parallel(100, test_hashmap_sizes, NULL);
1046 run_parallel(100, test_hashmap_walk, NULL);
1048 run_parallel(100, test_arraymap, NULL);
1049 run_parallel(100, test_arraymap_percpu, NULL);
1057 static void test_update_delete(int fn, void *data)
1059 int do_update = ((int *)data)[1];
1060 int fd = ((int *)data)[0];
1063 for (i = fn; i < MAP_SIZE; i += TASKS) {
1067 assert(bpf_map_update_elem(fd, &key, &value,
1069 assert(bpf_map_update_elem(fd, &key, &value,
1072 assert(bpf_map_delete_elem(fd, &key) == 0);
1077 static void test_map_parallel(void)
1079 int i, fd, key = 0, value = 0;
1082 fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value),
1083 MAP_SIZE, map_flags);
1085 printf("Failed to create map for parallel test '%s'!\n",
1090 /* Use the same fd in children to add elements to this map:
1091 * child_0 adds key=0, key=1024, key=2048, ...
1092 * child_1 adds key=1, key=1025, key=2049, ...
1093 * child_1023 adds key=1023, ...
1096 data[1] = DO_UPDATE;
1097 run_parallel(TASKS, test_update_delete, data);
1099 /* Check that key=0 is already there. */
1100 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == -1 &&
1103 /* Check that all elements were inserted. */
1104 assert(bpf_map_get_next_key(fd, NULL, &key) == 0);
1106 for (i = 0; i < MAP_SIZE; i++)
1107 assert(bpf_map_get_next_key(fd, &key, &key) == 0);
1108 assert(bpf_map_get_next_key(fd, &key, &key) == -1 && errno == ENOENT);
1110 /* Another check for all elements */
1111 for (i = 0; i < MAP_SIZE; i++) {
1112 key = MAP_SIZE - i - 1;
1114 assert(bpf_map_lookup_elem(fd, &key, &value) == 0 &&
1118 /* Now let's delete all elemenets in parallel. */
1119 data[1] = DO_DELETE;
1120 run_parallel(TASKS, test_update_delete, data);
1122 /* Nothing should be left. */
1124 assert(bpf_map_get_next_key(fd, NULL, &key) == -1 && errno == ENOENT);
1125 assert(bpf_map_get_next_key(fd, &key, &key) == -1 && errno == ENOENT);
1128 static void test_map_rdonly(void)
1130 int fd, key = 0, value = 0;
1132 fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value),
1133 MAP_SIZE, map_flags | BPF_F_RDONLY);
1135 printf("Failed to create map for read only test '%s'!\n",
1142 /* Insert key=1 element. */
1143 assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == -1 &&
1146 /* Check that key=2 is not found. */
1147 assert(bpf_map_lookup_elem(fd, &key, &value) == -1 && errno == ENOENT);
1148 assert(bpf_map_get_next_key(fd, &key, &value) == -1 && errno == ENOENT);
1151 static void test_map_wronly(void)
1153 int fd, key = 0, value = 0;
1155 fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value),
1156 MAP_SIZE, map_flags | BPF_F_WRONLY);
1158 printf("Failed to create map for read only test '%s'!\n",
1165 /* Insert key=1 element. */
1166 assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == 0);
1168 /* Check that key=2 is not found. */
1169 assert(bpf_map_lookup_elem(fd, &key, &value) == -1 && errno == EPERM);
1170 assert(bpf_map_get_next_key(fd, &key, &value) == -1 && errno == EPERM);
1173 static void prepare_reuseport_grp(int type, int map_fd,
1174 __s64 *fds64, __u64 *sk_cookies,
1177 socklen_t optlen, addrlen;
1178 struct sockaddr_in6 s6;
1179 const __u32 index0 = 0;
1180 const int optval = 1;
1186 s6.sin6_family = AF_INET6;
1187 s6.sin6_addr = in6addr_any;
1189 addrlen = sizeof(s6);
1190 optlen = sizeof(sk_cookie);
1192 for (i = 0; i < n; i++) {
1193 fd64 = socket(AF_INET6, type, 0);
1194 CHECK(fd64 == -1, "socket()",
1195 "sock_type:%d fd64:%lld errno:%d\n",
1198 err = setsockopt(fd64, SOL_SOCKET, SO_REUSEPORT,
1199 &optval, sizeof(optval));
1200 CHECK(err == -1, "setsockopt(SO_REUSEPORT)",
1201 "err:%d errno:%d\n", err, errno);
1203 /* reuseport_array does not allow unbound sk */
1204 err = bpf_map_update_elem(map_fd, &index0, &fd64,
1206 CHECK(err != -1 || errno != EINVAL,
1207 "reuseport array update unbound sk",
1208 "sock_type:%d err:%d errno:%d\n",
1211 err = bind(fd64, (struct sockaddr *)&s6, sizeof(s6));
1212 CHECK(err == -1, "bind()",
1213 "sock_type:%d err:%d errno:%d\n", type, err, errno);
1216 err = getsockname(fd64, (struct sockaddr *)&s6,
1218 CHECK(err == -1, "getsockname()",
1219 "sock_type:%d err:%d errno:%d\n",
1223 err = getsockopt(fd64, SOL_SOCKET, SO_COOKIE, &sk_cookie,
1225 CHECK(err == -1, "getsockopt(SO_COOKIE)",
1226 "sock_type:%d err:%d errno:%d\n", type, err, errno);
1228 if (type == SOCK_STREAM) {
1230 * reuseport_array does not allow
1231 * non-listening tcp sk.
1233 err = bpf_map_update_elem(map_fd, &index0, &fd64,
1235 CHECK(err != -1 || errno != EINVAL,
1236 "reuseport array update non-listening sk",
1237 "sock_type:%d err:%d errno:%d\n",
1239 err = listen(fd64, 0);
1240 CHECK(err == -1, "listen()",
1241 "sock_type:%d, err:%d errno:%d\n",
1246 sk_cookies[i] = sk_cookie;
1250 static void test_reuseport_array(void)
1252 #define REUSEPORT_FD_IDX(err, last) ({ (err) ? last : !last; })
1254 const __u32 array_size = 4, index0 = 0, index3 = 3;
1255 int types[2] = { SOCK_STREAM, SOCK_DGRAM }, type;
1256 __u64 grpa_cookies[2], sk_cookie, map_cookie;
1257 __s64 grpa_fds64[2] = { -1, -1 }, fd64 = -1;
1258 const __u32 bad_index = array_size;
1259 int map_fd, err, t, f;
1263 map_fd = bpf_create_map(BPF_MAP_TYPE_REUSEPORT_SOCKARRAY,
1264 sizeof(__u32), sizeof(__u64), array_size, 0);
1265 CHECK(map_fd == -1, "reuseport array create",
1266 "map_fd:%d, errno:%d\n", map_fd, errno);
1268 /* Test lookup/update/delete with invalid index */
1269 err = bpf_map_delete_elem(map_fd, &bad_index);
1270 CHECK(err != -1 || errno != E2BIG, "reuseport array del >=max_entries",
1271 "err:%d errno:%d\n", err, errno);
1273 err = bpf_map_update_elem(map_fd, &bad_index, &fd64, BPF_ANY);
1274 CHECK(err != -1 || errno != E2BIG,
1275 "reuseport array update >=max_entries",
1276 "err:%d errno:%d\n", err, errno);
1278 err = bpf_map_lookup_elem(map_fd, &bad_index, &map_cookie);
1279 CHECK(err != -1 || errno != ENOENT,
1280 "reuseport array update >=max_entries",
1281 "err:%d errno:%d\n", err, errno);
1283 /* Test lookup/delete non existence elem */
1284 err = bpf_map_lookup_elem(map_fd, &index3, &map_cookie);
1285 CHECK(err != -1 || errno != ENOENT,
1286 "reuseport array lookup not-exist elem",
1287 "err:%d errno:%d\n", err, errno);
1288 err = bpf_map_delete_elem(map_fd, &index3);
1289 CHECK(err != -1 || errno != ENOENT,
1290 "reuseport array del not-exist elem",
1291 "err:%d errno:%d\n", err, errno);
1293 for (t = 0; t < ARRAY_SIZE(types); t++) {
1296 prepare_reuseport_grp(type, map_fd, grpa_fds64,
1297 grpa_cookies, ARRAY_SIZE(grpa_fds64));
1299 /* Test BPF_* update flags */
1300 /* BPF_EXIST failure case */
1301 err = bpf_map_update_elem(map_fd, &index3, &grpa_fds64[fds_idx],
1303 CHECK(err != -1 || errno != ENOENT,
1304 "reuseport array update empty elem BPF_EXIST",
1305 "sock_type:%d err:%d errno:%d\n",
1307 fds_idx = REUSEPORT_FD_IDX(err, fds_idx);
1309 /* BPF_NOEXIST success case */
1310 err = bpf_map_update_elem(map_fd, &index3, &grpa_fds64[fds_idx],
1313 "reuseport array update empty elem BPF_NOEXIST",
1314 "sock_type:%d err:%d errno:%d\n",
1316 fds_idx = REUSEPORT_FD_IDX(err, fds_idx);
1318 /* BPF_EXIST success case. */
1319 err = bpf_map_update_elem(map_fd, &index3, &grpa_fds64[fds_idx],
1322 "reuseport array update same elem BPF_EXIST",
1323 "sock_type:%d err:%d errno:%d\n", type, err, errno);
1324 fds_idx = REUSEPORT_FD_IDX(err, fds_idx);
1326 /* BPF_NOEXIST failure case */
1327 err = bpf_map_update_elem(map_fd, &index3, &grpa_fds64[fds_idx],
1329 CHECK(err != -1 || errno != EEXIST,
1330 "reuseport array update non-empty elem BPF_NOEXIST",
1331 "sock_type:%d err:%d errno:%d\n",
1333 fds_idx = REUSEPORT_FD_IDX(err, fds_idx);
1335 /* BPF_ANY case (always succeed) */
1336 err = bpf_map_update_elem(map_fd, &index3, &grpa_fds64[fds_idx],
1339 "reuseport array update same sk with BPF_ANY",
1340 "sock_type:%d err:%d errno:%d\n", type, err, errno);
1342 fd64 = grpa_fds64[fds_idx];
1343 sk_cookie = grpa_cookies[fds_idx];
1345 /* The same sk cannot be added to reuseport_array twice */
1346 err = bpf_map_update_elem(map_fd, &index3, &fd64, BPF_ANY);
1347 CHECK(err != -1 || errno != EBUSY,
1348 "reuseport array update same sk with same index",
1349 "sock_type:%d err:%d errno:%d\n",
1352 err = bpf_map_update_elem(map_fd, &index0, &fd64, BPF_ANY);
1353 CHECK(err != -1 || errno != EBUSY,
1354 "reuseport array update same sk with different index",
1355 "sock_type:%d err:%d errno:%d\n",
1358 /* Test delete elem */
1359 err = bpf_map_delete_elem(map_fd, &index3);
1360 CHECK(err == -1, "reuseport array delete sk",
1361 "sock_type:%d err:%d errno:%d\n",
1364 /* Add it back with BPF_NOEXIST */
1365 err = bpf_map_update_elem(map_fd, &index3, &fd64, BPF_NOEXIST);
1367 "reuseport array re-add with BPF_NOEXIST after del",
1368 "sock_type:%d err:%d errno:%d\n", type, err, errno);
1371 err = bpf_map_lookup_elem(map_fd, &index3, &map_cookie);
1372 CHECK(err == -1 || sk_cookie != map_cookie,
1373 "reuseport array lookup re-added sk",
1374 "sock_type:%d err:%d errno:%d sk_cookie:0x%llx map_cookie:0x%llxn",
1375 type, err, errno, sk_cookie, map_cookie);
1377 /* Test elem removed by close() */
1378 for (f = 0; f < ARRAY_SIZE(grpa_fds64); f++)
1379 close(grpa_fds64[f]);
1380 err = bpf_map_lookup_elem(map_fd, &index3, &map_cookie);
1381 CHECK(err != -1 || errno != ENOENT,
1382 "reuseport array lookup after close()",
1383 "sock_type:%d err:%d errno:%d\n",
1388 fd64 = socket(AF_INET6, SOCK_RAW, IPPROTO_UDP);
1389 CHECK(fd64 == -1, "socket(SOCK_RAW)", "err:%d errno:%d\n",
1391 err = bpf_map_update_elem(map_fd, &index3, &fd64, BPF_NOEXIST);
1392 CHECK(err != -1 || errno != ENOTSUPP, "reuseport array update SOCK_RAW",
1393 "err:%d errno:%d\n", err, errno);
1396 /* Close the 64 bit value map */
1399 /* Test 32 bit fd */
1400 map_fd = bpf_create_map(BPF_MAP_TYPE_REUSEPORT_SOCKARRAY,
1401 sizeof(__u32), sizeof(__u32), array_size, 0);
1402 CHECK(map_fd == -1, "reuseport array create",
1403 "map_fd:%d, errno:%d\n", map_fd, errno);
1404 prepare_reuseport_grp(SOCK_STREAM, map_fd, &fd64, &sk_cookie, 1);
1406 err = bpf_map_update_elem(map_fd, &index3, &fd, BPF_NOEXIST);
1407 CHECK(err == -1, "reuseport array update 32 bit fd",
1408 "err:%d errno:%d\n", err, errno);
1409 err = bpf_map_lookup_elem(map_fd, &index3, &map_cookie);
1410 CHECK(err != -1 || errno != ENOSPC,
1411 "reuseport array lookup 32 bit fd",
1412 "err:%d errno:%d\n", err, errno);
1417 static void run_all_tests(void)
1419 test_hashmap(0, NULL);
1420 test_hashmap_percpu(0, NULL);
1421 test_hashmap_walk(0, NULL);
1423 test_arraymap(0, NULL);
1424 test_arraymap_percpu(0, NULL);
1426 test_arraymap_percpu_many_keys();
1428 test_devmap(0, NULL);
1429 test_sockmap(0, NULL);
1432 test_map_parallel();
1438 test_reuseport_array();
1446 map_flags = BPF_F_NO_PREALLOC;
1449 printf("test_maps: OK\n");