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>
21 #include <sys/types.h>
22 #include <sys/sendfile.h>
24 #include <linux/netlink.h>
25 #include <linux/socket.h>
26 #include <linux/sock_diag.h>
27 #include <linux/bpf.h>
28 #include <linux/if_link.h>
29 #include <linux/tls.h>
36 #include <bpf/libbpf.h>
39 #include "cgroup_helpers.h"
42 static void running_handler(int a);
51 /* randomly selected ports for testing on lo */
55 #define BPF_SOCKMAP_FILENAME "test_sockmap_kern.bpf.o"
56 #define BPF_SOCKHASH_FILENAME "test_sockhash_kern.bpf.o"
57 #define CG_PATH "/sockmap"
60 int s1, s2, c1, c2, p1, p2;
65 struct bpf_map *maps[9];
82 int txmsg_ktls_skb_drop;
83 int txmsg_ktls_skb_redir;
87 int txmsg_omit_skb_parser;
89 static const struct option long_options[] = {
90 {"help", no_argument, NULL, 'h' },
91 {"cgroup", required_argument, NULL, 'c' },
92 {"rate", required_argument, NULL, 'r' },
93 {"verbose", optional_argument, NULL, 'v' },
94 {"iov_count", required_argument, NULL, 'i' },
95 {"length", required_argument, NULL, 'l' },
96 {"test", required_argument, NULL, 't' },
97 {"data_test", no_argument, NULL, 'd' },
98 {"txmsg", no_argument, &txmsg_pass, 1 },
99 {"txmsg_redir", no_argument, &txmsg_redir, 1 },
100 {"txmsg_drop", no_argument, &txmsg_drop, 1 },
101 {"txmsg_apply", required_argument, NULL, 'a'},
102 {"txmsg_cork", required_argument, NULL, 'k'},
103 {"txmsg_start", required_argument, NULL, 's'},
104 {"txmsg_end", required_argument, NULL, 'e'},
105 {"txmsg_start_push", required_argument, NULL, 'p'},
106 {"txmsg_end_push", required_argument, NULL, 'q'},
107 {"txmsg_start_pop", required_argument, NULL, 'w'},
108 {"txmsg_pop", required_argument, NULL, 'x'},
109 {"txmsg_ingress", no_argument, &txmsg_ingress, 1 },
110 {"txmsg_redir_skb", no_argument, &txmsg_redir_skb, 1 },
111 {"ktls", no_argument, &ktls, 1 },
112 {"peek", no_argument, &peek_flag, 1 },
113 {"txmsg_omit_skb_parser", no_argument, &txmsg_omit_skb_parser, 1},
114 {"whitelist", required_argument, NULL, 'n' },
115 {"blacklist", required_argument, NULL, 'b' },
134 struct sockmap_options {
140 bool check_recved_len;
152 void (*tester)(int cg_fd, struct sockmap_options *opt);
155 static void test_start(void)
160 static void test_fail(void)
165 static void test_pass(void)
170 static void test_reset(void)
172 txmsg_start = txmsg_end = 0;
173 txmsg_start_pop = txmsg_pop = 0;
174 txmsg_start_push = txmsg_end_push = 0;
175 txmsg_pass = txmsg_drop = txmsg_redir = 0;
176 txmsg_apply = txmsg_cork = 0;
177 txmsg_ingress = txmsg_redir_skb = 0;
178 txmsg_ktls_skb = txmsg_ktls_skb_drop = txmsg_ktls_skb_redir = 0;
179 txmsg_omit_skb_parser = 0;
183 static int test_start_subtest(const struct _test *t, struct sockmap_options *o)
186 env.subtest = t->title;
187 env.prepend = o->prepend;
190 env.fail_last = env.fail_cnt;
195 static void test_end_subtest(void)
197 int error = env.fail_cnt - env.fail_last;
198 int type = strcmp(env.type, BPF_SOCKMAP_FILENAME);
203 fprintf(stdout, "#%2d/%2d %8s:%s:%s:%s\n",
204 env.test_num, env.subtest_num,
205 !type ? "sockmap" : "sockhash",
207 env.subtest, error ? "FAIL" : "OK");
210 static void test_print_results(void)
212 fprintf(stdout, "Pass: %d Fail: %d\n",
213 env.succ_cnt, env.fail_cnt);
216 static void usage(char *argv[])
220 printf(" Usage: %s --cgroup <cgroup_path>\n", argv[0]);
221 printf(" options:\n");
222 for (i = 0; long_options[i].name != 0; i++) {
223 printf(" --%-12s", long_options[i].name);
224 if (long_options[i].flag != NULL)
225 printf(" flag (internal value:%d)\n",
226 *long_options[i].flag);
228 printf(" -%c\n", long_options[i].val);
233 char *sock_to_string(int s)
251 static int sockmap_init_ktls(int verbose, int s)
253 struct tls12_crypto_info_aes_gcm_128 tls_tx = {
255 .version = TLS_1_2_VERSION,
256 .cipher_type = TLS_CIPHER_AES_GCM_128,
259 struct tls12_crypto_info_aes_gcm_128 tls_rx = {
261 .version = TLS_1_2_VERSION,
262 .cipher_type = TLS_CIPHER_AES_GCM_128,
265 int so_buf = 6553500;
268 err = setsockopt(s, 6, TCP_ULP, "tls", sizeof("tls"));
270 fprintf(stderr, "setsockopt: TCP_ULP(%s) failed with error %i\n", sock_to_string(s), err);
273 err = setsockopt(s, SOL_TLS, TLS_TX, (void *)&tls_tx, sizeof(tls_tx));
275 fprintf(stderr, "setsockopt: TLS_TX(%s) failed with error %i\n", sock_to_string(s), err);
278 err = setsockopt(s, SOL_TLS, TLS_RX, (void *)&tls_rx, sizeof(tls_rx));
280 fprintf(stderr, "setsockopt: TLS_RX(%s) failed with error %i\n", sock_to_string(s), err);
283 err = setsockopt(s, SOL_SOCKET, SO_SNDBUF, &so_buf, sizeof(so_buf));
285 fprintf(stderr, "setsockopt: (%s) failed sndbuf with error %i\n", sock_to_string(s), err);
288 err = setsockopt(s, SOL_SOCKET, SO_RCVBUF, &so_buf, sizeof(so_buf));
290 fprintf(stderr, "setsockopt: (%s) failed rcvbuf with error %i\n", sock_to_string(s), err);
295 fprintf(stdout, "socket(%s) kTLS enabled\n", sock_to_string(s));
298 static int sockmap_init_sockets(int verbose)
301 struct sockaddr_in addr;
302 int *fds[4] = {&s1, &s2, &c1, &c2};
304 s1 = s2 = p1 = p2 = c1 = c2 = 0;
307 for (i = 0; i < 4; i++) {
308 *fds[i] = socket(AF_INET, SOCK_STREAM, 0);
310 perror("socket s1 failed()");
316 for (i = 0; i < 2; i++) {
317 err = setsockopt(*fds[i], SOL_SOCKET, SO_REUSEADDR,
318 (char *)&one, sizeof(one));
320 perror("setsockopt failed()");
325 /* Non-blocking sockets */
326 for (i = 0; i < 2; i++) {
327 err = ioctl(*fds[i], FIONBIO, (char *)&one);
329 perror("ioctl s1 failed()");
334 /* Bind server sockets */
335 memset(&addr, 0, sizeof(struct sockaddr_in));
336 addr.sin_family = AF_INET;
337 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
339 addr.sin_port = htons(S1_PORT);
340 err = bind(s1, (struct sockaddr *)&addr, sizeof(addr));
342 perror("bind s1 failed()");
346 addr.sin_port = htons(S2_PORT);
347 err = bind(s2, (struct sockaddr *)&addr, sizeof(addr));
349 perror("bind s2 failed()");
353 /* Listen server sockets */
354 addr.sin_port = htons(S1_PORT);
355 err = listen(s1, 32);
357 perror("listen s1 failed()");
361 addr.sin_port = htons(S2_PORT);
362 err = listen(s2, 32);
364 perror("listen s1 failed()");
368 /* Initiate Connect */
369 addr.sin_port = htons(S1_PORT);
370 err = connect(c1, (struct sockaddr *)&addr, sizeof(addr));
371 if (err < 0 && errno != EINPROGRESS) {
372 perror("connect c1 failed()");
376 addr.sin_port = htons(S2_PORT);
377 err = connect(c2, (struct sockaddr *)&addr, sizeof(addr));
378 if (err < 0 && errno != EINPROGRESS) {
379 perror("connect c2 failed()");
381 } else if (err < 0) {
385 /* Accept Connecrtions */
386 p1 = accept(s1, NULL, NULL);
388 perror("accept s1 failed()");
392 p2 = accept(s2, NULL, NULL);
394 perror("accept s1 failed()");
399 printf("connected sockets: c1 <-> p1, c2 <-> p2\n");
400 printf("cgroups binding: c1(%i) <-> s1(%i) - - - c2(%i) <-> s2(%i)\n",
409 struct timespec start;
413 static int msg_loop_sendpage(int fd, int iov_length, int cnt,
415 struct sockmap_options *opt)
417 bool drop = opt->drop_expected;
424 perror("create file for sendpage");
427 for (i = 0; i < iov_length * cnt; i++, k++)
428 fwrite(&k, sizeof(char), 1, file);
430 fseek(file, 0, SEEK_SET);
434 clock_gettime(CLOCK_MONOTONIC, &s->start);
435 for (i = 0; i < cnt; i++) {
439 sent = sendfile(fd, fp, NULL, iov_length);
441 if (!drop && sent < 0) {
442 perror("sendpage loop error");
445 } else if (drop && sent >= 0) {
446 printf("sendpage loop error expected: %i errno %i\n",
453 s->bytes_sent += sent;
455 clock_gettime(CLOCK_MONOTONIC, &s->end);
460 static void msg_free_iov(struct msghdr *msg)
464 for (i = 0; i < msg->msg_iovlen; i++)
465 free(msg->msg_iov[i].iov_base);
471 static int msg_alloc_iov(struct msghdr *msg,
472 int iov_count, int iov_length,
473 bool data, bool xmit)
479 iov = calloc(iov_count, sizeof(struct iovec));
483 for (i = 0; i < iov_count; i++) {
484 unsigned char *d = calloc(iov_length, sizeof(char));
487 fprintf(stderr, "iov_count %i/%i OOM\n", i, iov_count);
491 iov[i].iov_len = iov_length;
496 for (j = 0; j < iov_length; j++)
502 msg->msg_iovlen = iov_count;
506 for (i--; i >= 0 ; i--)
507 free(msg->msg_iov[i].iov_base);
511 static int msg_verify_data(struct msghdr *msg, int size, int chunk_sz)
513 int i, j = 0, bytes_cnt = 0;
516 for (i = 0; i < msg->msg_iovlen; i++) {
517 unsigned char *d = msg->msg_iov[i].iov_base;
519 /* Special case test for skb ingress + ktls */
520 if (i == 0 && txmsg_ktls_skb) {
521 if (msg->msg_iov[i].iov_len < 4)
523 if (memcmp(d, "PASS", 4) != 0) {
525 "detected skb data error with skb ingress update @iov[%i]:%i \"%02x %02x %02x %02x\" != \"PASS\"\n",
526 i, 0, d[0], d[1], d[2], d[3]);
529 j = 4; /* advance index past PASS header */
532 for (; j < msg->msg_iov[i].iov_len && size; j++) {
535 "detected data corruption @iov[%i]:%i %02x != %02x, %02x ?= %02x\n",
536 i, j, d[j], k - 1, d[j+1], k);
540 if (bytes_cnt == chunk_sz) {
550 static int msg_loop(int fd, int iov_count, int iov_length, int cnt,
551 struct msg_stats *s, bool tx,
552 struct sockmap_options *opt)
554 struct msghdr msg = {0}, msg_peek = {0};
555 int err, i, flags = MSG_NOSIGNAL;
556 bool drop = opt->drop_expected;
557 bool data = opt->data_test;
558 int iov_alloc_length = iov_length;
560 if (!tx && opt->check_recved_len)
561 iov_alloc_length *= 2;
563 err = msg_alloc_iov(&msg, iov_count, iov_alloc_length, data, tx);
567 err = msg_alloc_iov(&msg_peek, iov_count, iov_length, data, tx);
573 clock_gettime(CLOCK_MONOTONIC, &s->start);
574 for (i = 0; i < cnt; i++) {
578 sent = sendmsg(fd, &msg, flags);
580 if (!drop && sent < 0) {
581 perror("sendmsg loop error");
583 } else if (drop && sent >= 0) {
585 "sendmsg loop error expected: %i errno %i\n",
591 s->bytes_sent += sent;
593 clock_gettime(CLOCK_MONOTONIC, &s->end);
595 int slct, recvp = 0, recv, max_fd = fd;
596 float total_bytes, txmsg_pop_total;
597 int fd_flags = O_NONBLOCK;
598 struct timeval timeout;
602 /* Account for pop bytes noting each iteration of apply will
603 * call msg_pop_data helper so we need to account for this
604 * by calculating the number of apply iterations. Note user
605 * of the tool can create cases where no data is sent by
606 * manipulating pop/push/pull/etc. For example txmsg_apply 1
607 * with txmsg_pop 1 will try to apply 1B at a time but each
608 * iteration will then pop 1B so no data will ever be sent.
609 * This is really only useful for testing edge cases in code
612 total_bytes = (float)iov_count * (float)iov_length * (float)cnt;
614 txmsg_pop_total = txmsg_pop * (total_bytes / txmsg_apply);
616 txmsg_pop_total = txmsg_pop * cnt;
617 total_bytes -= txmsg_pop_total;
618 err = clock_gettime(CLOCK_MONOTONIC, &s->start);
620 perror("recv start time");
621 while (s->bytes_recvd < total_bytes) {
624 timeout.tv_usec = 300000;
634 slct = select(max_fd + 1, &w, NULL, NULL, &timeout);
637 clock_gettime(CLOCK_MONOTONIC, &s->end);
641 fprintf(stderr, "unexpected timeout: recved %zu/%f pop_total %f\n", s->bytes_recvd, total_bytes, txmsg_pop_total);
643 clock_gettime(CLOCK_MONOTONIC, &s->end);
650 recvp = recvmsg(fd, &msg_peek, flags);
652 if (errno != EWOULDBLOCK) {
653 clock_gettime(CLOCK_MONOTONIC, &s->end);
660 recv = recvmsg(fd, &msg, flags);
662 if (errno != EWOULDBLOCK) {
663 clock_gettime(CLOCK_MONOTONIC, &s->end);
664 perror("recv failed()");
669 s->bytes_recvd += recv;
671 if (opt->check_recved_len && s->bytes_recvd > total_bytes) {
673 fprintf(stderr, "recv failed(), bytes_recvd:%zd, total_bytes:%f\n",
674 s->bytes_recvd, total_bytes);
679 int chunk_sz = opt->sendpage ?
681 iov_length * iov_count;
683 errno = msg_verify_data(&msg, recv, chunk_sz);
685 perror("data verify msg failed");
689 errno = msg_verify_data(&msg_peek,
693 perror("data verify msg_peek failed");
699 clock_gettime(CLOCK_MONOTONIC, &s->end);
703 msg_free_iov(&msg_peek);
707 msg_free_iov(&msg_peek);
711 static float giga = 1000000000;
713 static inline float sentBps(struct msg_stats s)
715 return s.bytes_sent / (s.end.tv_sec - s.start.tv_sec);
718 static inline float recvdBps(struct msg_stats s)
720 return s.bytes_recvd / (s.end.tv_sec - s.start.tv_sec);
723 static int sendmsg_test(struct sockmap_options *opt)
725 float sent_Bps = 0, recvd_Bps = 0;
726 int rx_fd, txpid, rxpid, err = 0;
727 struct msg_stats s = {0};
728 int iov_count = opt->iov_count;
729 int iov_buf = opt->iov_length;
730 int rx_status, tx_status;
741 /* Redirecting into non-TLS socket which sends into a TLS
742 * socket is not a valid test. So in this case lets not
743 * enable kTLS but still run the test.
745 if (!txmsg_redir || txmsg_ingress) {
746 err = sockmap_init_ktls(opt->verbose, rx_fd);
750 err = sockmap_init_ktls(opt->verbose, c1);
757 if (txmsg_pop || txmsg_start_pop)
758 iov_buf -= (txmsg_pop - txmsg_start_pop + 1);
759 if (opt->drop_expected || txmsg_ktls_skb_drop)
762 if (!iov_buf) /* zero bytes sent case */
767 err = msg_loop(rx_fd, iov_count, iov_buf,
768 cnt, &s, false, opt);
769 if (opt->verbose > 1)
771 "msg_loop_rx: iov_count %i iov_buf %i cnt %i err %i\n",
772 iov_count, iov_buf, cnt, err);
773 if (s.end.tv_sec - s.start.tv_sec) {
774 sent_Bps = sentBps(s);
775 recvd_Bps = recvdBps(s);
777 if (opt->verbose > 1)
779 "rx_sendmsg: TX: %zuB %fB/s %fGB/s RX: %zuB %fB/s %fGB/s %s\n",
780 s.bytes_sent, sent_Bps, sent_Bps/giga,
781 s.bytes_recvd, recvd_Bps, recvd_Bps/giga,
782 peek_flag ? "(peek_msg)" : "");
783 if (err && txmsg_cork)
786 } else if (rxpid == -1) {
787 perror("msg_loop_rx");
794 err = msg_loop_sendpage(c1, iov_buf, cnt, &s, opt);
796 err = msg_loop(c1, iov_count, iov_buf,
801 "msg_loop_tx: iov_count %i iov_buf %i cnt %i err %i\n",
802 iov_count, iov_buf, cnt, err);
803 if (s.end.tv_sec - s.start.tv_sec) {
804 sent_Bps = sentBps(s);
805 recvd_Bps = recvdBps(s);
807 if (opt->verbose > 1)
809 "tx_sendmsg: TX: %zuB %fB/s %f GB/s RX: %zuB %fB/s %fGB/s\n",
810 s.bytes_sent, sent_Bps, sent_Bps/giga,
811 s.bytes_recvd, recvd_Bps, recvd_Bps/giga);
813 } else if (txpid == -1) {
814 perror("msg_loop_tx");
818 assert(waitpid(rxpid, &rx_status, 0) == rxpid);
819 assert(waitpid(txpid, &tx_status, 0) == txpid);
820 if (WIFEXITED(rx_status)) {
821 err = WEXITSTATUS(rx_status);
823 fprintf(stderr, "rx thread exited with err %d.\n", err);
827 if (WIFEXITED(tx_status)) {
828 err = WEXITSTATUS(tx_status);
830 fprintf(stderr, "tx thread exited with err %d.\n", err);
836 static int forever_ping_pong(int rate, struct sockmap_options *opt)
838 struct timeval timeout;
839 char buf[1024] = {0};
845 /* Ping/Pong data from client to server */
846 sc = send(c1, buf, sizeof(buf), 0);
848 perror("send failed()");
853 int s, rc, i, max_fd = p2;
863 s = select(max_fd + 1, &w, NULL, NULL, &timeout);
868 fprintf(stderr, "unexpected timeout\n");
872 for (i = 0; i <= max_fd && s > 0; ++i) {
873 if (!FD_ISSET(i, &w))
878 rc = recv(i, buf, sizeof(buf), 0);
880 if (errno != EWOULDBLOCK) {
881 perror("recv failed()");
891 sc = send(i, buf, rc, 0);
893 perror("send failed()");
920 static int run_options(struct sockmap_options *options, int cg_fd, int test)
922 int i, key, next_key, err, tx_prog_fd = -1, zero = 0;
924 /* If base test skip BPF setup */
925 if (test == BASE || test == BASE_SENDPAGE)
928 /* Attach programs to sockmap */
929 if (!txmsg_omit_skb_parser) {
930 err = bpf_prog_attach(prog_fd[0], map_fd[0],
931 BPF_SK_SKB_STREAM_PARSER, 0);
934 "ERROR: bpf_prog_attach (sockmap %i->%i): %d (%s)\n",
935 prog_fd[0], map_fd[0], err, strerror(errno));
940 err = bpf_prog_attach(prog_fd[1], map_fd[0],
941 BPF_SK_SKB_STREAM_VERDICT, 0);
943 fprintf(stderr, "ERROR: bpf_prog_attach (sockmap): %d (%s)\n",
944 err, strerror(errno));
948 /* Attach programs to TLS sockmap */
949 if (txmsg_ktls_skb) {
950 if (!txmsg_omit_skb_parser) {
951 err = bpf_prog_attach(prog_fd[0], map_fd[8],
952 BPF_SK_SKB_STREAM_PARSER, 0);
955 "ERROR: bpf_prog_attach (TLS sockmap %i->%i): %d (%s)\n",
956 prog_fd[0], map_fd[8], err, strerror(errno));
961 err = bpf_prog_attach(prog_fd[2], map_fd[8],
962 BPF_SK_SKB_STREAM_VERDICT, 0);
964 fprintf(stderr, "ERROR: bpf_prog_attach (TLS sockmap): %d (%s)\n",
965 err, strerror(errno));
970 /* Attach to cgroups */
971 err = bpf_prog_attach(prog_fd[3], cg_fd, BPF_CGROUP_SOCK_OPS, 0);
973 fprintf(stderr, "ERROR: bpf_prog_attach (groups): %d (%s)\n",
974 err, strerror(errno));
979 err = sockmap_init_sockets(options->verbose);
981 fprintf(stderr, "ERROR: test socket failed: %d\n", err);
985 /* Attach txmsg program to sockmap */
987 tx_prog_fd = prog_fd[4];
988 else if (txmsg_redir)
989 tx_prog_fd = prog_fd[5];
990 else if (txmsg_apply)
991 tx_prog_fd = prog_fd[6];
993 tx_prog_fd = prog_fd[7];
995 tx_prog_fd = prog_fd[8];
1000 int redir_fd, i = 0;
1002 err = bpf_prog_attach(tx_prog_fd,
1003 map_fd[1], BPF_SK_MSG_VERDICT, 0);
1006 "ERROR: bpf_prog_attach (txmsg): %d (%s)\n",
1007 err, strerror(errno));
1011 err = bpf_map_update_elem(map_fd[1], &i, &c1, BPF_ANY);
1014 "ERROR: bpf_map_update_elem (txmsg): %d (%s\n",
1015 err, strerror(errno));
1024 err = bpf_map_update_elem(map_fd[2], &i, &redir_fd, BPF_ANY);
1027 "ERROR: bpf_map_update_elem (txmsg): %d (%s\n",
1028 err, strerror(errno));
1033 err = bpf_map_update_elem(map_fd[3],
1034 &i, &txmsg_apply, BPF_ANY);
1037 "ERROR: bpf_map_update_elem (apply_bytes): %d (%s\n",
1038 err, strerror(errno));
1044 err = bpf_map_update_elem(map_fd[4],
1045 &i, &txmsg_cork, BPF_ANY);
1048 "ERROR: bpf_map_update_elem (cork_bytes): %d (%s\n",
1049 err, strerror(errno));
1055 err = bpf_map_update_elem(map_fd[5],
1056 &i, &txmsg_start, BPF_ANY);
1059 "ERROR: bpf_map_update_elem (txmsg_start): %d (%s)\n",
1060 err, strerror(errno));
1067 err = bpf_map_update_elem(map_fd[5],
1068 &i, &txmsg_end, BPF_ANY);
1071 "ERROR: bpf_map_update_elem (txmsg_end): %d (%s)\n",
1072 err, strerror(errno));
1077 if (txmsg_start_push) {
1079 err = bpf_map_update_elem(map_fd[5],
1080 &i, &txmsg_start_push, BPF_ANY);
1083 "ERROR: bpf_map_update_elem (txmsg_start_push): %d (%s)\n",
1084 err, strerror(errno));
1089 if (txmsg_end_push) {
1091 err = bpf_map_update_elem(map_fd[5],
1092 &i, &txmsg_end_push, BPF_ANY);
1095 "ERROR: bpf_map_update_elem %i@%i (txmsg_end_push): %d (%s)\n",
1096 txmsg_end_push, i, err, strerror(errno));
1101 if (txmsg_start_pop) {
1103 err = bpf_map_update_elem(map_fd[5],
1104 &i, &txmsg_start_pop, BPF_ANY);
1107 "ERROR: bpf_map_update_elem %i@%i (txmsg_start_pop): %d (%s)\n",
1108 txmsg_start_pop, i, err, strerror(errno));
1113 bpf_map_update_elem(map_fd[5],
1114 &i, &txmsg_start_pop, BPF_ANY);
1119 err = bpf_map_update_elem(map_fd[5],
1120 &i, &txmsg_pop, BPF_ANY);
1123 "ERROR: bpf_map_update_elem %i@%i (txmsg_pop): %d (%s)\n",
1124 txmsg_pop, i, err, strerror(errno));
1129 bpf_map_update_elem(map_fd[5],
1130 &i, &txmsg_pop, BPF_ANY);
1134 if (txmsg_ingress) {
1135 int in = BPF_F_INGRESS;
1138 err = bpf_map_update_elem(map_fd[6], &i, &in, BPF_ANY);
1141 "ERROR: bpf_map_update_elem (txmsg_ingress): %d (%s)\n",
1142 err, strerror(errno));
1145 err = bpf_map_update_elem(map_fd[1], &i, &p1, BPF_ANY);
1148 "ERROR: bpf_map_update_elem (p1 txmsg): %d (%s)\n",
1149 err, strerror(errno));
1151 err = bpf_map_update_elem(map_fd[2], &i, &p1, BPF_ANY);
1154 "ERROR: bpf_map_update_elem (p1 redir): %d (%s)\n",
1155 err, strerror(errno));
1159 err = bpf_map_update_elem(map_fd[2], &i, &p2, BPF_ANY);
1162 "ERROR: bpf_map_update_elem (p2 txmsg): %d (%s)\n",
1163 err, strerror(errno));
1167 if (txmsg_ktls_skb) {
1168 int ingress = BPF_F_INGRESS;
1171 err = bpf_map_update_elem(map_fd[8], &i, &p2, BPF_ANY);
1174 "ERROR: bpf_map_update_elem (c1 sockmap): %d (%s)\n",
1175 err, strerror(errno));
1178 if (txmsg_ktls_skb_redir) {
1180 err = bpf_map_update_elem(map_fd[7],
1181 &i, &ingress, BPF_ANY);
1184 "ERROR: bpf_map_update_elem (txmsg_ingress): %d (%s)\n",
1185 err, strerror(errno));
1189 if (txmsg_ktls_skb_drop) {
1191 err = bpf_map_update_elem(map_fd[7], &i, &i, BPF_ANY);
1195 if (txmsg_redir_skb) {
1196 int skb_fd = (test == SENDMSG || test == SENDPAGE) ?
1198 int ingress = BPF_F_INGRESS;
1201 err = bpf_map_update_elem(map_fd[7],
1202 &i, &ingress, BPF_ANY);
1205 "ERROR: bpf_map_update_elem (txmsg_ingress): %d (%s)\n",
1206 err, strerror(errno));
1210 err = bpf_map_update_elem(map_fd[0], &i, &skb_fd, BPF_ANY);
1213 "ERROR: bpf_map_update_elem (c1 sockmap): %d (%s)\n",
1214 err, strerror(errno));
1219 if (skb_use_parser) {
1221 err = bpf_map_update_elem(map_fd[7], &i, &skb_use_parser, BPF_ANY);
1225 options->drop_expected = true;
1227 if (test == PING_PONG)
1228 err = forever_ping_pong(options->rate, options);
1229 else if (test == SENDMSG) {
1230 options->base = false;
1231 options->sendpage = false;
1232 err = sendmsg_test(options);
1233 } else if (test == SENDPAGE) {
1234 options->base = false;
1235 options->sendpage = true;
1236 err = sendmsg_test(options);
1237 } else if (test == BASE) {
1238 options->base = true;
1239 options->sendpage = false;
1240 err = sendmsg_test(options);
1241 } else if (test == BASE_SENDPAGE) {
1242 options->base = true;
1243 options->sendpage = true;
1244 err = sendmsg_test(options);
1246 fprintf(stderr, "unknown test\n");
1248 /* Detatch and zero all the maps */
1249 bpf_prog_detach2(prog_fd[3], cg_fd, BPF_CGROUP_SOCK_OPS);
1250 bpf_prog_detach2(prog_fd[0], map_fd[0], BPF_SK_SKB_STREAM_PARSER);
1251 bpf_prog_detach2(prog_fd[1], map_fd[0], BPF_SK_SKB_STREAM_VERDICT);
1252 bpf_prog_detach2(prog_fd[0], map_fd[8], BPF_SK_SKB_STREAM_PARSER);
1253 bpf_prog_detach2(prog_fd[2], map_fd[8], BPF_SK_SKB_STREAM_VERDICT);
1255 if (tx_prog_fd >= 0)
1256 bpf_prog_detach2(tx_prog_fd, map_fd[1], BPF_SK_MSG_VERDICT);
1258 for (i = 0; i < 8; i++) {
1260 bpf_map_update_elem(map_fd[i], &key, &zero, BPF_ANY);
1261 while (bpf_map_get_next_key(map_fd[i], &key, &next_key) == 0) {
1262 bpf_map_update_elem(map_fd[i], &key, &zero, BPF_ANY);
1276 static char *test_to_str(int test)
1287 static void append_str(char *dst, const char *src, size_t dst_cap)
1289 size_t avail = dst_cap - strlen(dst);
1291 if (avail <= 1) /* just zero byte could be written */
1294 strncat(dst, src, avail - 1); /* strncat() adds + 1 for zero byte */
1297 #define OPTSTRING 60
1298 static void test_options(char *options)
1300 char tstr[OPTSTRING];
1302 memset(options, 0, OPTSTRING);
1305 append_str(options, "pass,", OPTSTRING);
1307 append_str(options, "redir,", OPTSTRING);
1309 append_str(options, "drop,", OPTSTRING);
1311 snprintf(tstr, OPTSTRING, "apply %d,", txmsg_apply);
1312 append_str(options, tstr, OPTSTRING);
1315 snprintf(tstr, OPTSTRING, "cork %d,", txmsg_cork);
1316 append_str(options, tstr, OPTSTRING);
1319 snprintf(tstr, OPTSTRING, "start %d,", txmsg_start);
1320 append_str(options, tstr, OPTSTRING);
1323 snprintf(tstr, OPTSTRING, "end %d,", txmsg_end);
1324 append_str(options, tstr, OPTSTRING);
1326 if (txmsg_start_pop) {
1327 snprintf(tstr, OPTSTRING, "pop (%d,%d),",
1328 txmsg_start_pop, txmsg_start_pop + txmsg_pop);
1329 append_str(options, tstr, OPTSTRING);
1332 append_str(options, "ingress,", OPTSTRING);
1333 if (txmsg_redir_skb)
1334 append_str(options, "redir_skb,", OPTSTRING);
1336 append_str(options, "ktls_skb,", OPTSTRING);
1338 append_str(options, "ktls,", OPTSTRING);
1340 append_str(options, "peek,", OPTSTRING);
1343 static int __test_exec(int cgrp, int test, struct sockmap_options *opt)
1345 char *options = calloc(OPTSTRING, sizeof(char));
1348 if (test == SENDPAGE)
1349 opt->sendpage = true;
1351 opt->sendpage = false;
1354 opt->drop_expected = true;
1356 opt->drop_expected = false;
1358 test_options(options);
1362 " [TEST %i]: (%i, %i, %i, %s, %s): ",
1363 test_cnt, opt->rate, opt->iov_count, opt->iov_length,
1364 test_to_str(test), options);
1367 err = run_options(opt, cgrp, test);
1369 fprintf(stdout, " %s\n", !err ? "PASS" : "FAILED");
1371 !err ? passed++ : failed++;
1376 static void test_exec(int cgrp, struct sockmap_options *opt)
1378 int type = strcmp(opt->map, BPF_SOCKMAP_FILENAME);
1383 err = __test_exec(cgrp, SENDMSG, opt);
1388 err = __test_exec(cgrp, SENDPAGE, opt);
1394 static void test_send_one(struct sockmap_options *opt, int cgrp)
1396 opt->iov_length = 1;
1399 test_exec(cgrp, opt);
1401 opt->iov_length = 1;
1402 opt->iov_count = 1024;
1404 test_exec(cgrp, opt);
1406 opt->iov_length = 1024;
1409 test_exec(cgrp, opt);
1413 static void test_send_many(struct sockmap_options *opt, int cgrp)
1415 opt->iov_length = 3;
1418 test_exec(cgrp, opt);
1422 opt->iov_length = 5;
1423 test_exec(cgrp, opt);
1426 static void test_send_large(struct sockmap_options *opt, int cgrp)
1428 opt->iov_length = 256;
1429 opt->iov_count = 1024;
1431 test_exec(cgrp, opt);
1434 static void test_send(struct sockmap_options *opt, int cgrp)
1436 test_send_one(opt, cgrp);
1437 test_send_many(opt, cgrp);
1438 test_send_large(opt, cgrp);
1442 static void test_txmsg_pass(int cgrp, struct sockmap_options *opt)
1444 /* Test small and large iov_count values with pass/redir/apply/cork */
1446 test_send(opt, cgrp);
1449 static void test_txmsg_redir(int cgrp, struct sockmap_options *opt)
1452 test_send(opt, cgrp);
1455 static void test_txmsg_drop(int cgrp, struct sockmap_options *opt)
1458 test_send(opt, cgrp);
1461 static void test_txmsg_ingress_redir(int cgrp, struct sockmap_options *opt)
1463 txmsg_pass = txmsg_drop = 0;
1464 txmsg_ingress = txmsg_redir = 1;
1465 test_send(opt, cgrp);
1468 static void test_txmsg_skb(int cgrp, struct sockmap_options *opt)
1470 bool data = opt->data_test;
1473 opt->data_test = true;
1476 txmsg_pass = txmsg_drop = 0;
1477 txmsg_ingress = txmsg_redir = 0;
1481 /* Using data verification so ensure iov layout is
1482 * expected from test receiver side. e.g. has enough
1483 * bytes to write test code.
1485 opt->iov_length = 100;
1488 test_exec(cgrp, opt);
1490 txmsg_ktls_skb_drop = 1;
1491 test_exec(cgrp, opt);
1493 txmsg_ktls_skb_drop = 0;
1494 txmsg_ktls_skb_redir = 1;
1495 test_exec(cgrp, opt);
1496 txmsg_ktls_skb_redir = 0;
1498 /* Tests that omit skb_parser */
1499 txmsg_omit_skb_parser = 1;
1502 test_exec(cgrp, opt);
1504 txmsg_ktls_skb_drop = 1;
1505 test_exec(cgrp, opt);
1506 txmsg_ktls_skb_drop = 0;
1508 txmsg_ktls_skb_redir = 1;
1509 test_exec(cgrp, opt);
1512 test_exec(cgrp, opt);
1513 txmsg_omit_skb_parser = 0;
1515 opt->data_test = data;
1519 /* Test cork with hung data. This tests poor usage patterns where
1520 * cork can leave data on the ring if user program is buggy and
1521 * doesn't flush them somehow. They do take some time however
1522 * because they wait for a timeout. Test pass, redir and cork with
1523 * apply logic. Use cork size of 4097 with send_large to avoid
1524 * aligning cork size with send size.
1526 static void test_txmsg_cork_hangs(int cgrp, struct sockmap_options *opt)
1532 test_send_large(opt, cgrp);
1538 test_send_large(opt, cgrp);
1544 test_send_large(opt, cgrp);
1547 static void test_txmsg_pull(int cgrp, struct sockmap_options *opt)
1549 /* Test basic start/end */
1552 test_send(opt, cgrp);
1557 test_send_large(opt, cgrp);
1559 /* Test pull + redirect */
1563 test_send(opt, cgrp);
1565 /* Test pull + cork */
1570 test_send_many(opt, cgrp);
1572 /* Test pull + cork + redirect */
1577 test_send_many(opt, cgrp);
1580 static void test_txmsg_pop(int cgrp, struct sockmap_options *opt)
1582 /* Test basic pop */
1583 txmsg_start_pop = 1;
1585 test_send_many(opt, cgrp);
1587 /* Test pop with >4k */
1588 txmsg_start_pop = 4096;
1590 test_send_large(opt, cgrp);
1592 /* Test pop + redirect */
1594 txmsg_start_pop = 1;
1596 test_send_many(opt, cgrp);
1598 /* Test pop + cork */
1601 txmsg_start_pop = 1;
1603 test_send_many(opt, cgrp);
1605 /* Test pop + redirect + cork */
1608 txmsg_start_pop = 1;
1610 test_send_many(opt, cgrp);
1613 static void test_txmsg_push(int cgrp, struct sockmap_options *opt)
1615 /* Test basic push */
1616 txmsg_start_push = 1;
1618 test_send(opt, cgrp);
1620 /* Test push 4kB >4k */
1621 txmsg_start_push = 4096;
1622 txmsg_end_push = 4096;
1623 test_send_large(opt, cgrp);
1625 /* Test push + redirect */
1627 txmsg_start_push = 1;
1629 test_send_many(opt, cgrp);
1631 /* Test push + cork */
1634 txmsg_start_push = 1;
1636 test_send_many(opt, cgrp);
1639 static void test_txmsg_push_pop(int cgrp, struct sockmap_options *opt)
1641 txmsg_start_push = 1;
1642 txmsg_end_push = 10;
1643 txmsg_start_pop = 5;
1645 test_send_large(opt, cgrp);
1648 static void test_txmsg_apply(int cgrp, struct sockmap_options *opt)
1654 test_send_one(opt, cgrp);
1660 test_send_one(opt, cgrp);
1666 test_send_large(opt, cgrp);
1672 test_send_large(opt, cgrp);
1675 static void test_txmsg_cork(int cgrp, struct sockmap_options *opt)
1681 test_send(opt, cgrp);
1687 test_send(opt, cgrp);
1690 static void test_txmsg_ingress_parser(int cgrp, struct sockmap_options *opt)
1693 skb_use_parser = 512;
1695 skb_use_parser = 570;
1696 opt->iov_length = 256;
1699 test_exec(cgrp, opt);
1702 static void test_txmsg_ingress_parser2(int cgrp, struct sockmap_options *opt)
1706 skb_use_parser = 10;
1707 opt->iov_length = 20;
1710 opt->check_recved_len = true;
1711 test_exec(cgrp, opt);
1712 opt->check_recved_len = false;
1715 char *map_names[] = {
1727 int prog_attach_type[] = {
1728 BPF_SK_SKB_STREAM_PARSER,
1729 BPF_SK_SKB_STREAM_VERDICT,
1730 BPF_SK_SKB_STREAM_VERDICT,
1731 BPF_CGROUP_SOCK_OPS,
1742 BPF_PROG_TYPE_SK_SKB,
1743 BPF_PROG_TYPE_SK_SKB,
1744 BPF_PROG_TYPE_SK_SKB,
1745 BPF_PROG_TYPE_SOCK_OPS,
1746 BPF_PROG_TYPE_SK_MSG,
1747 BPF_PROG_TYPE_SK_MSG,
1748 BPF_PROG_TYPE_SK_MSG,
1749 BPF_PROG_TYPE_SK_MSG,
1750 BPF_PROG_TYPE_SK_MSG,
1751 BPF_PROG_TYPE_SK_MSG,
1752 BPF_PROG_TYPE_SK_MSG,
1755 static int populate_progs(char *bpf_file)
1757 struct bpf_program *prog;
1758 struct bpf_object *obj;
1762 obj = bpf_object__open(bpf_file);
1763 err = libbpf_get_error(obj);
1767 libbpf_strerror(err, err_buf, sizeof(err_buf));
1768 printf("Unable to load eBPF objects in file '%s' : %s\n",
1773 bpf_object__for_each_program(prog, obj) {
1774 bpf_program__set_type(prog, prog_type[i]);
1775 bpf_program__set_expected_attach_type(prog,
1776 prog_attach_type[i]);
1780 i = bpf_object__load(obj);
1782 bpf_object__for_each_program(prog, obj) {
1783 prog_fd[i] = bpf_program__fd(prog);
1787 for (i = 0; i < ARRAY_SIZE(map_fd); i++) {
1788 maps[i] = bpf_object__find_map_by_name(obj, map_names[i]);
1789 map_fd[i] = bpf_map__fd(maps[i]);
1790 if (map_fd[i] < 0) {
1791 fprintf(stderr, "load_bpf_file: (%i) %s\n",
1792 map_fd[i], strerror(errno));
1800 struct _test test[] = {
1801 {"txmsg test passthrough", test_txmsg_pass},
1802 {"txmsg test redirect", test_txmsg_redir},
1803 {"txmsg test drop", test_txmsg_drop},
1804 {"txmsg test ingress redirect", test_txmsg_ingress_redir},
1805 {"txmsg test skb", test_txmsg_skb},
1806 {"txmsg test apply", test_txmsg_apply},
1807 {"txmsg test cork", test_txmsg_cork},
1808 {"txmsg test hanging corks", test_txmsg_cork_hangs},
1809 {"txmsg test push_data", test_txmsg_push},
1810 {"txmsg test pull-data", test_txmsg_pull},
1811 {"txmsg test pop-data", test_txmsg_pop},
1812 {"txmsg test push/pop data", test_txmsg_push_pop},
1813 {"txmsg test ingress parser", test_txmsg_ingress_parser},
1814 {"txmsg test ingress parser2", test_txmsg_ingress_parser2},
1817 static int check_whitelist(struct _test *t, struct sockmap_options *opt)
1821 if (!opt->whitelist)
1823 ptr = strdup(opt->whitelist);
1826 entry = strtok(ptr, ",");
1828 if ((opt->prepend && strstr(opt->prepend, entry) != 0) ||
1829 strstr(opt->map, entry) != 0 ||
1830 strstr(t->title, entry) != 0)
1832 entry = strtok(NULL, ",");
1837 static int check_blacklist(struct _test *t, struct sockmap_options *opt)
1841 if (!opt->blacklist)
1843 ptr = strdup(opt->blacklist);
1846 entry = strtok(ptr, ",");
1848 if ((opt->prepend && strstr(opt->prepend, entry) != 0) ||
1849 strstr(opt->map, entry) != 0 ||
1850 strstr(t->title, entry) != 0)
1852 entry = strtok(NULL, ",");
1857 static int __test_selftests(int cg_fd, struct sockmap_options *opt)
1861 err = populate_progs(opt->map);
1863 fprintf(stderr, "ERROR: (%i) load bpf failed\n", err);
1867 /* Tests basic commands and APIs */
1868 for (i = 0; i < ARRAY_SIZE(test); i++) {
1869 struct _test t = test[i];
1871 if (check_whitelist(&t, opt) != 0)
1873 if (check_blacklist(&t, opt) == 0)
1876 test_start_subtest(&t, opt);
1877 t.tester(cg_fd, opt);
1884 static void test_selftests_sockmap(int cg_fd, struct sockmap_options *opt)
1886 opt->map = BPF_SOCKMAP_FILENAME;
1887 __test_selftests(cg_fd, opt);
1890 static void test_selftests_sockhash(int cg_fd, struct sockmap_options *opt)
1892 opt->map = BPF_SOCKHASH_FILENAME;
1893 __test_selftests(cg_fd, opt);
1896 static void test_selftests_ktls(int cg_fd, struct sockmap_options *opt)
1898 opt->map = BPF_SOCKHASH_FILENAME;
1899 opt->prepend = "ktls";
1901 __test_selftests(cg_fd, opt);
1905 static int test_selftest(int cg_fd, struct sockmap_options *opt)
1908 test_selftests_sockmap(cg_fd, opt);
1909 test_selftests_sockhash(cg_fd, opt);
1910 test_selftests_ktls(cg_fd, opt);
1911 test_print_results();
1915 int main(int argc, char **argv)
1917 int iov_count = 1, length = 1024, rate = 1;
1918 struct sockmap_options options = {0};
1919 int opt, longindex, err, cg_fd = 0;
1920 char *bpf_file = BPF_SOCKMAP_FILENAME;
1921 int test = SELFTESTS;
1922 bool cg_created = 0;
1924 while ((opt = getopt_long(argc, argv, ":dhv:c:r:i:l:t:p:q:n:b:",
1925 long_options, &longindex)) != -1) {
1928 txmsg_start = atoi(optarg);
1931 txmsg_end = atoi(optarg);
1934 txmsg_start_push = atoi(optarg);
1937 txmsg_end_push = atoi(optarg);
1940 txmsg_start_pop = atoi(optarg);
1943 txmsg_pop = atoi(optarg);
1946 txmsg_apply = atoi(optarg);
1949 txmsg_cork = atoi(optarg);
1952 cg_fd = open(optarg, O_DIRECTORY, O_RDONLY);
1955 "ERROR: (%i) open cg path failed: %s\n",
1961 rate = atoi(optarg);
1964 options.verbose = 1;
1966 options.verbose = atoi(optarg);
1969 iov_count = atoi(optarg);
1972 length = atoi(optarg);
1975 options.data_test = true;
1978 if (strcmp(optarg, "ping") == 0) {
1980 } else if (strcmp(optarg, "sendmsg") == 0) {
1982 } else if (strcmp(optarg, "base") == 0) {
1984 } else if (strcmp(optarg, "base_sendpage") == 0) {
1985 test = BASE_SENDPAGE;
1986 } else if (strcmp(optarg, "sendpage") == 0) {
1994 options.whitelist = strdup(optarg);
1995 if (!options.whitelist)
1999 options.blacklist = strdup(optarg);
2000 if (!options.blacklist)
2012 cg_fd = cgroup_setup_and_join(CG_PATH);
2018 /* Use libbpf 1.0 API mode */
2019 libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
2021 if (test == SELFTESTS) {
2022 err = test_selftest(cg_fd, &options);
2026 err = populate_progs(bpf_file);
2028 fprintf(stderr, "populate program: (%s) %s\n",
2029 bpf_file, strerror(errno));
2035 signal(SIGINT, running_handler);
2037 options.iov_count = iov_count;
2038 options.iov_length = length;
2039 options.rate = rate;
2041 err = run_options(&options, cg_fd, test);
2043 if (options.whitelist)
2044 free(options.whitelist);
2045 if (options.blacklist)
2046 free(options.blacklist);
2048 cleanup_cgroup_environment();
2053 void running_handler(int a)