Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 | // SPDX-License-Identifier: GPL-2.0 /* Copyright (c) 2019 Facebook */ #include <linux/err.h> #include <test_progs.h> #include "bpf_dctcp.skel.h" #include "bpf_cubic.skel.h" #define min(a, b) ((a) < (b) ? (a) : (b)) static const unsigned int total_bytes = 10 * 1024 * 1024; static const struct timeval timeo_sec = { .tv_sec = 10 }; static const size_t timeo_optlen = sizeof(timeo_sec); static int stop, duration; static int settimeo(int fd) { int err; err = setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeo_sec, timeo_optlen); if (CHECK(err == -1, "setsockopt(fd, SO_RCVTIMEO)", "errno:%d\n", errno)) return -1; err = setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &timeo_sec, timeo_optlen); if (CHECK(err == -1, "setsockopt(fd, SO_SNDTIMEO)", "errno:%d\n", errno)) return -1; return 0; } static int settcpca(int fd, const char *tcp_ca) { int err; err = setsockopt(fd, IPPROTO_TCP, TCP_CONGESTION, tcp_ca, strlen(tcp_ca)); if (CHECK(err == -1, "setsockopt(fd, TCP_CONGESTION)", "errno:%d\n", errno)) return -1; return 0; } static void *server(void *arg) { int lfd = (int)(long)arg, err = 0, fd; ssize_t nr_sent = 0, bytes = 0; char batch[1500]; fd = accept(lfd, NULL, NULL); while (fd == -1) { if (errno == EINTR) continue; err = -errno; goto done; } if (settimeo(fd)) { err = -errno; goto done; } while (bytes < total_bytes && !READ_ONCE(stop)) { nr_sent = send(fd, &batch, min(total_bytes - bytes, sizeof(batch)), 0); if (nr_sent == -1 && errno == EINTR) continue; if (nr_sent == -1) { err = -errno; break; } bytes += nr_sent; } CHECK(bytes != total_bytes, "send", "%zd != %u nr_sent:%zd errno:%d\n", bytes, total_bytes, nr_sent, errno); done: if (fd != -1) close(fd); if (err) { WRITE_ONCE(stop, 1); return ERR_PTR(err); } return NULL; } static void do_test(const char *tcp_ca) { struct sockaddr_in6 sa6 = {}; ssize_t nr_recv = 0, bytes = 0; int lfd = -1, fd = -1; pthread_t srv_thread; socklen_t addrlen = sizeof(sa6); void *thread_ret; char batch[1500]; int err; WRITE_ONCE(stop, 0); lfd = socket(AF_INET6, SOCK_STREAM, 0); if (CHECK(lfd == -1, "socket", "errno:%d\n", errno)) return; fd = socket(AF_INET6, SOCK_STREAM, 0); if (CHECK(fd == -1, "socket", "errno:%d\n", errno)) { close(lfd); return; } if (settcpca(lfd, tcp_ca) || settcpca(fd, tcp_ca) || settimeo(lfd) || settimeo(fd)) goto done; /* bind, listen and start server thread to accept */ sa6.sin6_family = AF_INET6; sa6.sin6_addr = in6addr_loopback; err = bind(lfd, (struct sockaddr *)&sa6, addrlen); if (CHECK(err == -1, "bind", "errno:%d\n", errno)) goto done; err = getsockname(lfd, (struct sockaddr *)&sa6, &addrlen); if (CHECK(err == -1, "getsockname", "errno:%d\n", errno)) goto done; err = listen(lfd, 1); if (CHECK(err == -1, "listen", "errno:%d\n", errno)) goto done; err = pthread_create(&srv_thread, NULL, server, (void *)(long)lfd); if (CHECK(err != 0, "pthread_create", "err:%d\n", err)) goto done; /* connect to server */ err = connect(fd, (struct sockaddr *)&sa6, addrlen); if (CHECK(err == -1, "connect", "errno:%d\n", errno)) goto wait_thread; /* recv total_bytes */ while (bytes < total_bytes && !READ_ONCE(stop)) { nr_recv = recv(fd, &batch, min(total_bytes - bytes, sizeof(batch)), 0); if (nr_recv == -1 && errno == EINTR) continue; if (nr_recv == -1) break; bytes += nr_recv; } CHECK(bytes != total_bytes, "recv", "%zd != %u nr_recv:%zd errno:%d\n", bytes, total_bytes, nr_recv, errno); wait_thread: WRITE_ONCE(stop, 1); pthread_join(srv_thread, &thread_ret); CHECK(IS_ERR(thread_ret), "pthread_join", "thread_ret:%ld", PTR_ERR(thread_ret)); done: close(lfd); close(fd); } static void test_cubic(void) { struct bpf_cubic *cubic_skel; struct bpf_link *link; cubic_skel = bpf_cubic__open_and_load(); if (CHECK(!cubic_skel, "bpf_cubic__open_and_load", "failed\n")) return; link = bpf_map__attach_struct_ops(cubic_skel->maps.cubic); if (CHECK(IS_ERR(link), "bpf_map__attach_struct_ops", "err:%ld\n", PTR_ERR(link))) { bpf_cubic__destroy(cubic_skel); return; } do_test("bpf_cubic"); bpf_link__destroy(link); bpf_cubic__destroy(cubic_skel); } static void test_dctcp(void) { struct bpf_dctcp *dctcp_skel; struct bpf_link *link; dctcp_skel = bpf_dctcp__open_and_load(); if (CHECK(!dctcp_skel, "bpf_dctcp__open_and_load", "failed\n")) return; link = bpf_map__attach_struct_ops(dctcp_skel->maps.dctcp); if (CHECK(IS_ERR(link), "bpf_map__attach_struct_ops", "err:%ld\n", PTR_ERR(link))) { bpf_dctcp__destroy(dctcp_skel); return; } do_test("bpf_dctcp"); bpf_link__destroy(link); bpf_dctcp__destroy(dctcp_skel); } void test_bpf_tcp_ca(void) { if (test__start_subtest("dctcp")) test_dctcp(); if (test__start_subtest("cubic")) test_cubic(); } |