1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2017-2018 Covalent IO, Inc. http://covalent.io
5 #include <sys/socket.h>
7 #include <sys/select.h>
8 #include <netinet/in.h>
13 #include <sys/ioctl.h>
22 #include <sys/resource.h>
23 #include <sys/types.h>
24 #include <sys/sendfile.h>
26 #include <linux/netlink.h>
27 #include <linux/socket.h>
28 #include <linux/sock_diag.h>
29 #include <linux/bpf.h>
30 #include <linux/if_link.h>
37 #include <bpf/libbpf.h>
40 #include "bpf_rlimit.h"
41 #include "cgroup_helpers.h"
44 static void running_handler(int a);
46 /* randomly selected ports for testing on lo */
50 #define BPF_SOCKMAP_FILENAME "test_sockmap_kern.o"
51 #define BPF_SOCKHASH_FILENAME "test_sockhash_kern.o"
52 #define CG_PATH "/sockmap"
55 int s1, s2, c1, c2, p1, p2;
60 struct bpf_map *maps[8];
66 int txmsg_redir_noisy;
75 static const struct option long_options[] = {
76 {"help", no_argument, NULL, 'h' },
77 {"cgroup", required_argument, NULL, 'c' },
78 {"rate", required_argument, NULL, 'r' },
79 {"verbose", no_argument, NULL, 'v' },
80 {"iov_count", required_argument, NULL, 'i' },
81 {"length", required_argument, NULL, 'l' },
82 {"test", required_argument, NULL, 't' },
83 {"data_test", no_argument, NULL, 'd' },
84 {"txmsg", no_argument, &txmsg_pass, 1 },
85 {"txmsg_noisy", no_argument, &txmsg_noisy, 1 },
86 {"txmsg_redir", no_argument, &txmsg_redir, 1 },
87 {"txmsg_redir_noisy", no_argument, &txmsg_redir_noisy, 1},
88 {"txmsg_drop", no_argument, &txmsg_drop, 1 },
89 {"txmsg_apply", required_argument, NULL, 'a'},
90 {"txmsg_cork", required_argument, NULL, 'k'},
91 {"txmsg_start", required_argument, NULL, 's'},
92 {"txmsg_end", required_argument, NULL, 'e'},
93 {"txmsg_ingress", no_argument, &txmsg_ingress, 1 },
94 {"txmsg_skb", no_argument, &txmsg_skb, 1 },
98 static void usage(char *argv[])
102 printf(" Usage: %s --cgroup <cgroup_path>\n", argv[0]);
103 printf(" options:\n");
104 for (i = 0; long_options[i].name != 0; i++) {
105 printf(" --%-12s", long_options[i].name);
106 if (long_options[i].flag != NULL)
107 printf(" flag (internal value:%d)\n",
108 *long_options[i].flag);
110 printf(" -%c\n", long_options[i].val);
115 static int sockmap_init_sockets(int verbose)
118 struct sockaddr_in addr;
119 int *fds[4] = {&s1, &s2, &c1, &c2};
121 s1 = s2 = p1 = p2 = c1 = c2 = 0;
124 for (i = 0; i < 4; i++) {
125 *fds[i] = socket(AF_INET, SOCK_STREAM, 0);
127 perror("socket s1 failed()");
133 for (i = 0; i < 2; i++) {
134 err = setsockopt(*fds[i], SOL_SOCKET, SO_REUSEADDR,
135 (char *)&one, sizeof(one));
137 perror("setsockopt failed()");
142 /* Non-blocking sockets */
143 for (i = 0; i < 2; i++) {
144 err = ioctl(*fds[i], FIONBIO, (char *)&one);
146 perror("ioctl s1 failed()");
151 /* Bind server sockets */
152 memset(&addr, 0, sizeof(struct sockaddr_in));
153 addr.sin_family = AF_INET;
154 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
156 addr.sin_port = htons(S1_PORT);
157 err = bind(s1, (struct sockaddr *)&addr, sizeof(addr));
159 perror("bind s1 failed()\n");
163 addr.sin_port = htons(S2_PORT);
164 err = bind(s2, (struct sockaddr *)&addr, sizeof(addr));
166 perror("bind s2 failed()\n");
170 /* Listen server sockets */
171 addr.sin_port = htons(S1_PORT);
172 err = listen(s1, 32);
174 perror("listen s1 failed()\n");
178 addr.sin_port = htons(S2_PORT);
179 err = listen(s2, 32);
181 perror("listen s1 failed()\n");
185 /* Initiate Connect */
186 addr.sin_port = htons(S1_PORT);
187 err = connect(c1, (struct sockaddr *)&addr, sizeof(addr));
188 if (err < 0 && errno != EINPROGRESS) {
189 perror("connect c1 failed()\n");
193 addr.sin_port = htons(S2_PORT);
194 err = connect(c2, (struct sockaddr *)&addr, sizeof(addr));
195 if (err < 0 && errno != EINPROGRESS) {
196 perror("connect c2 failed()\n");
198 } else if (err < 0) {
202 /* Accept Connecrtions */
203 p1 = accept(s1, NULL, NULL);
205 perror("accept s1 failed()\n");
209 p2 = accept(s2, NULL, NULL);
211 perror("accept s1 failed()\n");
216 printf("connected sockets: c1 <-> p1, c2 <-> p2\n");
217 printf("cgroups binding: c1(%i) <-> s1(%i) - - - c2(%i) <-> s2(%i)\n",
226 struct timespec start;
230 struct sockmap_options {
241 static int msg_loop_sendpage(int fd, int iov_length, int cnt,
243 struct sockmap_options *opt)
245 bool drop = opt->drop_expected;
250 file = fopen(".sendpage_tst.tmp", "w+");
251 for (i = 0; i < iov_length * cnt; i++, k++)
252 fwrite(&k, sizeof(char), 1, file);
254 fseek(file, 0, SEEK_SET);
257 fp = open(".sendpage_tst.tmp", O_RDONLY);
258 clock_gettime(CLOCK_MONOTONIC, &s->start);
259 for (i = 0; i < cnt; i++) {
260 int sent = sendfile(fd, fp, NULL, iov_length);
262 if (!drop && sent < 0) {
263 perror("send loop error:");
266 } else if (drop && sent >= 0) {
267 printf("sendpage loop error expected: %i\n", sent);
273 s->bytes_sent += sent;
275 clock_gettime(CLOCK_MONOTONIC, &s->end);
280 static int msg_loop(int fd, int iov_count, int iov_length, int cnt,
281 struct msg_stats *s, bool tx,
282 struct sockmap_options *opt)
284 struct msghdr msg = {0};
285 int err, i, flags = MSG_NOSIGNAL;
288 bool data_test = opt->data_test;
289 bool drop = opt->drop_expected;
291 iov = calloc(iov_count, sizeof(struct iovec));
296 for (i = 0; i < iov_count; i++) {
297 unsigned char *d = calloc(iov_length, sizeof(char));
300 fprintf(stderr, "iov_count %i/%i OOM\n", i, iov_count);
304 iov[i].iov_len = iov_length;
306 if (data_test && tx) {
309 for (j = 0; j < iov_length; j++)
315 msg.msg_iovlen = iov_count;
319 clock_gettime(CLOCK_MONOTONIC, &s->start);
320 for (i = 0; i < cnt; i++) {
321 int sent = sendmsg(fd, &msg, flags);
323 if (!drop && sent < 0) {
324 perror("send loop error:");
326 } else if (drop && sent >= 0) {
327 printf("send loop error expected: %i\n", sent);
332 s->bytes_sent += sent;
334 clock_gettime(CLOCK_MONOTONIC, &s->end);
336 int slct, recv, max_fd = fd;
337 int fd_flags = O_NONBLOCK;
338 struct timeval timeout;
345 chunk_sz = iov_length * cnt;
347 chunk_sz = iov_length * iov_count;
350 total_bytes = (float)iov_count * (float)iov_length * (float)cnt;
351 err = clock_gettime(CLOCK_MONOTONIC, &s->start);
353 perror("recv start time: ");
354 while (s->bytes_recvd < total_bytes) {
357 timeout.tv_usec = 1000;
367 slct = select(max_fd + 1, &w, NULL, NULL, &timeout);
370 clock_gettime(CLOCK_MONOTONIC, &s->end);
374 fprintf(stderr, "unexpected timeout\n");
376 clock_gettime(CLOCK_MONOTONIC, &s->end);
380 recv = recvmsg(fd, &msg, flags);
382 if (errno != EWOULDBLOCK) {
383 clock_gettime(CLOCK_MONOTONIC, &s->end);
384 perror("recv failed()\n");
389 s->bytes_recvd += recv;
394 for (i = 0; i < msg.msg_iovlen; i++) {
395 unsigned char *d = iov[i].iov_base;
398 j < iov[i].iov_len && recv; j++) {
402 "detected data corruption @iov[%i]:%i %02x != %02x, %02x ?= %02x\n",
403 i, j, d[j], k - 1, d[j+1], k);
407 if (bytes_cnt == chunk_sz) {
416 clock_gettime(CLOCK_MONOTONIC, &s->end);
419 for (i = 0; i < iov_count; i++)
420 free(iov[i].iov_base);
424 for (i = 0; i < iov_count; i++)
425 free(iov[i].iov_base);
430 static float giga = 1000000000;
432 static inline float sentBps(struct msg_stats s)
434 return s.bytes_sent / (s.end.tv_sec - s.start.tv_sec);
437 static inline float recvdBps(struct msg_stats s)
439 return s.bytes_recvd / (s.end.tv_sec - s.start.tv_sec);
442 static int sendmsg_test(struct sockmap_options *opt)
444 float sent_Bps = 0, recvd_Bps = 0;
445 int rx_fd, txpid, rxpid, err = 0;
446 struct msg_stats s = {0};
447 int iov_count = opt->iov_count;
448 int iov_buf = opt->iov_length;
449 int rx_status, tx_status;
461 if (opt->drop_expected)
466 err = msg_loop(rx_fd, iov_count, iov_buf,
467 cnt, &s, false, opt);
468 if (err && opt->verbose)
470 "msg_loop_rx: iov_count %i iov_buf %i cnt %i err %i\n",
471 iov_count, iov_buf, cnt, err);
472 shutdown(p2, SHUT_RDWR);
473 shutdown(p1, SHUT_RDWR);
474 if (s.end.tv_sec - s.start.tv_sec) {
475 sent_Bps = sentBps(s);
476 recvd_Bps = recvdBps(s);
480 "rx_sendmsg: TX: %zuB %fB/s %fGB/s RX: %zuB %fB/s %fGB/s\n",
481 s.bytes_sent, sent_Bps, sent_Bps/giga,
482 s.bytes_recvd, recvd_Bps, recvd_Bps/giga);
483 if (err && txmsg_cork)
486 } else if (rxpid == -1) {
487 perror("msg_loop_rx: ");
494 err = msg_loop_sendpage(c1, iov_buf, cnt, &s, opt);
496 err = msg_loop(c1, iov_count, iov_buf,
501 "msg_loop_tx: iov_count %i iov_buf %i cnt %i err %i\n",
502 iov_count, iov_buf, cnt, err);
503 shutdown(c1, SHUT_RDWR);
504 if (s.end.tv_sec - s.start.tv_sec) {
505 sent_Bps = sentBps(s);
506 recvd_Bps = recvdBps(s);
510 "tx_sendmsg: TX: %zuB %fB/s %f GB/s RX: %zuB %fB/s %fGB/s\n",
511 s.bytes_sent, sent_Bps, sent_Bps/giga,
512 s.bytes_recvd, recvd_Bps, recvd_Bps/giga);
514 } else if (txpid == -1) {
515 perror("msg_loop_tx: ");
519 assert(waitpid(rxpid, &rx_status, 0) == rxpid);
520 assert(waitpid(txpid, &tx_status, 0) == txpid);
521 if (WIFEXITED(rx_status)) {
522 err = WEXITSTATUS(rx_status);
524 fprintf(stderr, "rx thread exited with err %d. ", err);
528 if (WIFEXITED(tx_status)) {
529 err = WEXITSTATUS(tx_status);
531 fprintf(stderr, "tx thread exited with err %d. ", err);
537 static int forever_ping_pong(int rate, struct sockmap_options *opt)
539 struct timeval timeout;
540 char buf[1024] = {0};
546 /* Ping/Pong data from client to server */
547 sc = send(c1, buf, sizeof(buf), 0);
549 perror("send failed()\n");
554 int s, rc, i, max_fd = p2;
564 s = select(max_fd + 1, &w, NULL, NULL, &timeout);
569 fprintf(stderr, "unexpected timeout\n");
573 for (i = 0; i <= max_fd && s > 0; ++i) {
574 if (!FD_ISSET(i, &w))
579 rc = recv(i, buf, sizeof(buf), 0);
581 if (errno != EWOULDBLOCK) {
582 perror("recv failed()\n");
592 sc = send(i, buf, rc, 0);
594 perror("send failed()\n");
620 static int run_options(struct sockmap_options *options, int cg_fd, int test)
622 int i, key, next_key, err, tx_prog_fd = -1, zero = 0;
624 /* If base test skip BPF setup */
625 if (test == BASE || test == BASE_SENDPAGE)
628 /* Attach programs to sockmap */
629 err = bpf_prog_attach(prog_fd[0], map_fd[0],
630 BPF_SK_SKB_STREAM_PARSER, 0);
633 "ERROR: bpf_prog_attach (sockmap %i->%i): %d (%s)\n",
634 prog_fd[0], map_fd[0], err, strerror(errno));
638 err = bpf_prog_attach(prog_fd[1], map_fd[0],
639 BPF_SK_SKB_STREAM_VERDICT, 0);
641 fprintf(stderr, "ERROR: bpf_prog_attach (sockmap): %d (%s)\n",
642 err, strerror(errno));
646 /* Attach to cgroups */
647 err = bpf_prog_attach(prog_fd[2], cg_fd, BPF_CGROUP_SOCK_OPS, 0);
649 fprintf(stderr, "ERROR: bpf_prog_attach (groups): %d (%s)\n",
650 err, strerror(errno));
655 err = sockmap_init_sockets(options->verbose);
657 fprintf(stderr, "ERROR: test socket failed: %d\n", err);
661 /* Attach txmsg program to sockmap */
663 tx_prog_fd = prog_fd[3];
664 else if (txmsg_noisy)
665 tx_prog_fd = prog_fd[4];
666 else if (txmsg_redir)
667 tx_prog_fd = prog_fd[5];
668 else if (txmsg_redir_noisy)
669 tx_prog_fd = prog_fd[6];
671 tx_prog_fd = prog_fd[9];
672 /* apply and cork must be last */
673 else if (txmsg_apply)
674 tx_prog_fd = prog_fd[7];
676 tx_prog_fd = prog_fd[8];
683 err = bpf_prog_attach(tx_prog_fd,
684 map_fd[1], BPF_SK_MSG_VERDICT, 0);
687 "ERROR: bpf_prog_attach (txmsg): %d (%s)\n",
688 err, strerror(errno));
692 err = bpf_map_update_elem(map_fd[1], &i, &c1, BPF_ANY);
695 "ERROR: bpf_map_update_elem (txmsg): %d (%s\n",
696 err, strerror(errno));
700 if (txmsg_redir || txmsg_redir_noisy)
705 err = bpf_map_update_elem(map_fd[2], &i, &redir_fd, BPF_ANY);
708 "ERROR: bpf_map_update_elem (txmsg): %d (%s\n",
709 err, strerror(errno));
714 err = bpf_map_update_elem(map_fd[3],
715 &i, &txmsg_apply, BPF_ANY);
718 "ERROR: bpf_map_update_elem (apply_bytes): %d (%s\n",
719 err, strerror(errno));
725 err = bpf_map_update_elem(map_fd[4],
726 &i, &txmsg_cork, BPF_ANY);
729 "ERROR: bpf_map_update_elem (cork_bytes): %d (%s\n",
730 err, strerror(errno));
736 err = bpf_map_update_elem(map_fd[5],
737 &i, &txmsg_start, BPF_ANY);
740 "ERROR: bpf_map_update_elem (txmsg_start): %d (%s)\n",
741 err, strerror(errno));
748 err = bpf_map_update_elem(map_fd[5],
749 &i, &txmsg_end, BPF_ANY);
752 "ERROR: bpf_map_update_elem (txmsg_end): %d (%s)\n",
753 err, strerror(errno));
759 int in = BPF_F_INGRESS;
762 err = bpf_map_update_elem(map_fd[6], &i, &in, BPF_ANY);
765 "ERROR: bpf_map_update_elem (txmsg_ingress): %d (%s)\n",
766 err, strerror(errno));
769 err = bpf_map_update_elem(map_fd[1], &i, &p1, BPF_ANY);
772 "ERROR: bpf_map_update_elem (p1 txmsg): %d (%s)\n",
773 err, strerror(errno));
775 err = bpf_map_update_elem(map_fd[2], &i, &p1, BPF_ANY);
778 "ERROR: bpf_map_update_elem (p1 redir): %d (%s)\n",
779 err, strerror(errno));
783 err = bpf_map_update_elem(map_fd[2], &i, &p2, BPF_ANY);
786 "ERROR: bpf_map_update_elem (p2 txmsg): %d (%s)\n",
787 err, strerror(errno));
792 int skb_fd = (test == SENDMSG || test == SENDPAGE) ?
794 int ingress = BPF_F_INGRESS;
797 err = bpf_map_update_elem(map_fd[7],
798 &i, &ingress, BPF_ANY);
801 "ERROR: bpf_map_update_elem (txmsg_ingress): %d (%s)\n",
802 err, strerror(errno));
806 err = bpf_map_update_elem(map_fd[0],
807 &i, &skb_fd, BPF_ANY);
810 "ERROR: bpf_map_update_elem (c1 sockmap): %d (%s)\n",
811 err, strerror(errno));
817 options->drop_expected = true;
819 if (test == PING_PONG)
820 err = forever_ping_pong(options->rate, options);
821 else if (test == SENDMSG) {
822 options->base = false;
823 options->sendpage = false;
824 err = sendmsg_test(options);
825 } else if (test == SENDPAGE) {
826 options->base = false;
827 options->sendpage = true;
828 err = sendmsg_test(options);
829 } else if (test == BASE) {
830 options->base = true;
831 options->sendpage = false;
832 err = sendmsg_test(options);
833 } else if (test == BASE_SENDPAGE) {
834 options->base = true;
835 options->sendpage = true;
836 err = sendmsg_test(options);
838 fprintf(stderr, "unknown test\n");
840 /* Detatch and zero all the maps */
841 bpf_prog_detach2(prog_fd[2], cg_fd, BPF_CGROUP_SOCK_OPS);
842 bpf_prog_detach2(prog_fd[0], map_fd[0], BPF_SK_SKB_STREAM_PARSER);
843 bpf_prog_detach2(prog_fd[1], map_fd[0], BPF_SK_SKB_STREAM_VERDICT);
845 bpf_prog_detach2(tx_prog_fd, map_fd[1], BPF_SK_MSG_VERDICT);
847 for (i = 0; i < 8; i++) {
849 bpf_map_update_elem(map_fd[i], &key, &zero, BPF_ANY);
850 while (bpf_map_get_next_key(map_fd[i], &key, &next_key) == 0) {
851 bpf_map_update_elem(map_fd[i], &key, &zero, BPF_ANY);
865 static char *test_to_str(int test)
877 static void test_options(char *options)
879 char tstr[OPTSTRING];
881 memset(options, 0, OPTSTRING);
884 strncat(options, "pass,", OPTSTRING);
886 strncat(options, "pass_noisy,", OPTSTRING);
888 strncat(options, "redir,", OPTSTRING);
889 if (txmsg_redir_noisy)
890 strncat(options, "redir_noisy,", OPTSTRING);
892 strncat(options, "drop,", OPTSTRING);
894 snprintf(tstr, OPTSTRING, "apply %d,", txmsg_apply);
895 strncat(options, tstr, OPTSTRING);
898 snprintf(tstr, OPTSTRING, "cork %d,", txmsg_cork);
899 strncat(options, tstr, OPTSTRING);
902 snprintf(tstr, OPTSTRING, "start %d,", txmsg_start);
903 strncat(options, tstr, OPTSTRING);
906 snprintf(tstr, OPTSTRING, "end %d,", txmsg_end);
907 strncat(options, tstr, OPTSTRING);
910 strncat(options, "ingress,", OPTSTRING);
912 strncat(options, "skb,", OPTSTRING);
915 static int __test_exec(int cgrp, int test, struct sockmap_options *opt)
917 char *options = calloc(OPTSTRING, sizeof(char));
920 if (test == SENDPAGE)
921 opt->sendpage = true;
923 opt->sendpage = false;
926 opt->drop_expected = true;
928 opt->drop_expected = false;
930 test_options(options);
933 "[TEST %i]: (%i, %i, %i, %s, %s): ",
934 test_cnt, opt->rate, opt->iov_count, opt->iov_length,
935 test_to_str(test), options);
937 err = run_options(opt, cgrp, test);
938 fprintf(stdout, "%s\n", !err ? "PASS" : "FAILED");
940 !err ? passed++ : failed++;
945 static int test_exec(int cgrp, struct sockmap_options *opt)
947 int err = __test_exec(cgrp, SENDMSG, opt);
952 err = __test_exec(cgrp, SENDPAGE, opt);
957 static int test_loop(int cgrp)
959 struct sockmap_options opt;
965 opt.sendpage = false;
966 opt.data_test = false;
967 opt.drop_expected = false;
973 for (i = 1; i < 100; i += 33) {
974 for (l = 1; l < 100; l += 33) {
978 err = test_exec(cgrp, &opt);
988 static int test_txmsg(int cgrp)
992 txmsg_pass = txmsg_noisy = txmsg_redir_noisy = txmsg_drop = 0;
993 txmsg_apply = txmsg_cork = 0;
994 txmsg_ingress = txmsg_skb = 0;
997 err = test_loop(cgrp);
1003 err = test_loop(cgrp);
1009 err = test_loop(cgrp);
1016 err = test_loop(cgrp);
1028 static int test_send(struct sockmap_options *opt, int cgrp)
1032 opt->iov_length = 1;
1035 err = test_exec(cgrp, opt);
1039 opt->iov_length = 1;
1040 opt->iov_count = 1024;
1042 err = test_exec(cgrp, opt);
1046 opt->iov_length = 1024;
1049 err = test_exec(cgrp, opt);
1053 opt->iov_length = 1;
1056 err = test_exec(cgrp, opt);
1060 opt->iov_length = 256;
1061 opt->iov_count = 1024;
1063 err = test_exec(cgrp, opt);
1069 opt->iov_length = 5;
1070 err = test_exec(cgrp, opt);
1078 static int test_mixed(int cgrp)
1080 struct sockmap_options opt = {0};
1083 txmsg_pass = txmsg_noisy = txmsg_redir_noisy = txmsg_drop = 0;
1084 txmsg_apply = txmsg_cork = 0;
1085 txmsg_start = txmsg_end = 0;
1086 /* Test small and large iov_count values with pass/redir/apply/cork */
1091 err = test_send(&opt, cgrp);
1099 err = test_send(&opt, cgrp);
1107 err = test_send(&opt, cgrp);
1115 err = test_send(&opt, cgrp);
1123 err = test_send(&opt, cgrp);
1131 err = test_send(&opt, cgrp);
1139 err = test_send(&opt, cgrp);
1147 err = test_send(&opt, cgrp);
1155 err = test_send(&opt, cgrp);
1163 err = test_send(&opt, cgrp);
1171 err = test_send(&opt, cgrp);
1179 err = test_send(&opt, cgrp);
1187 err = test_send(&opt, cgrp);
1194 static int test_start_end(int cgrp)
1196 struct sockmap_options opt = {0};
1199 /* Test basic start/end with lots of iov_count and iov_lengths */
1202 err = test_txmsg(cgrp);
1206 /* Test start/end with cork */
1209 opt.iov_length = 100;
1212 for (i = 99; i <= 1600; i += 500) {
1215 err = test_exec(cgrp, &opt);
1220 /* Test start/end with cork but pull data in middle */
1221 for (i = 199; i <= 1600; i += 500) {
1224 err = test_exec(cgrp, &opt);
1229 /* Test start/end with cork pulling last sg entry */
1232 err = test_exec(cgrp, &opt);
1236 /* Test start/end pull of single byte in last page */
1239 err = test_exec(cgrp, &opt);
1243 /* Test start/end with end < start */
1246 err = test_exec(cgrp, &opt);
1250 /* Test start/end with end > data */
1253 err = test_exec(cgrp, &opt);
1257 /* Test start/end with start > data */
1260 err = test_exec(cgrp, &opt);
1269 char *map_names[] = {
1280 int prog_attach_type[] = {
1281 BPF_SK_SKB_STREAM_PARSER,
1282 BPF_SK_SKB_STREAM_VERDICT,
1283 BPF_CGROUP_SOCK_OPS,
1294 BPF_PROG_TYPE_SK_SKB,
1295 BPF_PROG_TYPE_SK_SKB,
1296 BPF_PROG_TYPE_SOCK_OPS,
1297 BPF_PROG_TYPE_SK_MSG,
1298 BPF_PROG_TYPE_SK_MSG,
1299 BPF_PROG_TYPE_SK_MSG,
1300 BPF_PROG_TYPE_SK_MSG,
1301 BPF_PROG_TYPE_SK_MSG,
1302 BPF_PROG_TYPE_SK_MSG,
1303 BPF_PROG_TYPE_SK_MSG,
1306 static int populate_progs(char *bpf_file)
1308 struct bpf_program *prog;
1309 struct bpf_object *obj;
1313 obj = bpf_object__open(bpf_file);
1314 err = libbpf_get_error(obj);
1318 libbpf_strerror(err, err_buf, sizeof(err_buf));
1319 printf("Unable to load eBPF objects in file '%s' : %s\n",
1324 bpf_object__for_each_program(prog, obj) {
1325 bpf_program__set_type(prog, prog_type[i]);
1326 bpf_program__set_expected_attach_type(prog,
1327 prog_attach_type[i]);
1331 i = bpf_object__load(obj);
1333 bpf_object__for_each_program(prog, obj) {
1334 prog_fd[i] = bpf_program__fd(prog);
1338 for (i = 0; i < sizeof(map_fd)/sizeof(int); i++) {
1339 maps[i] = bpf_object__find_map_by_name(obj, map_names[i]);
1340 map_fd[i] = bpf_map__fd(maps[i]);
1341 if (map_fd[i] < 0) {
1342 fprintf(stderr, "load_bpf_file: (%i) %s\n",
1343 map_fd[i], strerror(errno));
1351 static int __test_suite(char *bpf_file)
1355 err = populate_progs(bpf_file);
1357 fprintf(stderr, "ERROR: (%i) load bpf failed\n", err);
1361 if (setup_cgroup_environment()) {
1362 fprintf(stderr, "ERROR: cgroup env failed\n");
1366 cg_fd = create_and_get_cgroup(CG_PATH);
1369 "ERROR: (%i) open cg path failed: %s\n",
1374 if (join_cgroup(CG_PATH)) {
1375 fprintf(stderr, "ERROR: failed to join cgroup\n");
1379 /* Tests basic commands and APIs with range of iov values */
1380 txmsg_start = txmsg_end = 0;
1381 err = test_txmsg(cg_fd);
1385 /* Tests interesting combinations of APIs used together */
1386 err = test_mixed(cg_fd);
1390 /* Tests pull_data API using start/end API */
1391 err = test_start_end(cg_fd);
1396 printf("Summary: %i PASSED %i FAILED\n", passed, failed);
1397 cleanup_cgroup_environment();
1402 static int test_suite(void)
1406 err = __test_suite(BPF_SOCKMAP_FILENAME);
1409 err = __test_suite(BPF_SOCKHASH_FILENAME);
1414 int main(int argc, char **argv)
1416 int iov_count = 1, length = 1024, rate = 1;
1417 struct sockmap_options options = {0};
1418 int opt, longindex, err, cg_fd = 0;
1419 char *bpf_file = BPF_SOCKMAP_FILENAME;
1420 int test = PING_PONG;
1423 return test_suite();
1425 while ((opt = getopt_long(argc, argv, ":dhvc:r:i:l:t:",
1426 long_options, &longindex)) != -1) {
1429 txmsg_start = atoi(optarg);
1432 txmsg_end = atoi(optarg);
1435 txmsg_apply = atoi(optarg);
1438 txmsg_cork = atoi(optarg);
1441 cg_fd = open(optarg, O_DIRECTORY, O_RDONLY);
1444 "ERROR: (%i) open cg path failed: %s\n",
1450 rate = atoi(optarg);
1453 options.verbose = 1;
1456 iov_count = atoi(optarg);
1459 length = atoi(optarg);
1462 options.data_test = true;
1465 if (strcmp(optarg, "ping") == 0) {
1467 } else if (strcmp(optarg, "sendmsg") == 0) {
1469 } else if (strcmp(optarg, "base") == 0) {
1471 } else if (strcmp(optarg, "base_sendpage") == 0) {
1472 test = BASE_SENDPAGE;
1473 } else if (strcmp(optarg, "sendpage") == 0) {
1490 fprintf(stderr, "%s requires cgroup option: --cgroup <path>\n",
1495 err = populate_progs(bpf_file);
1497 fprintf(stderr, "populate program: (%s) %s\n",
1498 bpf_file, strerror(errno));
1504 signal(SIGINT, running_handler);
1506 options.iov_count = iov_count;
1507 options.iov_length = length;
1508 options.rate = rate;
1510 err = run_options(&options, cg_fd, test);
1515 void running_handler(int a)