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/resource.h>
22 #include <linux/bpf.h>
25 #include <bpf/libbpf.h>
30 static void test_hashmap(int task, void *data)
32 long long key, next_key, first_key, value;
35 fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value),
38 printf("Failed to create hashmap '%s'!\n", strerror(errno));
44 /* Insert key=1 element. */
45 assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == 0);
48 /* BPF_NOEXIST means add new element if it doesn't exist. */
49 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == -1 &&
50 /* key=1 already exists. */
53 /* -1 is an invalid flag. */
54 assert(bpf_map_update_elem(fd, &key, &value, -1) == -1 &&
57 /* Check that key=1 can be found. */
58 assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && value == 1234);
61 /* Check that key=2 is not found. */
62 assert(bpf_map_lookup_elem(fd, &key, &value) == -1 && errno == ENOENT);
64 /* BPF_EXIST means update existing element. */
65 assert(bpf_map_update_elem(fd, &key, &value, BPF_EXIST) == -1 &&
66 /* key=2 is not there. */
69 /* Insert key=2 element. */
70 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == 0);
72 /* key=1 and key=2 were inserted, check that key=0 cannot be
73 * inserted due to max_entries limit.
76 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == -1 &&
79 /* Update existing element, though the map is full. */
81 assert(bpf_map_update_elem(fd, &key, &value, BPF_EXIST) == 0);
83 assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == 0);
85 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == -1 &&
88 /* Check that key = 0 doesn't exist. */
90 assert(bpf_map_delete_elem(fd, &key) == -1 && errno == ENOENT);
92 /* Iterate over two elements. */
93 assert(bpf_map_get_next_key(fd, NULL, &first_key) == 0 &&
94 (first_key == 1 || first_key == 2));
95 assert(bpf_map_get_next_key(fd, &key, &next_key) == 0 &&
96 (next_key == first_key));
97 assert(bpf_map_get_next_key(fd, &next_key, &next_key) == 0 &&
98 (next_key == 1 || next_key == 2) &&
99 (next_key != first_key));
100 assert(bpf_map_get_next_key(fd, &next_key, &next_key) == -1 &&
103 /* Delete both elements. */
105 assert(bpf_map_delete_elem(fd, &key) == 0);
107 assert(bpf_map_delete_elem(fd, &key) == 0);
108 assert(bpf_map_delete_elem(fd, &key) == -1 && errno == ENOENT);
111 /* Check that map is empty. */
112 assert(bpf_map_get_next_key(fd, NULL, &next_key) == -1 &&
114 assert(bpf_map_get_next_key(fd, &key, &next_key) == -1 &&
120 static void test_hashmap_sizes(int task, void *data)
124 for (i = 1; i <= 512; i <<= 1)
125 for (j = 1; j <= 1 << 18; j <<= 1) {
126 fd = bpf_create_map(BPF_MAP_TYPE_HASH, i, j,
129 printf("Failed to create hashmap key=%d value=%d '%s'\n",
130 i, j, strerror(errno));
134 usleep(10); /* give kernel time to destroy */
138 static void test_hashmap_percpu(int task, void *data)
140 unsigned int nr_cpus = bpf_num_possible_cpus();
141 BPF_DECLARE_PERCPU(long, value);
142 long long key, next_key, first_key;
143 int expected_key_mask = 0;
146 fd = bpf_create_map(BPF_MAP_TYPE_PERCPU_HASH, sizeof(key),
147 sizeof(bpf_percpu(value, 0)), 2, map_flags);
149 printf("Failed to create hashmap '%s'!\n", strerror(errno));
153 for (i = 0; i < nr_cpus; i++)
154 bpf_percpu(value, i) = i + 100;
157 /* Insert key=1 element. */
158 assert(!(expected_key_mask & key));
159 assert(bpf_map_update_elem(fd, &key, value, BPF_ANY) == 0);
160 expected_key_mask |= key;
162 /* BPF_NOEXIST means add new element if it doesn't exist. */
163 assert(bpf_map_update_elem(fd, &key, value, BPF_NOEXIST) == -1 &&
164 /* key=1 already exists. */
167 /* -1 is an invalid flag. */
168 assert(bpf_map_update_elem(fd, &key, value, -1) == -1 &&
171 /* Check that key=1 can be found. Value could be 0 if the lookup
172 * was run from a different CPU.
174 bpf_percpu(value, 0) = 1;
175 assert(bpf_map_lookup_elem(fd, &key, value) == 0 &&
176 bpf_percpu(value, 0) == 100);
179 /* Check that key=2 is not found. */
180 assert(bpf_map_lookup_elem(fd, &key, value) == -1 && errno == ENOENT);
182 /* BPF_EXIST means update existing element. */
183 assert(bpf_map_update_elem(fd, &key, value, BPF_EXIST) == -1 &&
184 /* key=2 is not there. */
187 /* Insert key=2 element. */
188 assert(!(expected_key_mask & key));
189 assert(bpf_map_update_elem(fd, &key, value, BPF_NOEXIST) == 0);
190 expected_key_mask |= key;
192 /* key=1 and key=2 were inserted, check that key=0 cannot be
193 * inserted due to max_entries limit.
196 assert(bpf_map_update_elem(fd, &key, value, BPF_NOEXIST) == -1 &&
199 /* Check that key = 0 doesn't exist. */
200 assert(bpf_map_delete_elem(fd, &key) == -1 && errno == ENOENT);
202 /* Iterate over two elements. */
203 assert(bpf_map_get_next_key(fd, NULL, &first_key) == 0 &&
204 ((expected_key_mask & first_key) == first_key));
205 while (!bpf_map_get_next_key(fd, &key, &next_key)) {
207 assert(next_key == first_key);
210 assert((expected_key_mask & next_key) == next_key);
211 expected_key_mask &= ~next_key;
213 assert(bpf_map_lookup_elem(fd, &next_key, value) == 0);
215 for (i = 0; i < nr_cpus; i++)
216 assert(bpf_percpu(value, i) == i + 100);
220 assert(errno == ENOENT);
222 /* Update with BPF_EXIST. */
224 assert(bpf_map_update_elem(fd, &key, value, BPF_EXIST) == 0);
226 /* Delete both elements. */
228 assert(bpf_map_delete_elem(fd, &key) == 0);
230 assert(bpf_map_delete_elem(fd, &key) == 0);
231 assert(bpf_map_delete_elem(fd, &key) == -1 && errno == ENOENT);
234 /* Check that map is empty. */
235 assert(bpf_map_get_next_key(fd, NULL, &next_key) == -1 &&
237 assert(bpf_map_get_next_key(fd, &key, &next_key) == -1 &&
243 static void test_hashmap_walk(int task, void *data)
245 int fd, i, max_entries = 1000;
246 long long key, value, next_key;
247 bool next_key_valid = true;
249 fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value),
250 max_entries, map_flags);
252 printf("Failed to create hashmap '%s'!\n", strerror(errno));
256 for (i = 0; i < max_entries; i++) {
257 key = i; value = key;
258 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == 0);
261 for (i = 0; bpf_map_get_next_key(fd, !i ? NULL : &key,
262 &next_key) == 0; i++) {
264 assert(bpf_map_lookup_elem(fd, &key, &value) == 0);
267 assert(i == max_entries);
269 assert(bpf_map_get_next_key(fd, NULL, &key) == 0);
270 for (i = 0; next_key_valid; i++) {
271 next_key_valid = bpf_map_get_next_key(fd, &key, &next_key) == 0;
272 assert(bpf_map_lookup_elem(fd, &key, &value) == 0);
274 assert(bpf_map_update_elem(fd, &key, &value, BPF_EXIST) == 0);
278 assert(i == max_entries);
280 for (i = 0; bpf_map_get_next_key(fd, !i ? NULL : &key,
281 &next_key) == 0; i++) {
283 assert(bpf_map_lookup_elem(fd, &key, &value) == 0);
284 assert(value - 1 == key);
287 assert(i == max_entries);
291 static void test_arraymap(int task, void *data)
293 int key, next_key, fd;
296 fd = bpf_create_map(BPF_MAP_TYPE_ARRAY, sizeof(key), sizeof(value),
299 printf("Failed to create arraymap '%s'!\n", strerror(errno));
305 /* Insert key=1 element. */
306 assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == 0);
309 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == -1 &&
312 /* Check that key=1 can be found. */
313 assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && value == 1234);
316 /* Check that key=0 is also found and zero initialized. */
317 assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && value == 0);
319 /* key=0 and key=1 were inserted, check that key=2 cannot be inserted
320 * due to max_entries limit.
323 assert(bpf_map_update_elem(fd, &key, &value, BPF_EXIST) == -1 &&
326 /* Check that key = 2 doesn't exist. */
327 assert(bpf_map_lookup_elem(fd, &key, &value) == -1 && errno == ENOENT);
329 /* Iterate over two elements. */
330 assert(bpf_map_get_next_key(fd, NULL, &next_key) == 0 &&
332 assert(bpf_map_get_next_key(fd, &key, &next_key) == 0 &&
334 assert(bpf_map_get_next_key(fd, &next_key, &next_key) == 0 &&
336 assert(bpf_map_get_next_key(fd, &next_key, &next_key) == -1 &&
339 /* Delete shouldn't succeed. */
341 assert(bpf_map_delete_elem(fd, &key) == -1 && errno == EINVAL);
346 static void test_arraymap_percpu(int task, void *data)
348 unsigned int nr_cpus = bpf_num_possible_cpus();
349 BPF_DECLARE_PERCPU(long, values);
350 int key, next_key, fd, i;
352 fd = bpf_create_map(BPF_MAP_TYPE_PERCPU_ARRAY, sizeof(key),
353 sizeof(bpf_percpu(values, 0)), 2, 0);
355 printf("Failed to create arraymap '%s'!\n", strerror(errno));
359 for (i = 0; i < nr_cpus; i++)
360 bpf_percpu(values, i) = i + 100;
363 /* Insert key=1 element. */
364 assert(bpf_map_update_elem(fd, &key, values, BPF_ANY) == 0);
366 bpf_percpu(values, 0) = 0;
367 assert(bpf_map_update_elem(fd, &key, values, BPF_NOEXIST) == -1 &&
370 /* Check that key=1 can be found. */
371 assert(bpf_map_lookup_elem(fd, &key, values) == 0 &&
372 bpf_percpu(values, 0) == 100);
375 /* Check that key=0 is also found and zero initialized. */
376 assert(bpf_map_lookup_elem(fd, &key, values) == 0 &&
377 bpf_percpu(values, 0) == 0 &&
378 bpf_percpu(values, nr_cpus - 1) == 0);
380 /* Check that key=2 cannot be inserted due to max_entries limit. */
382 assert(bpf_map_update_elem(fd, &key, values, BPF_EXIST) == -1 &&
385 /* Check that key = 2 doesn't exist. */
386 assert(bpf_map_lookup_elem(fd, &key, values) == -1 && errno == ENOENT);
388 /* Iterate over two elements. */
389 assert(bpf_map_get_next_key(fd, NULL, &next_key) == 0 &&
391 assert(bpf_map_get_next_key(fd, &key, &next_key) == 0 &&
393 assert(bpf_map_get_next_key(fd, &next_key, &next_key) == 0 &&
395 assert(bpf_map_get_next_key(fd, &next_key, &next_key) == -1 &&
398 /* Delete shouldn't succeed. */
400 assert(bpf_map_delete_elem(fd, &key) == -1 && errno == EINVAL);
405 static void test_arraymap_percpu_many_keys(void)
407 unsigned int nr_cpus = bpf_num_possible_cpus();
408 BPF_DECLARE_PERCPU(long, values);
409 /* nr_keys is not too large otherwise the test stresses percpu
410 * allocator more than anything else
412 unsigned int nr_keys = 2000;
415 fd = bpf_create_map(BPF_MAP_TYPE_PERCPU_ARRAY, sizeof(key),
416 sizeof(bpf_percpu(values, 0)), nr_keys, 0);
418 printf("Failed to create per-cpu arraymap '%s'!\n",
423 for (i = 0; i < nr_cpus; i++)
424 bpf_percpu(values, i) = i + 10;
426 for (key = 0; key < nr_keys; key++)
427 assert(bpf_map_update_elem(fd, &key, values, BPF_ANY) == 0);
429 for (key = 0; key < nr_keys; key++) {
430 for (i = 0; i < nr_cpus; i++)
431 bpf_percpu(values, i) = 0;
433 assert(bpf_map_lookup_elem(fd, &key, values) == 0);
435 for (i = 0; i < nr_cpus; i++)
436 assert(bpf_percpu(values, i) == i + 10);
442 static void test_devmap(int task, void *data)
447 fd = bpf_create_map(BPF_MAP_TYPE_DEVMAP, sizeof(key), sizeof(value),
450 printf("Failed to create arraymap '%s'!\n", strerror(errno));
457 #include <sys/socket.h>
458 #include <sys/ioctl.h>
459 #include <arpa/inet.h>
460 #include <sys/select.h>
461 #include <linux/err.h>
462 #define SOCKMAP_PARSE_PROG "./sockmap_parse_prog.o"
463 #define SOCKMAP_VERDICT_PROG "./sockmap_verdict_prog.o"
464 static void test_sockmap(int tasks, void *data)
466 int one = 1, map_fd_rx = 0, map_fd_tx = 0, map_fd_break, s, sc, rc;
467 struct bpf_map *bpf_map_rx, *bpf_map_tx, *bpf_map_break;
468 int ports[] = {50200, 50201, 50202, 50204};
469 int err, i, fd, udp, sfd[6] = {0xdeadbeef};
470 u8 buf[20] = {0x0, 0x5, 0x3, 0x2, 0x1, 0x0};
471 int parse_prog, verdict_prog;
472 struct sockaddr_in addr;
473 struct bpf_object *obj;
479 /* Create some sockets to use with sockmap */
480 for (i = 0; i < 2; i++) {
481 sfd[i] = socket(AF_INET, SOCK_STREAM, 0);
484 err = setsockopt(sfd[i], SOL_SOCKET, SO_REUSEADDR,
485 (char *)&one, sizeof(one));
487 printf("failed to setsockopt\n");
490 err = ioctl(sfd[i], FIONBIO, (char *)&one);
492 printf("failed to ioctl\n");
495 memset(&addr, 0, sizeof(struct sockaddr_in));
496 addr.sin_family = AF_INET;
497 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
498 addr.sin_port = htons(ports[i]);
499 err = bind(sfd[i], (struct sockaddr *)&addr, sizeof(addr));
501 printf("failed to bind: err %i: %i:%i\n",
505 err = listen(sfd[i], 32);
507 printf("failed to listen\n");
512 for (i = 2; i < 4; i++) {
513 sfd[i] = socket(AF_INET, SOCK_STREAM, 0);
516 err = setsockopt(sfd[i], SOL_SOCKET, SO_REUSEADDR,
517 (char *)&one, sizeof(one));
519 printf("set sock opt\n");
522 memset(&addr, 0, sizeof(struct sockaddr_in));
523 addr.sin_family = AF_INET;
524 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
525 addr.sin_port = htons(ports[i - 2]);
526 err = connect(sfd[i], (struct sockaddr *)&addr, sizeof(addr));
528 printf("failed to connect\n");
534 for (i = 4; i < 6; i++) {
535 sfd[i] = accept(sfd[i - 4], NULL, NULL);
537 printf("accept failed\n");
542 /* Test sockmap with connected sockets */
543 fd = bpf_create_map(BPF_MAP_TYPE_SOCKMAP,
544 sizeof(key), sizeof(value),
547 printf("Failed to create sockmap %i\n", fd);
551 /* Test update with unsupported UDP socket */
552 udp = socket(AF_INET, SOCK_DGRAM, 0);
554 err = bpf_map_update_elem(fd, &i, &udp, BPF_ANY);
556 printf("Failed socket SOCK_DGRAM allowed '%i:%i'\n",
561 /* Test update without programs */
562 for (i = 0; i < 6; i++) {
563 err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_ANY);
565 printf("Failed noprog update sockmap '%i:%i'\n",
571 /* Test attaching/detaching bad fds */
572 err = bpf_prog_attach(-1, fd, BPF_SK_SKB_STREAM_PARSER, 0);
574 printf("Failed invalid parser prog attach\n");
578 err = bpf_prog_attach(-1, fd, BPF_SK_SKB_STREAM_VERDICT, 0);
580 printf("Failed invalid verdict prog attach\n");
584 err = bpf_prog_attach(-1, fd, __MAX_BPF_ATTACH_TYPE, 0);
586 printf("Failed unknown prog attach\n");
590 err = bpf_prog_detach(fd, BPF_SK_SKB_STREAM_PARSER);
592 printf("Failed empty parser prog detach\n");
596 err = bpf_prog_detach(fd, BPF_SK_SKB_STREAM_VERDICT);
598 printf("Failed empty verdict prog detach\n");
602 err = bpf_prog_detach(fd, __MAX_BPF_ATTACH_TYPE);
604 printf("Detach invalid prog successful\n");
608 /* Load SK_SKB program and Attach */
609 err = bpf_prog_load(SOCKMAP_PARSE_PROG,
610 BPF_PROG_TYPE_SK_SKB, &obj, &parse_prog);
612 printf("Failed to load SK_SKB parse prog\n");
616 err = bpf_prog_load(SOCKMAP_VERDICT_PROG,
617 BPF_PROG_TYPE_SK_SKB, &obj, &verdict_prog);
619 printf("Failed to load SK_SKB verdict prog\n");
623 bpf_map_rx = bpf_object__find_map_by_name(obj, "sock_map_rx");
624 if (IS_ERR(bpf_map_rx)) {
625 printf("Failed to load map rx from verdict prog\n");
629 map_fd_rx = bpf_map__fd(bpf_map_rx);
631 printf("Failed to get map fd\n");
635 bpf_map_tx = bpf_object__find_map_by_name(obj, "sock_map_tx");
636 if (IS_ERR(bpf_map_tx)) {
637 printf("Failed to load map tx from verdict prog\n");
641 map_fd_tx = bpf_map__fd(bpf_map_tx);
643 printf("Failed to get map tx fd\n");
647 bpf_map_break = bpf_object__find_map_by_name(obj, "sock_map_break");
648 if (IS_ERR(bpf_map_break)) {
649 printf("Failed to load map tx from verdict prog\n");
653 map_fd_break = bpf_map__fd(bpf_map_break);
654 if (map_fd_break < 0) {
655 printf("Failed to get map tx fd\n");
659 err = bpf_prog_attach(parse_prog, map_fd_break,
660 BPF_SK_SKB_STREAM_PARSER, 0);
662 printf("Allowed attaching SK_SKB program to invalid map\n");
666 err = bpf_prog_attach(parse_prog, map_fd_rx,
667 BPF_SK_SKB_STREAM_PARSER, 0);
669 printf("Failed stream parser bpf prog attach\n");
673 err = bpf_prog_attach(verdict_prog, map_fd_rx,
674 BPF_SK_SKB_STREAM_VERDICT, 0);
676 printf("Failed stream verdict bpf prog attach\n");
680 err = bpf_prog_attach(verdict_prog, map_fd_rx,
681 __MAX_BPF_ATTACH_TYPE, 0);
683 printf("Attached unknown bpf prog\n");
687 /* Test map update elem afterwards fd lives in fd and map_fd */
688 for (i = 0; i < 6; i++) {
689 err = bpf_map_update_elem(map_fd_rx, &i, &sfd[i], BPF_ANY);
691 printf("Failed map_fd_rx update sockmap %i '%i:%i'\n",
695 err = bpf_map_update_elem(map_fd_tx, &i, &sfd[i], BPF_ANY);
697 printf("Failed map_fd_tx update sockmap %i '%i:%i'\n",
703 /* Test map delete elem and remove send/recv sockets */
704 for (i = 2; i < 4; i++) {
705 err = bpf_map_delete_elem(map_fd_rx, &i);
707 printf("Failed delete sockmap rx %i '%i:%i'\n",
711 err = bpf_map_delete_elem(map_fd_tx, &i);
713 printf("Failed delete sockmap tx %i '%i:%i'\n",
719 /* Test map send/recv */
720 for (i = 0; i < 2; i++) {
723 sc = send(sfd[2], buf, 20, 0);
725 printf("Failed sockmap send\n");
733 s = select(sfd[3] + 1, &w, NULL, NULL, &to);
735 perror("Failed sockmap select()");
738 printf("Failed sockmap unexpected timeout\n");
742 if (!FD_ISSET(sfd[3], &w)) {
743 printf("Failed sockmap select/recv\n");
747 rc = recv(sfd[3], buf, sizeof(buf), 0);
749 printf("Failed sockmap recv\n");
754 /* Negative null entry lookup from datapath should be dropped */
757 sc = send(sfd[2], buf, 20, 0);
759 printf("Failed sockmap send\n");
763 /* Push fd into same slot */
765 err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_NOEXIST);
767 printf("Failed allowed sockmap dup slot BPF_NOEXIST\n");
771 err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_ANY);
773 printf("Failed sockmap update new slot BPF_ANY\n");
777 err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_EXIST);
779 printf("Failed sockmap update new slot BPF_EXIST\n");
783 /* Delete the elems without programs */
784 for (i = 0; i < 6; i++) {
785 err = bpf_map_delete_elem(fd, &i);
787 printf("Failed delete sockmap %i '%i:%i'\n",
792 /* Test having multiple maps open and set with programs on same fds */
793 err = bpf_prog_attach(parse_prog, fd,
794 BPF_SK_SKB_STREAM_PARSER, 0);
796 printf("Failed fd bpf parse prog attach\n");
799 err = bpf_prog_attach(verdict_prog, fd,
800 BPF_SK_SKB_STREAM_VERDICT, 0);
802 printf("Failed fd bpf verdict prog attach\n");
806 for (i = 4; i < 6; i++) {
807 err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_ANY);
809 printf("Failed allowed duplicate programs in update ANY sockmap %i '%i:%i'\n",
813 err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_NOEXIST);
815 printf("Failed allowed duplicate program in update NOEXIST sockmap %i '%i:%i'\n",
819 err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_EXIST);
821 printf("Failed allowed duplicate program in update EXIST sockmap %i '%i:%i'\n",
827 /* Test tasks number of forked operations */
828 for (i = 0; i < tasks; i++) {
831 for (i = 0; i < 6; i++) {
832 bpf_map_delete_elem(map_fd_tx, &i);
833 bpf_map_delete_elem(map_fd_rx, &i);
834 bpf_map_update_elem(map_fd_tx, &i,
836 bpf_map_update_elem(map_fd_rx, &i,
840 } else if (pid[i] == -1) {
841 printf("Couldn't spawn #%d process!\n", i);
846 for (i = 0; i < tasks; i++) {
849 assert(waitpid(pid[i], &status, 0) == pid[i]);
853 err = bpf_prog_detach(map_fd_rx, __MAX_BPF_ATTACH_TYPE);
855 printf("Detached an invalid prog type.\n");
859 err = bpf_prog_detach(map_fd_rx, BPF_SK_SKB_STREAM_PARSER);
861 printf("Failed parser prog detach\n");
865 err = bpf_prog_detach(map_fd_rx, BPF_SK_SKB_STREAM_VERDICT);
867 printf("Failed parser prog detach\n");
871 /* Test map close sockets and empty maps */
872 for (i = 0; i < 6; i++) {
873 bpf_map_delete_elem(map_fd_tx, &i);
874 bpf_map_delete_elem(map_fd_rx, &i);
879 bpf_object__close(obj);
882 for (i = 0; i < 6; i++)
884 printf("Failed to create sockmap '%i:%s'!\n", i, strerror(errno));
887 for (i = 0; i < 6; i++) {
889 bpf_map_delete_elem(map_fd_tx, &i);
891 bpf_map_delete_elem(map_fd_rx, &i);
898 #define MAP_SIZE (32 * 1024)
900 static void test_map_large(void)
909 fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value),
910 MAP_SIZE, map_flags);
912 printf("Failed to create large map '%s'!\n", strerror(errno));
916 for (i = 0; i < MAP_SIZE; i++) {
917 key = (struct bigkey) { .c = i };
920 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == 0);
924 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == -1 &&
927 /* Iterate through all elements. */
928 assert(bpf_map_get_next_key(fd, NULL, &key) == 0);
930 for (i = 0; i < MAP_SIZE; i++)
931 assert(bpf_map_get_next_key(fd, &key, &key) == 0);
932 assert(bpf_map_get_next_key(fd, &key, &key) == -1 && errno == ENOENT);
935 assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && value == 0);
937 assert(bpf_map_lookup_elem(fd, &key, &value) == -1 && errno == ENOENT);
942 #define run_parallel(N, FN, DATA) \
943 printf("Fork %d tasks to '" #FN "'\n", N); \
944 __run_parallel(N, FN, DATA)
946 static void __run_parallel(int tasks, void (*fn)(int task, void *data),
952 for (i = 0; i < tasks; i++) {
957 } else if (pid[i] == -1) {
958 printf("Couldn't spawn #%d process!\n", i);
963 for (i = 0; i < tasks; i++) {
966 assert(waitpid(pid[i], &status, 0) == pid[i]);
971 static void test_map_stress(void)
973 run_parallel(100, test_hashmap, NULL);
974 run_parallel(100, test_hashmap_percpu, NULL);
975 run_parallel(100, test_hashmap_sizes, NULL);
976 run_parallel(100, test_hashmap_walk, NULL);
978 run_parallel(100, test_arraymap, NULL);
979 run_parallel(100, test_arraymap_percpu, NULL);
987 static void test_update_delete(int fn, void *data)
989 int do_update = ((int *)data)[1];
990 int fd = ((int *)data)[0];
993 for (i = fn; i < MAP_SIZE; i += TASKS) {
997 assert(bpf_map_update_elem(fd, &key, &value,
999 assert(bpf_map_update_elem(fd, &key, &value,
1002 assert(bpf_map_delete_elem(fd, &key) == 0);
1007 static void test_map_parallel(void)
1009 int i, fd, key = 0, value = 0;
1012 fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value),
1013 MAP_SIZE, map_flags);
1015 printf("Failed to create map for parallel test '%s'!\n",
1020 /* Use the same fd in children to add elements to this map:
1021 * child_0 adds key=0, key=1024, key=2048, ...
1022 * child_1 adds key=1, key=1025, key=2049, ...
1023 * child_1023 adds key=1023, ...
1026 data[1] = DO_UPDATE;
1027 run_parallel(TASKS, test_update_delete, data);
1029 /* Check that key=0 is already there. */
1030 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == -1 &&
1033 /* Check that all elements were inserted. */
1034 assert(bpf_map_get_next_key(fd, NULL, &key) == 0);
1036 for (i = 0; i < MAP_SIZE; i++)
1037 assert(bpf_map_get_next_key(fd, &key, &key) == 0);
1038 assert(bpf_map_get_next_key(fd, &key, &key) == -1 && errno == ENOENT);
1040 /* Another check for all elements */
1041 for (i = 0; i < MAP_SIZE; i++) {
1042 key = MAP_SIZE - i - 1;
1044 assert(bpf_map_lookup_elem(fd, &key, &value) == 0 &&
1048 /* Now let's delete all elemenets in parallel. */
1049 data[1] = DO_DELETE;
1050 run_parallel(TASKS, test_update_delete, data);
1052 /* Nothing should be left. */
1054 assert(bpf_map_get_next_key(fd, NULL, &key) == -1 && errno == ENOENT);
1055 assert(bpf_map_get_next_key(fd, &key, &key) == -1 && errno == ENOENT);
1058 static void test_map_rdonly(void)
1060 int fd, key = 0, value = 0;
1062 fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value),
1063 MAP_SIZE, map_flags | BPF_F_RDONLY);
1065 printf("Failed to create map for read only test '%s'!\n",
1072 /* Insert key=1 element. */
1073 assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == -1 &&
1076 /* Check that key=2 is not found. */
1077 assert(bpf_map_lookup_elem(fd, &key, &value) == -1 && errno == ENOENT);
1078 assert(bpf_map_get_next_key(fd, &key, &value) == -1 && errno == ENOENT);
1081 static void test_map_wronly(void)
1083 int fd, key = 0, value = 0;
1085 fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value),
1086 MAP_SIZE, map_flags | BPF_F_WRONLY);
1088 printf("Failed to create map for read only test '%s'!\n",
1095 /* Insert key=1 element. */
1096 assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == 0);
1098 /* Check that key=2 is not found. */
1099 assert(bpf_map_lookup_elem(fd, &key, &value) == -1 && errno == EPERM);
1100 assert(bpf_map_get_next_key(fd, &key, &value) == -1 && errno == EPERM);
1103 static void run_all_tests(void)
1105 test_hashmap(0, NULL);
1106 test_hashmap_percpu(0, NULL);
1107 test_hashmap_walk(0, NULL);
1109 test_arraymap(0, NULL);
1110 test_arraymap_percpu(0, NULL);
1112 test_arraymap_percpu_many_keys();
1114 test_devmap(0, NULL);
1115 test_sockmap(0, NULL);
1118 test_map_parallel();
1127 struct rlimit rinf = { RLIM_INFINITY, RLIM_INFINITY };
1129 setrlimit(RLIMIT_MEMLOCK, &rinf);
1134 map_flags = BPF_F_NO_PREALLOC;
1137 printf("test_maps: OK\n");