1 // SPDX-License-Identifier: GPL-2.0
20 #include <sys/ioctl.h>
21 #include <sys/random.h>
22 #include <sys/socket.h>
23 #include <sys/types.h>
27 #include <netinet/in.h>
29 #include <linux/tcp.h>
30 #include <linux/sockios.h>
33 #define IPPROTO_MPTCP 262
39 static int pf = AF_INET;
40 static int proto_tx = IPPROTO_MPTCP;
41 static int proto_rx = IPPROTO_MPTCP;
43 static void die_perror(const char *msg)
49 static void die_usage(int r)
51 fprintf(stderr, "Usage: mptcp_inq [-6] [ -t tcp|mptcp ] [ -r tcp|mptcp]\n");
55 static void xerror(const char *fmt, ...)
60 vfprintf(stderr, fmt, ap);
66 static const char *getxinfo_strerr(int err)
68 if (err == EAI_SYSTEM)
69 return strerror(errno);
71 return gai_strerror(err);
74 static void xgetaddrinfo(const char *node, const char *service,
75 const struct addrinfo *hints,
76 struct addrinfo **res)
78 int err = getaddrinfo(node, service, hints, res);
81 const char *errstr = getxinfo_strerr(err);
83 fprintf(stderr, "Fatal: getaddrinfo(%s:%s): %s\n",
84 node ? node : "", service ? service : "", errstr);
89 static int sock_listen_mptcp(const char * const listenaddr,
90 const char * const port)
93 struct addrinfo hints = {
94 .ai_protocol = IPPROTO_TCP,
95 .ai_socktype = SOCK_STREAM,
96 .ai_flags = AI_PASSIVE | AI_NUMERICHOST
101 struct addrinfo *a, *addr;
104 xgetaddrinfo(listenaddr, port, &hints, &addr);
105 hints.ai_family = pf;
107 for (a = addr; a; a = a->ai_next) {
108 sock = socket(a->ai_family, a->ai_socktype, proto_rx);
112 if (-1 == setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &one,
114 perror("setsockopt");
116 if (bind(sock, a->ai_addr, a->ai_addrlen) == 0)
127 xerror("could not create listen socket");
129 if (listen(sock, 20))
130 die_perror("listen");
135 static int sock_connect_mptcp(const char * const remoteaddr,
136 const char * const port, int proto)
138 struct addrinfo hints = {
139 .ai_protocol = IPPROTO_TCP,
140 .ai_socktype = SOCK_STREAM,
142 struct addrinfo *a, *addr;
145 hints.ai_family = pf;
147 xgetaddrinfo(remoteaddr, port, &hints, &addr);
148 for (a = addr; a; a = a->ai_next) {
149 sock = socket(a->ai_family, a->ai_socktype, proto);
153 if (connect(sock, a->ai_addr, a->ai_addrlen) == 0)
156 die_perror("connect");
160 xerror("could not create connect socket");
166 static int protostr_to_num(const char *s)
168 if (strcasecmp(s, "tcp") == 0)
170 if (strcasecmp(s, "mptcp") == 0)
171 return IPPROTO_MPTCP;
177 static void parse_opts(int argc, char **argv)
181 while ((c = getopt(argc, argv, "h6t:r:")) != -1) {
190 proto_tx = protostr_to_num(optarg);
193 proto_rx = protostr_to_num(optarg);
202 /* wait up to timeout milliseconds */
203 static void wait_for_ack(int fd, int timeout, size_t total)
207 for (i = 0; i < timeout; i++) {
208 int nsd, ret, queued = -1;
211 ret = ioctl(fd, TIOCOUTQ, &queued);
213 die_perror("TIOCOUTQ");
215 ret = ioctl(fd, SIOCOUTQNSD, &nsd);
217 die_perror("SIOCOUTQNSD");
219 if ((size_t)queued > total)
220 xerror("TIOCOUTQ %u, but only %zu expected\n", queued, total);
221 assert(nsd <= queued);
226 /* wait for peer to ack rx of all data */
228 req.tv_nsec = 1 * 1000 * 1000ul; /* 1ms */
229 nanosleep(&req, NULL);
232 xerror("still tx data queued after %u ms\n", timeout);
235 static void connect_one_server(int fd, int unixfd)
237 size_t len, i, total, sent;
238 char buf[4096], buf2[4096];
241 len = rand() % (sizeof(buf) - 1);
246 for (i = 0; i < len ; i++) {
247 buf[i] = rand() % 26;
253 /* un-block server */
254 ret = read(unixfd, buf2, 4);
257 assert(strncmp(buf2, "xmit", 4) == 0);
259 ret = write(unixfd, &len, sizeof(len));
260 assert(ret == (ssize_t)sizeof(len));
262 ret = write(fd, buf, len);
266 if (ret != (ssize_t)len)
267 xerror("short write");
269 ret = read(unixfd, buf2, 4);
270 assert(strncmp(buf2, "huge", 4) == 0);
272 total = rand() % (16 * 1024 * 1024);
273 total += (1 * 1024 * 1024);
276 ret = write(unixfd, &total, sizeof(total));
277 assert(ret == (ssize_t)sizeof(total));
279 wait_for_ack(fd, 5000, len);
282 if (total > sizeof(buf))
287 ret = write(fd, buf, len);
292 /* we don't have to care about buf content, only
293 * number of total bytes sent
297 ret = read(unixfd, buf2, 4);
299 assert(strncmp(buf2, "shut", 4) == 0);
301 wait_for_ack(fd, 5000, sent);
303 ret = write(fd, buf, 1);
306 ret = write(unixfd, "closed", 6);
312 static void get_tcp_inq(struct msghdr *msgh, unsigned int *inqv)
314 struct cmsghdr *cmsg;
316 for (cmsg = CMSG_FIRSTHDR(msgh); cmsg ; cmsg = CMSG_NXTHDR(msgh, cmsg)) {
317 if (cmsg->cmsg_level == IPPROTO_TCP && cmsg->cmsg_type == TCP_CM_INQ) {
318 memcpy(inqv, CMSG_DATA(cmsg), sizeof(*inqv));
323 xerror("could not find TCP_CM_INQ cmsg type");
326 static void process_one_client(int fd, int unixfd)
328 unsigned int tcp_inq;
337 struct msghdr msg = {
340 .msg_control = msg_buf,
341 .msg_controllen = sizeof(msg_buf),
345 ret = write(unixfd, "xmit", 4);
348 ret = read(unixfd, &expect_len, sizeof(expect_len));
349 assert(ret == (ssize_t)sizeof(expect_len));
351 if (expect_len > sizeof(buf))
352 xerror("expect len %zu exceeds buffer size", expect_len);
358 ret = ioctl(fd, FIONREAD, &queued);
360 die_perror("FIONREAD");
361 if (queued > expect_len)
362 xerror("FIONREAD returned %u, but only %zu expected\n",
364 if (queued == expect_len)
368 req.tv_nsec = 1000 * 1000ul;
369 nanosleep(&req, NULL);
372 /* read one byte, expect cmsg to return expected - 1 */
373 ret = recvmsg(fd, &msg, 0);
375 die_perror("recvmsg");
377 if (msg.msg_controllen == 0)
378 xerror("msg_controllen is 0");
380 get_tcp_inq(&msg, &tcp_inq);
382 assert((size_t)tcp_inq == (expect_len - 1));
384 iov.iov_len = sizeof(buf);
385 ret = recvmsg(fd, &msg, 0);
387 die_perror("recvmsg");
389 /* should have gotten exact remainder of all pending data */
390 assert(ret == (ssize_t)tcp_inq);
392 /* should be 0, all drained */
393 get_tcp_inq(&msg, &tcp_inq);
394 assert(tcp_inq == 0);
396 /* request a large swath of data. */
397 ret = write(unixfd, "huge", 4);
400 ret = read(unixfd, &expect_len, sizeof(expect_len));
401 assert(ret == (ssize_t)sizeof(expect_len));
403 /* peer should send us a few mb of data */
404 if (expect_len <= sizeof(buf))
405 xerror("expect len %zu too small\n", expect_len);
409 iov.iov_len = sizeof(buf);
410 ret = recvmsg(fd, &msg, 0);
412 die_perror("recvmsg");
416 get_tcp_inq(&msg, &tcp_inq);
418 if (tcp_inq > expect_len - tot)
419 xerror("inq %d, remaining %d total_len %d\n",
420 tcp_inq, expect_len - tot, (int)expect_len);
422 assert(tcp_inq <= expect_len - tot);
423 } while ((size_t)tot < expect_len);
425 ret = write(unixfd, "shut", 4);
428 /* wait for hangup. Should have received one more byte of data. */
429 ret = read(unixfd, tmp, sizeof(tmp));
431 assert(strncmp(tmp, "closed", 6) == 0);
436 ret = recvmsg(fd, &msg, 0);
438 die_perror("recvmsg");
441 get_tcp_inq(&msg, &tcp_inq);
443 /* tcp_inq should be 1 due to received fin. */
444 assert(tcp_inq == 1);
447 ret = recvmsg(fd, &msg, 0);
449 die_perror("recvmsg");
453 get_tcp_inq(&msg, &tcp_inq);
454 assert(tcp_inq == 1);
459 static int xaccept(int s)
461 int fd = accept(s, NULL, 0);
464 die_perror("accept");
469 static int server(int unixfd)
471 int fd = -1, r, on = 1;
475 fd = sock_listen_mptcp("127.0.0.1", "15432");
478 fd = sock_listen_mptcp("::1", "15432");
481 xerror("Unknown pf %d\n", pf);
485 r = write(unixfd, "conn", 4);
491 if (-1 == setsockopt(r, IPPROTO_TCP, TCP_INQ, &on, sizeof(on)))
492 die_perror("setsockopt");
494 process_one_client(r, unixfd);
499 static int client(int unixfd)
507 fd = sock_connect_mptcp("127.0.0.1", "15432", proto_tx);
510 fd = sock_connect_mptcp("::1", "15432", proto_tx);
513 xerror("Unknown pf %d\n", pf);
516 connect_one_server(fd, unixfd);
521 static void init_rng(void)
525 if (getrandom(&foo, sizeof(foo), 0) == -1) {
533 static pid_t xfork(void)
545 static int rcheck(int wstatus, const char *what)
547 if (WIFEXITED(wstatus)) {
548 if (WEXITSTATUS(wstatus) == 0)
550 fprintf(stderr, "%s exited, status=%d\n", what, WEXITSTATUS(wstatus));
551 return WEXITSTATUS(wstatus);
552 } else if (WIFSIGNALED(wstatus)) {
553 xerror("%s killed by signal %d\n", what, WTERMSIG(wstatus));
554 } else if (WIFSTOPPED(wstatus)) {
555 xerror("%s stopped by signal %d\n", what, WSTOPSIG(wstatus));
561 int main(int argc, char *argv[])
567 parse_opts(argc, argv);
569 e1 = socketpair(AF_UNIX, SOCK_DGRAM, 0, unixfds);
575 return server(unixfds[1]);
579 /* wait until server bound a socket */
580 e1 = read(unixfds[0], &e1, 4);
585 return client(unixfds[0]);
589 ret = waitpid(s, &wstatus, 0);
591 die_perror("waitpid");
592 e1 = rcheck(wstatus, "server");
593 ret = waitpid(c, &wstatus, 0);
595 die_perror("waitpid");
596 e2 = rcheck(wstatus, "client");