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+");
252 perror("create file for sendpage");
255 for (i = 0; i < iov_length * cnt; i++, k++)
256 fwrite(&k, sizeof(char), 1, file);
258 fseek(file, 0, SEEK_SET);
261 fp = open(".sendpage_tst.tmp", O_RDONLY);
263 perror("reopen file for sendpage");
267 clock_gettime(CLOCK_MONOTONIC, &s->start);
268 for (i = 0; i < cnt; i++) {
269 int sent = sendfile(fd, fp, NULL, iov_length);
271 if (!drop && sent < 0) {
272 perror("send loop error:");
275 } else if (drop && sent >= 0) {
276 printf("sendpage loop error expected: %i\n", sent);
282 s->bytes_sent += sent;
284 clock_gettime(CLOCK_MONOTONIC, &s->end);
289 static int msg_loop(int fd, int iov_count, int iov_length, int cnt,
290 struct msg_stats *s, bool tx,
291 struct sockmap_options *opt)
293 struct msghdr msg = {0};
294 int err, i, flags = MSG_NOSIGNAL;
297 bool data_test = opt->data_test;
298 bool drop = opt->drop_expected;
300 iov = calloc(iov_count, sizeof(struct iovec));
305 for (i = 0; i < iov_count; i++) {
306 unsigned char *d = calloc(iov_length, sizeof(char));
309 fprintf(stderr, "iov_count %i/%i OOM\n", i, iov_count);
313 iov[i].iov_len = iov_length;
315 if (data_test && tx) {
318 for (j = 0; j < iov_length; j++)
324 msg.msg_iovlen = iov_count;
328 clock_gettime(CLOCK_MONOTONIC, &s->start);
329 for (i = 0; i < cnt; i++) {
330 int sent = sendmsg(fd, &msg, flags);
332 if (!drop && sent < 0) {
333 perror("send loop error:");
335 } else if (drop && sent >= 0) {
336 printf("send loop error expected: %i\n", sent);
341 s->bytes_sent += sent;
343 clock_gettime(CLOCK_MONOTONIC, &s->end);
345 int slct, recv, max_fd = fd;
346 int fd_flags = O_NONBLOCK;
347 struct timeval timeout;
354 chunk_sz = iov_length * cnt;
356 chunk_sz = iov_length * iov_count;
359 total_bytes = (float)iov_count * (float)iov_length * (float)cnt;
360 err = clock_gettime(CLOCK_MONOTONIC, &s->start);
362 perror("recv start time: ");
363 while (s->bytes_recvd < total_bytes) {
366 timeout.tv_usec = 300000;
376 slct = select(max_fd + 1, &w, NULL, NULL, &timeout);
379 clock_gettime(CLOCK_MONOTONIC, &s->end);
383 fprintf(stderr, "unexpected timeout\n");
385 clock_gettime(CLOCK_MONOTONIC, &s->end);
389 recv = recvmsg(fd, &msg, flags);
391 if (errno != EWOULDBLOCK) {
392 clock_gettime(CLOCK_MONOTONIC, &s->end);
393 perror("recv failed()\n");
398 s->bytes_recvd += recv;
403 for (i = 0; i < msg.msg_iovlen; i++) {
404 unsigned char *d = iov[i].iov_base;
407 j < iov[i].iov_len && recv; j++) {
411 "detected data corruption @iov[%i]:%i %02x != %02x, %02x ?= %02x\n",
412 i, j, d[j], k - 1, d[j+1], k);
416 if (bytes_cnt == chunk_sz) {
425 clock_gettime(CLOCK_MONOTONIC, &s->end);
428 for (i = 0; i < iov_count; i++)
429 free(iov[i].iov_base);
433 for (i = 0; i < iov_count; i++)
434 free(iov[i].iov_base);
439 static float giga = 1000000000;
441 static inline float sentBps(struct msg_stats s)
443 return s.bytes_sent / (s.end.tv_sec - s.start.tv_sec);
446 static inline float recvdBps(struct msg_stats s)
448 return s.bytes_recvd / (s.end.tv_sec - s.start.tv_sec);
451 static int sendmsg_test(struct sockmap_options *opt)
453 float sent_Bps = 0, recvd_Bps = 0;
454 int rx_fd, txpid, rxpid, err = 0;
455 struct msg_stats s = {0};
456 int iov_count = opt->iov_count;
457 int iov_buf = opt->iov_length;
458 int rx_status, tx_status;
470 if (opt->drop_expected)
475 err = msg_loop(rx_fd, iov_count, iov_buf,
476 cnt, &s, false, opt);
477 if (err && opt->verbose)
479 "msg_loop_rx: iov_count %i iov_buf %i cnt %i err %i\n",
480 iov_count, iov_buf, cnt, err);
481 shutdown(p2, SHUT_RDWR);
482 shutdown(p1, SHUT_RDWR);
483 if (s.end.tv_sec - s.start.tv_sec) {
484 sent_Bps = sentBps(s);
485 recvd_Bps = recvdBps(s);
489 "rx_sendmsg: TX: %zuB %fB/s %fGB/s RX: %zuB %fB/s %fGB/s\n",
490 s.bytes_sent, sent_Bps, sent_Bps/giga,
491 s.bytes_recvd, recvd_Bps, recvd_Bps/giga);
492 if (err && txmsg_cork)
495 } else if (rxpid == -1) {
496 perror("msg_loop_rx: ");
503 err = msg_loop_sendpage(c1, iov_buf, cnt, &s, opt);
505 err = msg_loop(c1, iov_count, iov_buf,
510 "msg_loop_tx: iov_count %i iov_buf %i cnt %i err %i\n",
511 iov_count, iov_buf, cnt, err);
512 shutdown(c1, SHUT_RDWR);
513 if (s.end.tv_sec - s.start.tv_sec) {
514 sent_Bps = sentBps(s);
515 recvd_Bps = recvdBps(s);
519 "tx_sendmsg: TX: %zuB %fB/s %f GB/s RX: %zuB %fB/s %fGB/s\n",
520 s.bytes_sent, sent_Bps, sent_Bps/giga,
521 s.bytes_recvd, recvd_Bps, recvd_Bps/giga);
523 } else if (txpid == -1) {
524 perror("msg_loop_tx: ");
528 assert(waitpid(rxpid, &rx_status, 0) == rxpid);
529 assert(waitpid(txpid, &tx_status, 0) == txpid);
530 if (WIFEXITED(rx_status)) {
531 err = WEXITSTATUS(rx_status);
533 fprintf(stderr, "rx thread exited with err %d. ", err);
537 if (WIFEXITED(tx_status)) {
538 err = WEXITSTATUS(tx_status);
540 fprintf(stderr, "tx thread exited with err %d. ", err);
546 static int forever_ping_pong(int rate, struct sockmap_options *opt)
548 struct timeval timeout;
549 char buf[1024] = {0};
555 /* Ping/Pong data from client to server */
556 sc = send(c1, buf, sizeof(buf), 0);
558 perror("send failed()\n");
563 int s, rc, i, max_fd = p2;
573 s = select(max_fd + 1, &w, NULL, NULL, &timeout);
578 fprintf(stderr, "unexpected timeout\n");
582 for (i = 0; i <= max_fd && s > 0; ++i) {
583 if (!FD_ISSET(i, &w))
588 rc = recv(i, buf, sizeof(buf), 0);
590 if (errno != EWOULDBLOCK) {
591 perror("recv failed()\n");
601 sc = send(i, buf, rc, 0);
603 perror("send failed()\n");
629 static int run_options(struct sockmap_options *options, int cg_fd, int test)
631 int i, key, next_key, err, tx_prog_fd = -1, zero = 0;
633 /* If base test skip BPF setup */
634 if (test == BASE || test == BASE_SENDPAGE)
637 /* Attach programs to sockmap */
638 err = bpf_prog_attach(prog_fd[0], map_fd[0],
639 BPF_SK_SKB_STREAM_PARSER, 0);
642 "ERROR: bpf_prog_attach (sockmap %i->%i): %d (%s)\n",
643 prog_fd[0], map_fd[0], err, strerror(errno));
647 err = bpf_prog_attach(prog_fd[1], map_fd[0],
648 BPF_SK_SKB_STREAM_VERDICT, 0);
650 fprintf(stderr, "ERROR: bpf_prog_attach (sockmap): %d (%s)\n",
651 err, strerror(errno));
655 /* Attach to cgroups */
656 err = bpf_prog_attach(prog_fd[2], cg_fd, BPF_CGROUP_SOCK_OPS, 0);
658 fprintf(stderr, "ERROR: bpf_prog_attach (groups): %d (%s)\n",
659 err, strerror(errno));
664 err = sockmap_init_sockets(options->verbose);
666 fprintf(stderr, "ERROR: test socket failed: %d\n", err);
670 /* Attach txmsg program to sockmap */
672 tx_prog_fd = prog_fd[3];
673 else if (txmsg_noisy)
674 tx_prog_fd = prog_fd[4];
675 else if (txmsg_redir)
676 tx_prog_fd = prog_fd[5];
677 else if (txmsg_redir_noisy)
678 tx_prog_fd = prog_fd[6];
680 tx_prog_fd = prog_fd[9];
681 /* apply and cork must be last */
682 else if (txmsg_apply)
683 tx_prog_fd = prog_fd[7];
685 tx_prog_fd = prog_fd[8];
692 err = bpf_prog_attach(tx_prog_fd,
693 map_fd[1], BPF_SK_MSG_VERDICT, 0);
696 "ERROR: bpf_prog_attach (txmsg): %d (%s)\n",
697 err, strerror(errno));
701 err = bpf_map_update_elem(map_fd[1], &i, &c1, BPF_ANY);
704 "ERROR: bpf_map_update_elem (txmsg): %d (%s\n",
705 err, strerror(errno));
709 if (txmsg_redir || txmsg_redir_noisy)
714 err = bpf_map_update_elem(map_fd[2], &i, &redir_fd, BPF_ANY);
717 "ERROR: bpf_map_update_elem (txmsg): %d (%s\n",
718 err, strerror(errno));
723 err = bpf_map_update_elem(map_fd[3],
724 &i, &txmsg_apply, BPF_ANY);
727 "ERROR: bpf_map_update_elem (apply_bytes): %d (%s\n",
728 err, strerror(errno));
734 err = bpf_map_update_elem(map_fd[4],
735 &i, &txmsg_cork, BPF_ANY);
738 "ERROR: bpf_map_update_elem (cork_bytes): %d (%s\n",
739 err, strerror(errno));
745 err = bpf_map_update_elem(map_fd[5],
746 &i, &txmsg_start, BPF_ANY);
749 "ERROR: bpf_map_update_elem (txmsg_start): %d (%s)\n",
750 err, strerror(errno));
757 err = bpf_map_update_elem(map_fd[5],
758 &i, &txmsg_end, BPF_ANY);
761 "ERROR: bpf_map_update_elem (txmsg_end): %d (%s)\n",
762 err, strerror(errno));
768 int in = BPF_F_INGRESS;
771 err = bpf_map_update_elem(map_fd[6], &i, &in, BPF_ANY);
774 "ERROR: bpf_map_update_elem (txmsg_ingress): %d (%s)\n",
775 err, strerror(errno));
778 err = bpf_map_update_elem(map_fd[1], &i, &p1, BPF_ANY);
781 "ERROR: bpf_map_update_elem (p1 txmsg): %d (%s)\n",
782 err, strerror(errno));
784 err = bpf_map_update_elem(map_fd[2], &i, &p1, BPF_ANY);
787 "ERROR: bpf_map_update_elem (p1 redir): %d (%s)\n",
788 err, strerror(errno));
792 err = bpf_map_update_elem(map_fd[2], &i, &p2, BPF_ANY);
795 "ERROR: bpf_map_update_elem (p2 txmsg): %d (%s)\n",
796 err, strerror(errno));
801 int skb_fd = (test == SENDMSG || test == SENDPAGE) ?
803 int ingress = BPF_F_INGRESS;
806 err = bpf_map_update_elem(map_fd[7],
807 &i, &ingress, BPF_ANY);
810 "ERROR: bpf_map_update_elem (txmsg_ingress): %d (%s)\n",
811 err, strerror(errno));
815 err = bpf_map_update_elem(map_fd[0],
816 &i, &skb_fd, BPF_ANY);
819 "ERROR: bpf_map_update_elem (c1 sockmap): %d (%s)\n",
820 err, strerror(errno));
826 options->drop_expected = true;
828 if (test == PING_PONG)
829 err = forever_ping_pong(options->rate, options);
830 else if (test == SENDMSG) {
831 options->base = false;
832 options->sendpage = false;
833 err = sendmsg_test(options);
834 } else if (test == SENDPAGE) {
835 options->base = false;
836 options->sendpage = true;
837 err = sendmsg_test(options);
838 } else if (test == BASE) {
839 options->base = true;
840 options->sendpage = false;
841 err = sendmsg_test(options);
842 } else if (test == BASE_SENDPAGE) {
843 options->base = true;
844 options->sendpage = true;
845 err = sendmsg_test(options);
847 fprintf(stderr, "unknown test\n");
849 /* Detatch and zero all the maps */
850 bpf_prog_detach2(prog_fd[2], cg_fd, BPF_CGROUP_SOCK_OPS);
851 bpf_prog_detach2(prog_fd[0], map_fd[0], BPF_SK_SKB_STREAM_PARSER);
852 bpf_prog_detach2(prog_fd[1], map_fd[0], BPF_SK_SKB_STREAM_VERDICT);
854 bpf_prog_detach2(tx_prog_fd, map_fd[1], BPF_SK_MSG_VERDICT);
856 for (i = 0; i < 8; i++) {
858 bpf_map_update_elem(map_fd[i], &key, &zero, BPF_ANY);
859 while (bpf_map_get_next_key(map_fd[i], &key, &next_key) == 0) {
860 bpf_map_update_elem(map_fd[i], &key, &zero, BPF_ANY);
874 static char *test_to_str(int test)
886 static void test_options(char *options)
888 char tstr[OPTSTRING];
890 memset(options, 0, OPTSTRING);
893 strncat(options, "pass,", OPTSTRING);
895 strncat(options, "pass_noisy,", OPTSTRING);
897 strncat(options, "redir,", OPTSTRING);
898 if (txmsg_redir_noisy)
899 strncat(options, "redir_noisy,", OPTSTRING);
901 strncat(options, "drop,", OPTSTRING);
903 snprintf(tstr, OPTSTRING, "apply %d,", txmsg_apply);
904 strncat(options, tstr, OPTSTRING);
907 snprintf(tstr, OPTSTRING, "cork %d,", txmsg_cork);
908 strncat(options, tstr, OPTSTRING);
911 snprintf(tstr, OPTSTRING, "start %d,", txmsg_start);
912 strncat(options, tstr, OPTSTRING);
915 snprintf(tstr, OPTSTRING, "end %d,", txmsg_end);
916 strncat(options, tstr, OPTSTRING);
919 strncat(options, "ingress,", OPTSTRING);
921 strncat(options, "skb,", OPTSTRING);
924 static int __test_exec(int cgrp, int test, struct sockmap_options *opt)
926 char *options = calloc(OPTSTRING, sizeof(char));
929 if (test == SENDPAGE)
930 opt->sendpage = true;
932 opt->sendpage = false;
935 opt->drop_expected = true;
937 opt->drop_expected = false;
939 test_options(options);
942 "[TEST %i]: (%i, %i, %i, %s, %s): ",
943 test_cnt, opt->rate, opt->iov_count, opt->iov_length,
944 test_to_str(test), options);
946 err = run_options(opt, cgrp, test);
947 fprintf(stdout, "%s\n", !err ? "PASS" : "FAILED");
949 !err ? passed++ : failed++;
954 static int test_exec(int cgrp, struct sockmap_options *opt)
956 int err = __test_exec(cgrp, SENDMSG, opt);
961 err = __test_exec(cgrp, SENDPAGE, opt);
966 static int test_loop(int cgrp)
968 struct sockmap_options opt;
974 opt.sendpage = false;
975 opt.data_test = false;
976 opt.drop_expected = false;
982 for (i = 1; i < 100; i += 33) {
983 for (l = 1; l < 100; l += 33) {
987 err = test_exec(cgrp, &opt);
997 static int test_txmsg(int cgrp)
1001 txmsg_pass = txmsg_noisy = txmsg_redir_noisy = txmsg_drop = 0;
1002 txmsg_apply = txmsg_cork = 0;
1003 txmsg_ingress = txmsg_skb = 0;
1006 err = test_loop(cgrp);
1012 err = test_loop(cgrp);
1018 err = test_loop(cgrp);
1025 err = test_loop(cgrp);
1037 static int test_send(struct sockmap_options *opt, int cgrp)
1041 opt->iov_length = 1;
1044 err = test_exec(cgrp, opt);
1048 opt->iov_length = 1;
1049 opt->iov_count = 1024;
1051 err = test_exec(cgrp, opt);
1055 opt->iov_length = 1024;
1058 err = test_exec(cgrp, opt);
1062 opt->iov_length = 1;
1065 err = test_exec(cgrp, opt);
1069 opt->iov_length = 256;
1070 opt->iov_count = 1024;
1072 err = test_exec(cgrp, opt);
1078 opt->iov_length = 5;
1079 err = test_exec(cgrp, opt);
1087 static int test_mixed(int cgrp)
1089 struct sockmap_options opt = {0};
1092 txmsg_pass = txmsg_noisy = txmsg_redir_noisy = txmsg_drop = 0;
1093 txmsg_apply = txmsg_cork = 0;
1094 txmsg_start = txmsg_end = 0;
1095 /* Test small and large iov_count values with pass/redir/apply/cork */
1100 err = test_send(&opt, cgrp);
1108 err = test_send(&opt, cgrp);
1116 err = test_send(&opt, cgrp);
1124 err = test_send(&opt, cgrp);
1132 err = test_send(&opt, cgrp);
1140 err = test_send(&opt, cgrp);
1148 err = test_send(&opt, cgrp);
1156 err = test_send(&opt, cgrp);
1164 err = test_send(&opt, cgrp);
1172 err = test_send(&opt, cgrp);
1180 err = test_send(&opt, cgrp);
1188 err = test_send(&opt, cgrp);
1196 err = test_send(&opt, cgrp);
1203 static int test_start_end(int cgrp)
1205 struct sockmap_options opt = {0};
1208 /* Test basic start/end with lots of iov_count and iov_lengths */
1211 err = test_txmsg(cgrp);
1215 /* Test start/end with cork */
1218 opt.iov_length = 100;
1221 for (i = 99; i <= 1600; i += 500) {
1224 err = test_exec(cgrp, &opt);
1229 /* Test start/end with cork but pull data in middle */
1230 for (i = 199; i <= 1600; i += 500) {
1233 err = test_exec(cgrp, &opt);
1238 /* Test start/end with cork pulling last sg entry */
1241 err = test_exec(cgrp, &opt);
1245 /* Test start/end pull of single byte in last page */
1248 err = test_exec(cgrp, &opt);
1252 /* Test start/end with end < start */
1255 err = test_exec(cgrp, &opt);
1259 /* Test start/end with end > data */
1262 err = test_exec(cgrp, &opt);
1266 /* Test start/end with start > data */
1269 err = test_exec(cgrp, &opt);
1278 char *map_names[] = {
1289 int prog_attach_type[] = {
1290 BPF_SK_SKB_STREAM_PARSER,
1291 BPF_SK_SKB_STREAM_VERDICT,
1292 BPF_CGROUP_SOCK_OPS,
1303 BPF_PROG_TYPE_SK_SKB,
1304 BPF_PROG_TYPE_SK_SKB,
1305 BPF_PROG_TYPE_SOCK_OPS,
1306 BPF_PROG_TYPE_SK_MSG,
1307 BPF_PROG_TYPE_SK_MSG,
1308 BPF_PROG_TYPE_SK_MSG,
1309 BPF_PROG_TYPE_SK_MSG,
1310 BPF_PROG_TYPE_SK_MSG,
1311 BPF_PROG_TYPE_SK_MSG,
1312 BPF_PROG_TYPE_SK_MSG,
1315 static int populate_progs(char *bpf_file)
1317 struct bpf_program *prog;
1318 struct bpf_object *obj;
1322 obj = bpf_object__open(bpf_file);
1323 err = libbpf_get_error(obj);
1327 libbpf_strerror(err, err_buf, sizeof(err_buf));
1328 printf("Unable to load eBPF objects in file '%s' : %s\n",
1333 bpf_object__for_each_program(prog, obj) {
1334 bpf_program__set_type(prog, prog_type[i]);
1335 bpf_program__set_expected_attach_type(prog,
1336 prog_attach_type[i]);
1340 i = bpf_object__load(obj);
1342 bpf_object__for_each_program(prog, obj) {
1343 prog_fd[i] = bpf_program__fd(prog);
1347 for (i = 0; i < sizeof(map_fd)/sizeof(int); i++) {
1348 maps[i] = bpf_object__find_map_by_name(obj, map_names[i]);
1349 map_fd[i] = bpf_map__fd(maps[i]);
1350 if (map_fd[i] < 0) {
1351 fprintf(stderr, "load_bpf_file: (%i) %s\n",
1352 map_fd[i], strerror(errno));
1360 static int __test_suite(char *bpf_file)
1364 err = populate_progs(bpf_file);
1366 fprintf(stderr, "ERROR: (%i) load bpf failed\n", err);
1370 if (setup_cgroup_environment()) {
1371 fprintf(stderr, "ERROR: cgroup env failed\n");
1375 cg_fd = create_and_get_cgroup(CG_PATH);
1378 "ERROR: (%i) open cg path failed: %s\n",
1383 if (join_cgroup(CG_PATH)) {
1384 fprintf(stderr, "ERROR: failed to join cgroup\n");
1388 /* Tests basic commands and APIs with range of iov values */
1389 txmsg_start = txmsg_end = 0;
1390 err = test_txmsg(cg_fd);
1394 /* Tests interesting combinations of APIs used together */
1395 err = test_mixed(cg_fd);
1399 /* Tests pull_data API using start/end API */
1400 err = test_start_end(cg_fd);
1405 printf("Summary: %i PASSED %i FAILED\n", passed, failed);
1406 cleanup_cgroup_environment();
1411 static int test_suite(void)
1415 err = __test_suite(BPF_SOCKMAP_FILENAME);
1418 err = __test_suite(BPF_SOCKHASH_FILENAME);
1423 int main(int argc, char **argv)
1425 int iov_count = 1, length = 1024, rate = 1;
1426 struct sockmap_options options = {0};
1427 int opt, longindex, err, cg_fd = 0;
1428 char *bpf_file = BPF_SOCKMAP_FILENAME;
1429 int test = PING_PONG;
1432 return test_suite();
1434 while ((opt = getopt_long(argc, argv, ":dhvc:r:i:l:t:",
1435 long_options, &longindex)) != -1) {
1438 txmsg_start = atoi(optarg);
1441 txmsg_end = atoi(optarg);
1444 txmsg_apply = atoi(optarg);
1447 txmsg_cork = atoi(optarg);
1450 cg_fd = open(optarg, O_DIRECTORY, O_RDONLY);
1453 "ERROR: (%i) open cg path failed: %s\n",
1459 rate = atoi(optarg);
1462 options.verbose = 1;
1465 iov_count = atoi(optarg);
1468 length = atoi(optarg);
1471 options.data_test = true;
1474 if (strcmp(optarg, "ping") == 0) {
1476 } else if (strcmp(optarg, "sendmsg") == 0) {
1478 } else if (strcmp(optarg, "base") == 0) {
1480 } else if (strcmp(optarg, "base_sendpage") == 0) {
1481 test = BASE_SENDPAGE;
1482 } else if (strcmp(optarg, "sendpage") == 0) {
1499 fprintf(stderr, "%s requires cgroup option: --cgroup <path>\n",
1504 err = populate_progs(bpf_file);
1506 fprintf(stderr, "populate program: (%s) %s\n",
1507 bpf_file, strerror(errno));
1513 signal(SIGINT, running_handler);
1515 options.iov_count = iov_count;
1516 options.iov_length = length;
1517 options.rate = rate;
1519 err = run_options(&options, cg_fd, test);
1524 void running_handler(int a)