1048d19d4SFlorian Westphal // SPDX-License-Identifier: GPL-2.0 2048d19d4SFlorian Westphal 3048d19d4SFlorian Westphal #define _GNU_SOURCE 4048d19d4SFlorian Westphal 5048d19d4SFlorian Westphal #include <errno.h> 6048d19d4SFlorian Westphal #include <limits.h> 7048d19d4SFlorian Westphal #include <fcntl.h> 8048d19d4SFlorian Westphal #include <string.h> 95e6af0a7SFlorian Westphal #include <stdarg.h> 10048d19d4SFlorian Westphal #include <stdbool.h> 11048d19d4SFlorian Westphal #include <stdint.h> 12048d19d4SFlorian Westphal #include <stdio.h> 13048d19d4SFlorian Westphal #include <stdlib.h> 14048d19d4SFlorian Westphal #include <strings.h> 15df62f2ecSPaolo Abeni #include <signal.h> 16048d19d4SFlorian Westphal #include <unistd.h> 17b6ab64b0SPaolo Abeni #include <time.h> 18048d19d4SFlorian Westphal 1905be5e27SPaolo Abeni #include <sys/ioctl.h> 20048d19d4SFlorian Westphal #include <sys/poll.h> 21048d19d4SFlorian Westphal #include <sys/sendfile.h> 22048d19d4SFlorian Westphal #include <sys/stat.h> 23048d19d4SFlorian Westphal #include <sys/socket.h> 24048d19d4SFlorian Westphal #include <sys/types.h> 25048d19d4SFlorian Westphal #include <sys/mman.h> 26048d19d4SFlorian Westphal 27048d19d4SFlorian Westphal #include <netdb.h> 28048d19d4SFlorian Westphal #include <netinet/in.h> 29048d19d4SFlorian Westphal 30048d19d4SFlorian Westphal #include <linux/tcp.h> 315e6af0a7SFlorian Westphal #include <linux/time_types.h> 3205be5e27SPaolo Abeni #include <linux/sockios.h> 33048d19d4SFlorian Westphal 34048d19d4SFlorian Westphal extern int optind; 35048d19d4SFlorian Westphal 36048d19d4SFlorian Westphal #ifndef IPPROTO_MPTCP 37048d19d4SFlorian Westphal #define IPPROTO_MPTCP 262 38048d19d4SFlorian Westphal #endif 39048d19d4SFlorian Westphal #ifndef TCP_ULP 40048d19d4SFlorian Westphal #define TCP_ULP 31 41048d19d4SFlorian Westphal #endif 42048d19d4SFlorian Westphal 438a4b910dSFlorian Westphal static int poll_timeout = 10 * 1000; 44048d19d4SFlorian Westphal static bool listen_mode; 45df62f2ecSPaolo Abeni static bool quit; 46048d19d4SFlorian Westphal 47048d19d4SFlorian Westphal enum cfg_mode { 48048d19d4SFlorian Westphal CFG_MODE_POLL, 49048d19d4SFlorian Westphal CFG_MODE_MMAP, 50048d19d4SFlorian Westphal CFG_MODE_SENDFILE, 51048d19d4SFlorian Westphal }; 52048d19d4SFlorian Westphal 53df8aee6dSYonglong Li enum cfg_peek { 54df8aee6dSYonglong Li CFG_NONE_PEEK, 55df8aee6dSYonglong Li CFG_WITH_PEEK, 56df8aee6dSYonglong Li CFG_AFTER_PEEK, 57df8aee6dSYonglong Li }; 58df8aee6dSYonglong Li 59048d19d4SFlorian Westphal static enum cfg_mode cfg_mode = CFG_MODE_POLL; 60df8aee6dSYonglong Li static enum cfg_peek cfg_peek = CFG_NONE_PEEK; 61048d19d4SFlorian Westphal static const char *cfg_host; 62048d19d4SFlorian Westphal static const char *cfg_port = "12000"; 63048d19d4SFlorian Westphal static int cfg_sock_proto = IPPROTO_MPTCP; 64048d19d4SFlorian Westphal static int pf = AF_INET; 65048d19d4SFlorian Westphal static int cfg_sndbuf; 668a4b910dSFlorian Westphal static int cfg_rcvbuf; 67b08fbf24SPaolo Abeni static bool cfg_join; 6813153324SGeliang Tang static bool cfg_remove; 69b6ab64b0SPaolo Abeni static unsigned int cfg_time; 702e580a63SGeliang Tang static unsigned int cfg_do_w; 71df62f2ecSPaolo Abeni static int cfg_wait; 72dc65fe82SFlorian Westphal static uint32_t cfg_mark; 7305be5e27SPaolo Abeni static char *cfg_input; 7405be5e27SPaolo Abeni static int cfg_repeat = 1; 756bf41020SPaolo Abeni static int cfg_truncate; 766bf41020SPaolo Abeni static int cfg_rcv_trunc; 77048d19d4SFlorian Westphal 785e6af0a7SFlorian Westphal struct cfg_cmsg_types { 795e6af0a7SFlorian Westphal unsigned int cmsg_enabled:1; 805e6af0a7SFlorian Westphal unsigned int timestampns:1; 815cbd886cSFlorian Westphal unsigned int tcp_inq:1; 825e6af0a7SFlorian Westphal }; 835e6af0a7SFlorian Westphal 845fb62e9cSFlorian Westphal struct cfg_sockopt_types { 855fb62e9cSFlorian Westphal unsigned int transparent:1; 86ca7ae891SDmytro Shytyi unsigned int mptfo:1; 875fb62e9cSFlorian Westphal }; 885fb62e9cSFlorian Westphal 895cbd886cSFlorian Westphal struct tcp_inq_state { 905cbd886cSFlorian Westphal unsigned int last; 915cbd886cSFlorian Westphal bool expect_eof; 925cbd886cSFlorian Westphal }; 935cbd886cSFlorian Westphal 94ca7ae891SDmytro Shytyi struct wstate { 95ca7ae891SDmytro Shytyi char buf[8192]; 96ca7ae891SDmytro Shytyi unsigned int len; 97ca7ae891SDmytro Shytyi unsigned int off; 98ca7ae891SDmytro Shytyi unsigned int total_len; 99ca7ae891SDmytro Shytyi }; 100ca7ae891SDmytro Shytyi 1015cbd886cSFlorian Westphal static struct tcp_inq_state tcp_inq; 1025cbd886cSFlorian Westphal 1035e6af0a7SFlorian Westphal static struct cfg_cmsg_types cfg_cmsg_types; 1045fb62e9cSFlorian Westphal static struct cfg_sockopt_types cfg_sockopt_types; 1055e6af0a7SFlorian Westphal 106048d19d4SFlorian Westphal static void die_usage(void) 107048d19d4SFlorian Westphal { 1086bf41020SPaolo Abeni fprintf(stderr, "Usage: mptcp_connect [-6] [-c cmsg] [-f offset] [-i file] [-I num] [-j] [-l] " 10905be5e27SPaolo Abeni "[-m mode] [-M mark] [-o option] [-p port] [-P mode] [-j] [-l] [-r num] " 11005be5e27SPaolo Abeni "[-s MPTCP|TCP] [-S num] [-r num] [-t num] [-T num] [-u] [-w sec] connect_address\n"); 1118a4b910dSFlorian Westphal fprintf(stderr, "\t-6 use ipv6\n"); 11205be5e27SPaolo Abeni fprintf(stderr, "\t-c cmsg -- test cmsg type <cmsg>\n"); 1136bf41020SPaolo Abeni fprintf(stderr, "\t-f offset -- stop the I/O after receiving and sending the specified amount " 1146bf41020SPaolo Abeni "of bytes. If there are unread bytes in the receive queue, that will cause a MPTCP " 1156bf41020SPaolo Abeni "fastclose at close/shutdown. If offset is negative, expect the peer to close before " 1166bf41020SPaolo Abeni "all the local data as been sent, thus toleration errors on write and EPIPE signals\n"); 11705be5e27SPaolo Abeni fprintf(stderr, "\t-i file -- read the data to send from the given file instead of stdin"); 11805be5e27SPaolo Abeni fprintf(stderr, "\t-I num -- repeat the transfer 'num' times. In listen mode accepts num " 11905be5e27SPaolo Abeni "incoming connections, in client mode, disconnect and reconnect to the server\n"); 12005be5e27SPaolo Abeni fprintf(stderr, "\t-j -- add additional sleep at connection start and tear down " 12105be5e27SPaolo Abeni "-- for MPJ tests\n"); 12205be5e27SPaolo Abeni fprintf(stderr, "\t-l -- listens mode, accepts incoming connection\n"); 123c6f4c2b0SDavide Caratti fprintf(stderr, "\t-m [poll|mmap|sendfile] -- use poll(default)/mmap+write/sendfile\n"); 124dc65fe82SFlorian Westphal fprintf(stderr, "\t-M mark -- set socket packet mark\n"); 1255fb62e9cSFlorian Westphal fprintf(stderr, "\t-o option -- test sockopt <option>\n"); 12605be5e27SPaolo Abeni fprintf(stderr, "\t-p num -- use port num\n"); 127df8aee6dSYonglong Li fprintf(stderr, 128df8aee6dSYonglong Li "\t-P [saveWithPeek|saveAfterPeek] -- save data with/after MSG_PEEK form tcp socket\n"); 12905be5e27SPaolo Abeni fprintf(stderr, "\t-t num -- set poll timeout to num\n"); 13005be5e27SPaolo Abeni fprintf(stderr, "\t-T num -- set expected runtime to num ms\n"); 13105be5e27SPaolo Abeni fprintf(stderr, "\t-r num -- enable slow mode, limiting each write to num bytes " 13205be5e27SPaolo Abeni "-- for remove addr tests\n"); 13305be5e27SPaolo Abeni fprintf(stderr, "\t-R num -- set SO_RCVBUF to num\n"); 13405be5e27SPaolo Abeni fprintf(stderr, "\t-s [MPTCP|TCP] -- use mptcp(default) or tcp sockets\n"); 13505be5e27SPaolo Abeni fprintf(stderr, "\t-S num -- set SO_SNDBUF to num\n"); 13605be5e27SPaolo Abeni fprintf(stderr, "\t-w num -- wait num sec before closing the socket\n"); 137048d19d4SFlorian Westphal exit(1); 138048d19d4SFlorian Westphal } 139048d19d4SFlorian Westphal 1405e6af0a7SFlorian Westphal static void xerror(const char *fmt, ...) 1415e6af0a7SFlorian Westphal { 1425e6af0a7SFlorian Westphal va_list ap; 1435e6af0a7SFlorian Westphal 1445e6af0a7SFlorian Westphal va_start(ap, fmt); 1455e6af0a7SFlorian Westphal vfprintf(stderr, fmt, ap); 1465e6af0a7SFlorian Westphal va_end(ap); 1475e6af0a7SFlorian Westphal exit(1); 1485e6af0a7SFlorian Westphal } 1495e6af0a7SFlorian Westphal 150df62f2ecSPaolo Abeni static void handle_signal(int nr) 151df62f2ecSPaolo Abeni { 152df62f2ecSPaolo Abeni quit = true; 153df62f2ecSPaolo Abeni } 154df62f2ecSPaolo Abeni 155048d19d4SFlorian Westphal static const char *getxinfo_strerr(int err) 156048d19d4SFlorian Westphal { 157048d19d4SFlorian Westphal if (err == EAI_SYSTEM) 158048d19d4SFlorian Westphal return strerror(errno); 159048d19d4SFlorian Westphal 160048d19d4SFlorian Westphal return gai_strerror(err); 161048d19d4SFlorian Westphal } 162048d19d4SFlorian Westphal 163048d19d4SFlorian Westphal static void xgetnameinfo(const struct sockaddr *addr, socklen_t addrlen, 164048d19d4SFlorian Westphal char *host, socklen_t hostlen, 165048d19d4SFlorian Westphal char *serv, socklen_t servlen) 166048d19d4SFlorian Westphal { 167048d19d4SFlorian Westphal int flags = NI_NUMERICHOST | NI_NUMERICSERV; 168048d19d4SFlorian Westphal int err = getnameinfo(addr, addrlen, host, hostlen, serv, servlen, 169048d19d4SFlorian Westphal flags); 170048d19d4SFlorian Westphal 171048d19d4SFlorian Westphal if (err) { 172048d19d4SFlorian Westphal const char *errstr = getxinfo_strerr(err); 173048d19d4SFlorian Westphal 174048d19d4SFlorian Westphal fprintf(stderr, "Fatal: getnameinfo: %s\n", errstr); 175048d19d4SFlorian Westphal exit(1); 176048d19d4SFlorian Westphal } 177048d19d4SFlorian Westphal } 178048d19d4SFlorian Westphal 179048d19d4SFlorian Westphal static void xgetaddrinfo(const char *node, const char *service, 180048d19d4SFlorian Westphal const struct addrinfo *hints, 181048d19d4SFlorian Westphal struct addrinfo **res) 182048d19d4SFlorian Westphal { 183048d19d4SFlorian Westphal int err = getaddrinfo(node, service, hints, res); 184048d19d4SFlorian Westphal 185048d19d4SFlorian Westphal if (err) { 186048d19d4SFlorian Westphal const char *errstr = getxinfo_strerr(err); 187048d19d4SFlorian Westphal 188048d19d4SFlorian Westphal fprintf(stderr, "Fatal: getaddrinfo(%s:%s): %s\n", 189048d19d4SFlorian Westphal node ? node : "", service ? service : "", errstr); 190048d19d4SFlorian Westphal exit(1); 191048d19d4SFlorian Westphal } 192048d19d4SFlorian Westphal } 193048d19d4SFlorian Westphal 1948a4b910dSFlorian Westphal static void set_rcvbuf(int fd, unsigned int size) 1958a4b910dSFlorian Westphal { 1968a4b910dSFlorian Westphal int err; 1978a4b910dSFlorian Westphal 1988a4b910dSFlorian Westphal err = setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size)); 1998a4b910dSFlorian Westphal if (err) { 2008a4b910dSFlorian Westphal perror("set SO_RCVBUF"); 2018a4b910dSFlorian Westphal exit(1); 2028a4b910dSFlorian Westphal } 2038a4b910dSFlorian Westphal } 2048a4b910dSFlorian Westphal 205048d19d4SFlorian Westphal static void set_sndbuf(int fd, unsigned int size) 206048d19d4SFlorian Westphal { 207048d19d4SFlorian Westphal int err; 208048d19d4SFlorian Westphal 209048d19d4SFlorian Westphal err = setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &size, sizeof(size)); 210048d19d4SFlorian Westphal if (err) { 211048d19d4SFlorian Westphal perror("set SO_SNDBUF"); 212048d19d4SFlorian Westphal exit(1); 213048d19d4SFlorian Westphal } 214048d19d4SFlorian Westphal } 215048d19d4SFlorian Westphal 216dc65fe82SFlorian Westphal static void set_mark(int fd, uint32_t mark) 217dc65fe82SFlorian Westphal { 218dc65fe82SFlorian Westphal int err; 219dc65fe82SFlorian Westphal 220dc65fe82SFlorian Westphal err = setsockopt(fd, SOL_SOCKET, SO_MARK, &mark, sizeof(mark)); 221dc65fe82SFlorian Westphal if (err) { 222dc65fe82SFlorian Westphal perror("set SO_MARK"); 223dc65fe82SFlorian Westphal exit(1); 224dc65fe82SFlorian Westphal } 225dc65fe82SFlorian Westphal } 226dc65fe82SFlorian Westphal 2275fb62e9cSFlorian Westphal static void set_transparent(int fd, int pf) 2285fb62e9cSFlorian Westphal { 2295fb62e9cSFlorian Westphal int one = 1; 2305fb62e9cSFlorian Westphal 2315fb62e9cSFlorian Westphal switch (pf) { 2325fb62e9cSFlorian Westphal case AF_INET: 2335fb62e9cSFlorian Westphal if (-1 == setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one))) 2345fb62e9cSFlorian Westphal perror("IP_TRANSPARENT"); 2355fb62e9cSFlorian Westphal break; 2365fb62e9cSFlorian Westphal case AF_INET6: 2375fb62e9cSFlorian Westphal if (-1 == setsockopt(fd, IPPROTO_IPV6, IPV6_TRANSPARENT, &one, sizeof(one))) 2385fb62e9cSFlorian Westphal perror("IPV6_TRANSPARENT"); 2395fb62e9cSFlorian Westphal break; 2405fb62e9cSFlorian Westphal } 2415fb62e9cSFlorian Westphal } 2425fb62e9cSFlorian Westphal 243ca7ae891SDmytro Shytyi static void set_mptfo(int fd, int pf) 244ca7ae891SDmytro Shytyi { 245ca7ae891SDmytro Shytyi int qlen = 25; 246ca7ae891SDmytro Shytyi 247ca7ae891SDmytro Shytyi if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, sizeof(qlen)) == -1) 248ca7ae891SDmytro Shytyi perror("TCP_FASTOPEN"); 249ca7ae891SDmytro Shytyi } 250ca7ae891SDmytro Shytyi 251f730b65cSFlorian Westphal static int do_ulp_so(int sock, const char *name) 252f730b65cSFlorian Westphal { 253f730b65cSFlorian Westphal return setsockopt(sock, IPPROTO_TCP, TCP_ULP, name, strlen(name)); 254f730b65cSFlorian Westphal } 255f730b65cSFlorian Westphal 256f730b65cSFlorian Westphal #define X(m) xerror("%s:%u: %s: failed for proto %d at line %u", __FILE__, __LINE__, (m), proto, line) 257f730b65cSFlorian Westphal static void sock_test_tcpulp(int sock, int proto, unsigned int line) 258f730b65cSFlorian Westphal { 259f730b65cSFlorian Westphal socklen_t buflen = 8; 260f730b65cSFlorian Westphal char buf[8] = ""; 261f730b65cSFlorian Westphal int ret = getsockopt(sock, IPPROTO_TCP, TCP_ULP, buf, &buflen); 262f730b65cSFlorian Westphal 263f730b65cSFlorian Westphal if (ret != 0) 264f730b65cSFlorian Westphal X("getsockopt"); 265f730b65cSFlorian Westphal 266f730b65cSFlorian Westphal if (buflen > 0) { 267f730b65cSFlorian Westphal if (strcmp(buf, "mptcp") != 0) 268f730b65cSFlorian Westphal xerror("unexpected ULP '%s' for proto %d at line %u", buf, proto, line); 269f730b65cSFlorian Westphal ret = do_ulp_so(sock, "tls"); 270f730b65cSFlorian Westphal if (ret == 0) 271f730b65cSFlorian Westphal X("setsockopt"); 272f730b65cSFlorian Westphal } else if (proto == IPPROTO_MPTCP) { 273f730b65cSFlorian Westphal ret = do_ulp_so(sock, "tls"); 274f730b65cSFlorian Westphal if (ret != -1) 275f730b65cSFlorian Westphal X("setsockopt"); 276f730b65cSFlorian Westphal } 277f730b65cSFlorian Westphal 278f730b65cSFlorian Westphal ret = do_ulp_so(sock, "mptcp"); 279f730b65cSFlorian Westphal if (ret != -1) 280f730b65cSFlorian Westphal X("setsockopt"); 281f730b65cSFlorian Westphal 282f730b65cSFlorian Westphal #undef X 283f730b65cSFlorian Westphal } 284f730b65cSFlorian Westphal 285f730b65cSFlorian Westphal #define SOCK_TEST_TCPULP(s, p) sock_test_tcpulp((s), (p), __LINE__) 286f730b65cSFlorian Westphal 287048d19d4SFlorian Westphal static int sock_listen_mptcp(const char * const listenaddr, 288048d19d4SFlorian Westphal const char * const port) 289048d19d4SFlorian Westphal { 290fd37c2ecSMat Martineau int sock = -1; 291048d19d4SFlorian Westphal struct addrinfo hints = { 292048d19d4SFlorian Westphal .ai_protocol = IPPROTO_TCP, 293048d19d4SFlorian Westphal .ai_socktype = SOCK_STREAM, 294048d19d4SFlorian Westphal .ai_flags = AI_PASSIVE | AI_NUMERICHOST 295048d19d4SFlorian Westphal }; 296048d19d4SFlorian Westphal 297048d19d4SFlorian Westphal hints.ai_family = pf; 298048d19d4SFlorian Westphal 299048d19d4SFlorian Westphal struct addrinfo *a, *addr; 300048d19d4SFlorian Westphal int one = 1; 301048d19d4SFlorian Westphal 302048d19d4SFlorian Westphal xgetaddrinfo(listenaddr, port, &hints, &addr); 303048d19d4SFlorian Westphal hints.ai_family = pf; 304048d19d4SFlorian Westphal 305048d19d4SFlorian Westphal for (a = addr; a; a = a->ai_next) { 306048d19d4SFlorian Westphal sock = socket(a->ai_family, a->ai_socktype, cfg_sock_proto); 307048d19d4SFlorian Westphal if (sock < 0) 308048d19d4SFlorian Westphal continue; 309048d19d4SFlorian Westphal 310f730b65cSFlorian Westphal SOCK_TEST_TCPULP(sock, cfg_sock_proto); 311f730b65cSFlorian Westphal 312048d19d4SFlorian Westphal if (-1 == setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &one, 313048d19d4SFlorian Westphal sizeof(one))) 314048d19d4SFlorian Westphal perror("setsockopt"); 315048d19d4SFlorian Westphal 3165fb62e9cSFlorian Westphal if (cfg_sockopt_types.transparent) 3175fb62e9cSFlorian Westphal set_transparent(sock, pf); 3185fb62e9cSFlorian Westphal 319ca7ae891SDmytro Shytyi if (cfg_sockopt_types.mptfo) 320ca7ae891SDmytro Shytyi set_mptfo(sock, pf); 321ca7ae891SDmytro Shytyi 322048d19d4SFlorian Westphal if (bind(sock, a->ai_addr, a->ai_addrlen) == 0) 323048d19d4SFlorian Westphal break; /* success */ 324048d19d4SFlorian Westphal 325048d19d4SFlorian Westphal perror("bind"); 326048d19d4SFlorian Westphal close(sock); 327048d19d4SFlorian Westphal sock = -1; 328048d19d4SFlorian Westphal } 329048d19d4SFlorian Westphal 330048d19d4SFlorian Westphal freeaddrinfo(addr); 331048d19d4SFlorian Westphal 332048d19d4SFlorian Westphal if (sock < 0) { 333048d19d4SFlorian Westphal fprintf(stderr, "Could not create listen socket\n"); 334048d19d4SFlorian Westphal return sock; 335048d19d4SFlorian Westphal } 336048d19d4SFlorian Westphal 337f730b65cSFlorian Westphal SOCK_TEST_TCPULP(sock, cfg_sock_proto); 338f730b65cSFlorian Westphal 339048d19d4SFlorian Westphal if (listen(sock, 20)) { 340048d19d4SFlorian Westphal perror("listen"); 341048d19d4SFlorian Westphal close(sock); 342048d19d4SFlorian Westphal return -1; 343048d19d4SFlorian Westphal } 344048d19d4SFlorian Westphal 345f730b65cSFlorian Westphal SOCK_TEST_TCPULP(sock, cfg_sock_proto); 346f730b65cSFlorian Westphal 347048d19d4SFlorian Westphal return sock; 348048d19d4SFlorian Westphal } 349048d19d4SFlorian Westphal 350048d19d4SFlorian Westphal static int sock_connect_mptcp(const char * const remoteaddr, 35105be5e27SPaolo Abeni const char * const port, int proto, 352ca7ae891SDmytro Shytyi struct addrinfo **peer, 353ca7ae891SDmytro Shytyi int infd, struct wstate *winfo) 354048d19d4SFlorian Westphal { 355048d19d4SFlorian Westphal struct addrinfo hints = { 356048d19d4SFlorian Westphal .ai_protocol = IPPROTO_TCP, 357048d19d4SFlorian Westphal .ai_socktype = SOCK_STREAM, 358048d19d4SFlorian Westphal }; 359048d19d4SFlorian Westphal struct addrinfo *a, *addr; 360ca7ae891SDmytro Shytyi int syn_copied = 0; 361048d19d4SFlorian Westphal int sock = -1; 362048d19d4SFlorian Westphal 363048d19d4SFlorian Westphal hints.ai_family = pf; 364048d19d4SFlorian Westphal 365048d19d4SFlorian Westphal xgetaddrinfo(remoteaddr, port, &hints, &addr); 366048d19d4SFlorian Westphal for (a = addr; a; a = a->ai_next) { 367048d19d4SFlorian Westphal sock = socket(a->ai_family, a->ai_socktype, proto); 368048d19d4SFlorian Westphal if (sock < 0) { 369048d19d4SFlorian Westphal perror("socket"); 370048d19d4SFlorian Westphal continue; 371048d19d4SFlorian Westphal } 372048d19d4SFlorian Westphal 373f730b65cSFlorian Westphal SOCK_TEST_TCPULP(sock, proto); 374f730b65cSFlorian Westphal 375dc65fe82SFlorian Westphal if (cfg_mark) 376dc65fe82SFlorian Westphal set_mark(sock, cfg_mark); 377dc65fe82SFlorian Westphal 378ca7ae891SDmytro Shytyi if (cfg_sockopt_types.mptfo) { 379ca7ae891SDmytro Shytyi if (!winfo->total_len) 380ca7ae891SDmytro Shytyi winfo->total_len = winfo->len = read(infd, winfo->buf, 381ca7ae891SDmytro Shytyi sizeof(winfo->buf)); 382ca7ae891SDmytro Shytyi 383ca7ae891SDmytro Shytyi syn_copied = sendto(sock, winfo->buf, winfo->len, MSG_FASTOPEN, 384ca7ae891SDmytro Shytyi a->ai_addr, a->ai_addrlen); 385ca7ae891SDmytro Shytyi if (syn_copied >= 0) { 386ca7ae891SDmytro Shytyi winfo->off = syn_copied; 387ca7ae891SDmytro Shytyi winfo->len -= syn_copied; 388ca7ae891SDmytro Shytyi *peer = a; 389ca7ae891SDmytro Shytyi break; /* success */ 390ca7ae891SDmytro Shytyi } 391ca7ae891SDmytro Shytyi } else { 39205be5e27SPaolo Abeni if (connect(sock, a->ai_addr, a->ai_addrlen) == 0) { 39305be5e27SPaolo Abeni *peer = a; 394048d19d4SFlorian Westphal break; /* success */ 39505be5e27SPaolo Abeni } 396ca7ae891SDmytro Shytyi } 397ca7ae891SDmytro Shytyi if (cfg_sockopt_types.mptfo) { 398ca7ae891SDmytro Shytyi perror("sendto()"); 399ca7ae891SDmytro Shytyi close(sock); 400ca7ae891SDmytro Shytyi sock = -1; 401ca7ae891SDmytro Shytyi } else { 402048d19d4SFlorian Westphal perror("connect()"); 403048d19d4SFlorian Westphal close(sock); 404048d19d4SFlorian Westphal sock = -1; 405048d19d4SFlorian Westphal } 406ca7ae891SDmytro Shytyi } 407048d19d4SFlorian Westphal 408048d19d4SFlorian Westphal freeaddrinfo(addr); 409f730b65cSFlorian Westphal if (sock != -1) 410f730b65cSFlorian Westphal SOCK_TEST_TCPULP(sock, proto); 411048d19d4SFlorian Westphal return sock; 412048d19d4SFlorian Westphal } 413048d19d4SFlorian Westphal 414048d19d4SFlorian Westphal static size_t do_rnd_write(const int fd, char *buf, const size_t len) 415048d19d4SFlorian Westphal { 416b08fbf24SPaolo Abeni static bool first = true; 417048d19d4SFlorian Westphal unsigned int do_w; 418048d19d4SFlorian Westphal ssize_t bw; 419048d19d4SFlorian Westphal 420048d19d4SFlorian Westphal do_w = rand() & 0xffff; 421048d19d4SFlorian Westphal if (do_w == 0 || do_w > len) 422048d19d4SFlorian Westphal do_w = len; 423048d19d4SFlorian Westphal 424b08fbf24SPaolo Abeni if (cfg_join && first && do_w > 100) 425b08fbf24SPaolo Abeni do_w = 100; 426b08fbf24SPaolo Abeni 4272e580a63SGeliang Tang if (cfg_remove && do_w > cfg_do_w) 4282e580a63SGeliang Tang do_w = cfg_do_w; 42913153324SGeliang Tang 430048d19d4SFlorian Westphal bw = write(fd, buf, do_w); 431048d19d4SFlorian Westphal if (bw < 0) 4326bf41020SPaolo Abeni return bw; 433048d19d4SFlorian Westphal 434b08fbf24SPaolo Abeni /* let the join handshake complete, before going on */ 435b08fbf24SPaolo Abeni if (cfg_join && first) { 436b08fbf24SPaolo Abeni usleep(200000); 437b08fbf24SPaolo Abeni first = false; 438b08fbf24SPaolo Abeni } 439b08fbf24SPaolo Abeni 44013153324SGeliang Tang if (cfg_remove) 44113153324SGeliang Tang usleep(200000); 44213153324SGeliang Tang 443048d19d4SFlorian Westphal return bw; 444048d19d4SFlorian Westphal } 445048d19d4SFlorian Westphal 446048d19d4SFlorian Westphal static size_t do_write(const int fd, char *buf, const size_t len) 447048d19d4SFlorian Westphal { 448048d19d4SFlorian Westphal size_t offset = 0; 449048d19d4SFlorian Westphal 450048d19d4SFlorian Westphal while (offset < len) { 451048d19d4SFlorian Westphal size_t written; 452048d19d4SFlorian Westphal ssize_t bw; 453048d19d4SFlorian Westphal 454048d19d4SFlorian Westphal bw = write(fd, buf + offset, len - offset); 455048d19d4SFlorian Westphal if (bw < 0) { 456048d19d4SFlorian Westphal perror("write"); 457048d19d4SFlorian Westphal return 0; 458048d19d4SFlorian Westphal } 459048d19d4SFlorian Westphal 460048d19d4SFlorian Westphal written = (size_t)bw; 461048d19d4SFlorian Westphal offset += written; 462048d19d4SFlorian Westphal } 463048d19d4SFlorian Westphal 464048d19d4SFlorian Westphal return offset; 465048d19d4SFlorian Westphal } 466048d19d4SFlorian Westphal 4675e6af0a7SFlorian Westphal static void process_cmsg(struct msghdr *msgh) 4685e6af0a7SFlorian Westphal { 4695e6af0a7SFlorian Westphal struct __kernel_timespec ts; 4705cbd886cSFlorian Westphal bool inq_found = false; 4715e6af0a7SFlorian Westphal bool ts_found = false; 4725cbd886cSFlorian Westphal unsigned int inq = 0; 4735e6af0a7SFlorian Westphal struct cmsghdr *cmsg; 4745e6af0a7SFlorian Westphal 4755e6af0a7SFlorian Westphal for (cmsg = CMSG_FIRSTHDR(msgh); cmsg ; cmsg = CMSG_NXTHDR(msgh, cmsg)) { 4765e6af0a7SFlorian Westphal if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SO_TIMESTAMPNS_NEW) { 4775e6af0a7SFlorian Westphal memcpy(&ts, CMSG_DATA(cmsg), sizeof(ts)); 4785e6af0a7SFlorian Westphal ts_found = true; 4795e6af0a7SFlorian Westphal continue; 4805e6af0a7SFlorian Westphal } 4815cbd886cSFlorian Westphal if (cmsg->cmsg_level == IPPROTO_TCP && cmsg->cmsg_type == TCP_CM_INQ) { 4825cbd886cSFlorian Westphal memcpy(&inq, CMSG_DATA(cmsg), sizeof(inq)); 4835cbd886cSFlorian Westphal inq_found = true; 4845cbd886cSFlorian Westphal continue; 4855cbd886cSFlorian Westphal } 4865cbd886cSFlorian Westphal 4875e6af0a7SFlorian Westphal } 4885e6af0a7SFlorian Westphal 4895e6af0a7SFlorian Westphal if (cfg_cmsg_types.timestampns) { 4905e6af0a7SFlorian Westphal if (!ts_found) 4915e6af0a7SFlorian Westphal xerror("TIMESTAMPNS not present\n"); 4925e6af0a7SFlorian Westphal } 4935cbd886cSFlorian Westphal 4945cbd886cSFlorian Westphal if (cfg_cmsg_types.tcp_inq) { 4955cbd886cSFlorian Westphal if (!inq_found) 4965cbd886cSFlorian Westphal xerror("TCP_INQ not present\n"); 4975cbd886cSFlorian Westphal 4985cbd886cSFlorian Westphal if (inq > 1024) 4995cbd886cSFlorian Westphal xerror("tcp_inq %u is larger than one kbyte\n", inq); 5005cbd886cSFlorian Westphal tcp_inq.last = inq; 5015cbd886cSFlorian Westphal } 5025e6af0a7SFlorian Westphal } 5035e6af0a7SFlorian Westphal 5045e6af0a7SFlorian Westphal static ssize_t do_recvmsg_cmsg(const int fd, char *buf, const size_t len) 5055e6af0a7SFlorian Westphal { 5065e6af0a7SFlorian Westphal char msg_buf[8192]; 5075e6af0a7SFlorian Westphal struct iovec iov = { 5085e6af0a7SFlorian Westphal .iov_base = buf, 5095e6af0a7SFlorian Westphal .iov_len = len, 5105e6af0a7SFlorian Westphal }; 5115e6af0a7SFlorian Westphal struct msghdr msg = { 5125e6af0a7SFlorian Westphal .msg_iov = &iov, 5135e6af0a7SFlorian Westphal .msg_iovlen = 1, 5145e6af0a7SFlorian Westphal .msg_control = msg_buf, 5155e6af0a7SFlorian Westphal .msg_controllen = sizeof(msg_buf), 5165e6af0a7SFlorian Westphal }; 5175e6af0a7SFlorian Westphal int flags = 0; 5185cbd886cSFlorian Westphal unsigned int last_hint = tcp_inq.last; 5195e6af0a7SFlorian Westphal int ret = recvmsg(fd, &msg, flags); 5205e6af0a7SFlorian Westphal 5215cbd886cSFlorian Westphal if (ret <= 0) { 5225cbd886cSFlorian Westphal if (ret == 0 && tcp_inq.expect_eof) 5235e6af0a7SFlorian Westphal return ret; 5245e6af0a7SFlorian Westphal 5255cbd886cSFlorian Westphal if (ret == 0 && cfg_cmsg_types.tcp_inq) 5265cbd886cSFlorian Westphal if (last_hint != 1 && last_hint != 0) 5275cbd886cSFlorian Westphal xerror("EOF but last tcp_inq hint was %u\n", last_hint); 5285cbd886cSFlorian Westphal 5295cbd886cSFlorian Westphal return ret; 5305cbd886cSFlorian Westphal } 5315cbd886cSFlorian Westphal 5325cbd886cSFlorian Westphal if (tcp_inq.expect_eof) 5335cbd886cSFlorian Westphal xerror("expected EOF, last_hint %u, now %u\n", 5345cbd886cSFlorian Westphal last_hint, tcp_inq.last); 5355cbd886cSFlorian Westphal 5365e6af0a7SFlorian Westphal if (msg.msg_controllen && !cfg_cmsg_types.cmsg_enabled) 5375e6af0a7SFlorian Westphal xerror("got %lu bytes of cmsg data, expected 0\n", 5385e6af0a7SFlorian Westphal (unsigned long)msg.msg_controllen); 5395e6af0a7SFlorian Westphal 5405e6af0a7SFlorian Westphal if (msg.msg_controllen == 0 && cfg_cmsg_types.cmsg_enabled) 5415e6af0a7SFlorian Westphal xerror("%s\n", "got no cmsg data"); 5425e6af0a7SFlorian Westphal 5435e6af0a7SFlorian Westphal if (msg.msg_controllen) 5445e6af0a7SFlorian Westphal process_cmsg(&msg); 5455e6af0a7SFlorian Westphal 5465cbd886cSFlorian Westphal if (cfg_cmsg_types.tcp_inq) { 5475cbd886cSFlorian Westphal if ((size_t)ret < len && last_hint > (unsigned int)ret) { 5485cbd886cSFlorian Westphal if (ret + 1 != (int)last_hint) { 5495cbd886cSFlorian Westphal int next = read(fd, msg_buf, sizeof(msg_buf)); 5505cbd886cSFlorian Westphal 5515cbd886cSFlorian Westphal xerror("read %u of %u, last_hint was %u tcp_inq hint now %u next_read returned %d/%m\n", 5525cbd886cSFlorian Westphal ret, (unsigned int)len, last_hint, tcp_inq.last, next); 5535cbd886cSFlorian Westphal } else { 5545cbd886cSFlorian Westphal tcp_inq.expect_eof = true; 5555cbd886cSFlorian Westphal } 5565cbd886cSFlorian Westphal } 5575cbd886cSFlorian Westphal } 5585cbd886cSFlorian Westphal 5595e6af0a7SFlorian Westphal return ret; 5605e6af0a7SFlorian Westphal } 5615e6af0a7SFlorian Westphal 562048d19d4SFlorian Westphal static ssize_t do_rnd_read(const int fd, char *buf, const size_t len) 563048d19d4SFlorian Westphal { 564df8aee6dSYonglong Li int ret = 0; 565df8aee6dSYonglong Li char tmp[16384]; 566048d19d4SFlorian Westphal size_t cap = rand(); 567048d19d4SFlorian Westphal 568048d19d4SFlorian Westphal cap &= 0xffff; 569048d19d4SFlorian Westphal 570048d19d4SFlorian Westphal if (cap == 0) 571048d19d4SFlorian Westphal cap = 1; 572048d19d4SFlorian Westphal else if (cap > len) 573048d19d4SFlorian Westphal cap = len; 574048d19d4SFlorian Westphal 575df8aee6dSYonglong Li if (cfg_peek == CFG_WITH_PEEK) { 576df8aee6dSYonglong Li ret = recv(fd, buf, cap, MSG_PEEK); 577df8aee6dSYonglong Li ret = (ret < 0) ? ret : read(fd, tmp, ret); 578df8aee6dSYonglong Li } else if (cfg_peek == CFG_AFTER_PEEK) { 579df8aee6dSYonglong Li ret = recv(fd, buf, cap, MSG_PEEK); 580df8aee6dSYonglong Li ret = (ret < 0) ? ret : read(fd, buf, cap); 5815e6af0a7SFlorian Westphal } else if (cfg_cmsg_types.cmsg_enabled) { 5825e6af0a7SFlorian Westphal ret = do_recvmsg_cmsg(fd, buf, cap); 583df8aee6dSYonglong Li } else { 584df8aee6dSYonglong Li ret = read(fd, buf, cap); 585df8aee6dSYonglong Li } 586df8aee6dSYonglong Li 587df8aee6dSYonglong Li return ret; 588048d19d4SFlorian Westphal } 589048d19d4SFlorian Westphal 59005be5e27SPaolo Abeni static void set_nonblock(int fd, bool nonblock) 591048d19d4SFlorian Westphal { 592048d19d4SFlorian Westphal int flags = fcntl(fd, F_GETFL); 593048d19d4SFlorian Westphal 594048d19d4SFlorian Westphal if (flags == -1) 595048d19d4SFlorian Westphal return; 596048d19d4SFlorian Westphal 59705be5e27SPaolo Abeni if (nonblock) 598048d19d4SFlorian Westphal fcntl(fd, F_SETFL, flags | O_NONBLOCK); 59905be5e27SPaolo Abeni else 60005be5e27SPaolo Abeni fcntl(fd, F_SETFL, flags & ~O_NONBLOCK); 601048d19d4SFlorian Westphal } 602048d19d4SFlorian Westphal 603df9e03aeSFlorian Westphal static void shut_wr(int fd) 604df9e03aeSFlorian Westphal { 605df9e03aeSFlorian Westphal /* Close our write side, ev. give some time 606df9e03aeSFlorian Westphal * for address notification and/or checking 607df9e03aeSFlorian Westphal * the current status 608df9e03aeSFlorian Westphal */ 609df9e03aeSFlorian Westphal if (cfg_wait) 610df9e03aeSFlorian Westphal usleep(cfg_wait); 611df9e03aeSFlorian Westphal 612df9e03aeSFlorian Westphal shutdown(fd, SHUT_WR); 613df9e03aeSFlorian Westphal } 614df9e03aeSFlorian Westphal 615ca7ae891SDmytro Shytyi static int copyfd_io_poll(int infd, int peerfd, int outfd, 616ca7ae891SDmytro Shytyi bool *in_closed_after_out, struct wstate *winfo) 617048d19d4SFlorian Westphal { 618048d19d4SFlorian Westphal struct pollfd fds = { 619048d19d4SFlorian Westphal .fd = peerfd, 620048d19d4SFlorian Westphal .events = POLLIN | POLLOUT, 621048d19d4SFlorian Westphal }; 622ca7ae891SDmytro Shytyi unsigned int total_wlen = 0, total_rlen = 0; 623048d19d4SFlorian Westphal 62405be5e27SPaolo Abeni set_nonblock(peerfd, true); 625048d19d4SFlorian Westphal 626048d19d4SFlorian Westphal for (;;) { 627048d19d4SFlorian Westphal char rbuf[8192]; 628048d19d4SFlorian Westphal ssize_t len; 629048d19d4SFlorian Westphal 630*4a753ca5SMenglong Dong if (fds.events == 0 || quit) 631048d19d4SFlorian Westphal break; 632048d19d4SFlorian Westphal 633048d19d4SFlorian Westphal switch (poll(&fds, 1, poll_timeout)) { 634048d19d4SFlorian Westphal case -1: 635048d19d4SFlorian Westphal if (errno == EINTR) 636048d19d4SFlorian Westphal continue; 637048d19d4SFlorian Westphal perror("poll"); 638048d19d4SFlorian Westphal return 1; 639048d19d4SFlorian Westphal case 0: 640048d19d4SFlorian Westphal fprintf(stderr, "%s: poll timed out (events: " 641048d19d4SFlorian Westphal "POLLIN %u, POLLOUT %u)\n", __func__, 642048d19d4SFlorian Westphal fds.events & POLLIN, fds.events & POLLOUT); 643048d19d4SFlorian Westphal return 2; 644048d19d4SFlorian Westphal } 645048d19d4SFlorian Westphal 646048d19d4SFlorian Westphal if (fds.revents & POLLIN) { 6476bf41020SPaolo Abeni ssize_t rb = sizeof(rbuf); 6486bf41020SPaolo Abeni 6496bf41020SPaolo Abeni /* limit the total amount of read data to the trunc value*/ 6506bf41020SPaolo Abeni if (cfg_truncate > 0) { 6516bf41020SPaolo Abeni if (rb + total_rlen > cfg_truncate) 6526bf41020SPaolo Abeni rb = cfg_truncate - total_rlen; 6536bf41020SPaolo Abeni len = read(peerfd, rbuf, rb); 6546bf41020SPaolo Abeni } else { 655048d19d4SFlorian Westphal len = do_rnd_read(peerfd, rbuf, sizeof(rbuf)); 6566bf41020SPaolo Abeni } 657048d19d4SFlorian Westphal if (len == 0) { 658048d19d4SFlorian Westphal /* no more data to receive: 659048d19d4SFlorian Westphal * peer has closed its write side 660048d19d4SFlorian Westphal */ 661048d19d4SFlorian Westphal fds.events &= ~POLLIN; 662048d19d4SFlorian Westphal 663b6ab64b0SPaolo Abeni if ((fds.events & POLLOUT) == 0) { 664b6ab64b0SPaolo Abeni *in_closed_after_out = true; 665048d19d4SFlorian Westphal /* and nothing more to send */ 666048d19d4SFlorian Westphal break; 667b6ab64b0SPaolo Abeni } 668048d19d4SFlorian Westphal 669048d19d4SFlorian Westphal /* Else, still have data to transmit */ 670048d19d4SFlorian Westphal } else if (len < 0) { 6716bf41020SPaolo Abeni if (cfg_rcv_trunc) 6726bf41020SPaolo Abeni return 0; 673048d19d4SFlorian Westphal perror("read"); 674048d19d4SFlorian Westphal return 3; 675048d19d4SFlorian Westphal } 676048d19d4SFlorian Westphal 6776bf41020SPaolo Abeni total_rlen += len; 678048d19d4SFlorian Westphal do_write(outfd, rbuf, len); 679048d19d4SFlorian Westphal } 680048d19d4SFlorian Westphal 681048d19d4SFlorian Westphal if (fds.revents & POLLOUT) { 682ca7ae891SDmytro Shytyi if (winfo->len == 0) { 683ca7ae891SDmytro Shytyi winfo->off = 0; 684ca7ae891SDmytro Shytyi winfo->len = read(infd, winfo->buf, sizeof(winfo->buf)); 685048d19d4SFlorian Westphal } 686048d19d4SFlorian Westphal 687ca7ae891SDmytro Shytyi if (winfo->len > 0) { 688048d19d4SFlorian Westphal ssize_t bw; 689048d19d4SFlorian Westphal 6906bf41020SPaolo Abeni /* limit the total amount of written data to the trunc value */ 691ca7ae891SDmytro Shytyi if (cfg_truncate > 0 && winfo->len + total_wlen > cfg_truncate) 692ca7ae891SDmytro Shytyi winfo->len = cfg_truncate - total_wlen; 6936bf41020SPaolo Abeni 694ca7ae891SDmytro Shytyi bw = do_rnd_write(peerfd, winfo->buf + winfo->off, winfo->len); 6956bf41020SPaolo Abeni if (bw < 0) { 6966bf41020SPaolo Abeni if (cfg_rcv_trunc) 6976bf41020SPaolo Abeni return 0; 6986bf41020SPaolo Abeni perror("write"); 699048d19d4SFlorian Westphal return 111; 7006bf41020SPaolo Abeni } 701048d19d4SFlorian Westphal 702ca7ae891SDmytro Shytyi winfo->off += bw; 703ca7ae891SDmytro Shytyi winfo->len -= bw; 7046bf41020SPaolo Abeni total_wlen += bw; 705ca7ae891SDmytro Shytyi } else if (winfo->len == 0) { 706048d19d4SFlorian Westphal /* We have no more data to send. */ 707048d19d4SFlorian Westphal fds.events &= ~POLLOUT; 708048d19d4SFlorian Westphal 709048d19d4SFlorian Westphal if ((fds.events & POLLIN) == 0) 710048d19d4SFlorian Westphal /* ... and peer also closed already */ 711048d19d4SFlorian Westphal break; 712048d19d4SFlorian Westphal 713df9e03aeSFlorian Westphal shut_wr(peerfd); 714048d19d4SFlorian Westphal } else { 715048d19d4SFlorian Westphal if (errno == EINTR) 716048d19d4SFlorian Westphal continue; 717048d19d4SFlorian Westphal perror("read"); 718048d19d4SFlorian Westphal return 4; 719048d19d4SFlorian Westphal } 720048d19d4SFlorian Westphal } 721048d19d4SFlorian Westphal 722048d19d4SFlorian Westphal if (fds.revents & (POLLERR | POLLNVAL)) { 7236bf41020SPaolo Abeni if (cfg_rcv_trunc) 7246bf41020SPaolo Abeni return 0; 725048d19d4SFlorian Westphal fprintf(stderr, "Unexpected revents: " 726048d19d4SFlorian Westphal "POLLERR/POLLNVAL(%x)\n", fds.revents); 727048d19d4SFlorian Westphal return 5; 728048d19d4SFlorian Westphal } 7296bf41020SPaolo Abeni 7306bf41020SPaolo Abeni if (cfg_truncate > 0 && total_wlen >= cfg_truncate && 7316bf41020SPaolo Abeni total_rlen >= cfg_truncate) 7326bf41020SPaolo Abeni break; 733048d19d4SFlorian Westphal } 734048d19d4SFlorian Westphal 735b08fbf24SPaolo Abeni /* leave some time for late join/announce */ 736*4a753ca5SMenglong Dong if (cfg_remove && !quit) 737df62f2ecSPaolo Abeni usleep(cfg_wait); 738b08fbf24SPaolo Abeni 739048d19d4SFlorian Westphal return 0; 740048d19d4SFlorian Westphal } 741048d19d4SFlorian Westphal 742048d19d4SFlorian Westphal static int do_recvfile(int infd, int outfd) 743048d19d4SFlorian Westphal { 744048d19d4SFlorian Westphal ssize_t r; 745048d19d4SFlorian Westphal 746048d19d4SFlorian Westphal do { 747048d19d4SFlorian Westphal char buf[16384]; 748048d19d4SFlorian Westphal 749048d19d4SFlorian Westphal r = do_rnd_read(infd, buf, sizeof(buf)); 750048d19d4SFlorian Westphal if (r > 0) { 751048d19d4SFlorian Westphal if (write(outfd, buf, r) != r) 752048d19d4SFlorian Westphal break; 753048d19d4SFlorian Westphal } else if (r < 0) { 754048d19d4SFlorian Westphal perror("read"); 755048d19d4SFlorian Westphal } 756048d19d4SFlorian Westphal } while (r > 0); 757048d19d4SFlorian Westphal 758048d19d4SFlorian Westphal return (int)r; 759048d19d4SFlorian Westphal } 760048d19d4SFlorian Westphal 761ca7ae891SDmytro Shytyi static int spool_buf(int fd, struct wstate *winfo) 762ca7ae891SDmytro Shytyi { 763ca7ae891SDmytro Shytyi while (winfo->len) { 764ca7ae891SDmytro Shytyi int ret = write(fd, winfo->buf + winfo->off, winfo->len); 765ca7ae891SDmytro Shytyi 766ca7ae891SDmytro Shytyi if (ret < 0) { 767ca7ae891SDmytro Shytyi perror("write"); 768ca7ae891SDmytro Shytyi return 4; 769ca7ae891SDmytro Shytyi } 770ca7ae891SDmytro Shytyi winfo->off += ret; 771ca7ae891SDmytro Shytyi winfo->len -= ret; 772ca7ae891SDmytro Shytyi } 773ca7ae891SDmytro Shytyi return 0; 774ca7ae891SDmytro Shytyi } 775ca7ae891SDmytro Shytyi 776ca7ae891SDmytro Shytyi static int do_mmap(int infd, int outfd, unsigned int size, 777ca7ae891SDmytro Shytyi struct wstate *winfo) 778048d19d4SFlorian Westphal { 779048d19d4SFlorian Westphal char *inbuf = mmap(NULL, size, PROT_READ, MAP_SHARED, infd, 0); 780ca7ae891SDmytro Shytyi ssize_t ret = 0, off = winfo->total_len; 781048d19d4SFlorian Westphal size_t rem; 782048d19d4SFlorian Westphal 783048d19d4SFlorian Westphal if (inbuf == MAP_FAILED) { 784048d19d4SFlorian Westphal perror("mmap"); 785048d19d4SFlorian Westphal return 1; 786048d19d4SFlorian Westphal } 787048d19d4SFlorian Westphal 788ca7ae891SDmytro Shytyi ret = spool_buf(outfd, winfo); 789ca7ae891SDmytro Shytyi if (ret < 0) 790ca7ae891SDmytro Shytyi return ret; 791ca7ae891SDmytro Shytyi 792ca7ae891SDmytro Shytyi rem = size - winfo->total_len; 793048d19d4SFlorian Westphal 794048d19d4SFlorian Westphal while (rem > 0) { 795048d19d4SFlorian Westphal ret = write(outfd, inbuf + off, rem); 796048d19d4SFlorian Westphal 797048d19d4SFlorian Westphal if (ret < 0) { 798048d19d4SFlorian Westphal perror("write"); 799048d19d4SFlorian Westphal break; 800048d19d4SFlorian Westphal } 801048d19d4SFlorian Westphal 802048d19d4SFlorian Westphal off += ret; 803048d19d4SFlorian Westphal rem -= ret; 804048d19d4SFlorian Westphal } 805048d19d4SFlorian Westphal 806048d19d4SFlorian Westphal munmap(inbuf, size); 807048d19d4SFlorian Westphal return rem; 808048d19d4SFlorian Westphal } 809048d19d4SFlorian Westphal 810048d19d4SFlorian Westphal static int get_infd_size(int fd) 811048d19d4SFlorian Westphal { 812048d19d4SFlorian Westphal struct stat sb; 813048d19d4SFlorian Westphal ssize_t count; 814048d19d4SFlorian Westphal int err; 815048d19d4SFlorian Westphal 816048d19d4SFlorian Westphal err = fstat(fd, &sb); 817048d19d4SFlorian Westphal if (err < 0) { 818048d19d4SFlorian Westphal perror("fstat"); 819048d19d4SFlorian Westphal return -1; 820048d19d4SFlorian Westphal } 821048d19d4SFlorian Westphal 822048d19d4SFlorian Westphal if ((sb.st_mode & S_IFMT) != S_IFREG) { 823048d19d4SFlorian Westphal fprintf(stderr, "%s: stdin is not a regular file\n", __func__); 824048d19d4SFlorian Westphal return -2; 825048d19d4SFlorian Westphal } 826048d19d4SFlorian Westphal 827048d19d4SFlorian Westphal count = sb.st_size; 828048d19d4SFlorian Westphal if (count > INT_MAX) { 829048d19d4SFlorian Westphal fprintf(stderr, "File too large: %zu\n", count); 830048d19d4SFlorian Westphal return -3; 831048d19d4SFlorian Westphal } 832048d19d4SFlorian Westphal 833048d19d4SFlorian Westphal return (int)count; 834048d19d4SFlorian Westphal } 835048d19d4SFlorian Westphal 836ca7ae891SDmytro Shytyi static int do_sendfile(int infd, int outfd, unsigned int count, 837ca7ae891SDmytro Shytyi struct wstate *winfo) 838048d19d4SFlorian Westphal { 839ca7ae891SDmytro Shytyi int ret = spool_buf(outfd, winfo); 840ca7ae891SDmytro Shytyi 841ca7ae891SDmytro Shytyi if (ret < 0) 842ca7ae891SDmytro Shytyi return ret; 843ca7ae891SDmytro Shytyi 844ca7ae891SDmytro Shytyi count -= winfo->total_len; 845ca7ae891SDmytro Shytyi 846048d19d4SFlorian Westphal while (count > 0) { 847048d19d4SFlorian Westphal ssize_t r; 848048d19d4SFlorian Westphal 849048d19d4SFlorian Westphal r = sendfile(outfd, infd, NULL, count); 850048d19d4SFlorian Westphal if (r < 0) { 851048d19d4SFlorian Westphal perror("sendfile"); 852048d19d4SFlorian Westphal return 3; 853048d19d4SFlorian Westphal } 854048d19d4SFlorian Westphal 855048d19d4SFlorian Westphal count -= r; 856048d19d4SFlorian Westphal } 857048d19d4SFlorian Westphal 858048d19d4SFlorian Westphal return 0; 859048d19d4SFlorian Westphal } 860048d19d4SFlorian Westphal 861048d19d4SFlorian Westphal static int copyfd_io_mmap(int infd, int peerfd, int outfd, 862ca7ae891SDmytro Shytyi unsigned int size, bool *in_closed_after_out, 863ca7ae891SDmytro Shytyi struct wstate *winfo) 864048d19d4SFlorian Westphal { 865048d19d4SFlorian Westphal int err; 866048d19d4SFlorian Westphal 867048d19d4SFlorian Westphal if (listen_mode) { 868048d19d4SFlorian Westphal err = do_recvfile(peerfd, outfd); 869048d19d4SFlorian Westphal if (err) 870048d19d4SFlorian Westphal return err; 871048d19d4SFlorian Westphal 872ca7ae891SDmytro Shytyi err = do_mmap(infd, peerfd, size, winfo); 873048d19d4SFlorian Westphal } else { 874ca7ae891SDmytro Shytyi err = do_mmap(infd, peerfd, size, winfo); 875048d19d4SFlorian Westphal if (err) 876048d19d4SFlorian Westphal return err; 877048d19d4SFlorian Westphal 878df9e03aeSFlorian Westphal shut_wr(peerfd); 879048d19d4SFlorian Westphal 880048d19d4SFlorian Westphal err = do_recvfile(peerfd, outfd); 881b6ab64b0SPaolo Abeni *in_closed_after_out = true; 882048d19d4SFlorian Westphal } 883048d19d4SFlorian Westphal 884048d19d4SFlorian Westphal return err; 885048d19d4SFlorian Westphal } 886048d19d4SFlorian Westphal 887048d19d4SFlorian Westphal static int copyfd_io_sendfile(int infd, int peerfd, int outfd, 888ca7ae891SDmytro Shytyi unsigned int size, bool *in_closed_after_out, struct wstate *winfo) 889048d19d4SFlorian Westphal { 890048d19d4SFlorian Westphal int err; 891048d19d4SFlorian Westphal 892048d19d4SFlorian Westphal if (listen_mode) { 893048d19d4SFlorian Westphal err = do_recvfile(peerfd, outfd); 894048d19d4SFlorian Westphal if (err) 895048d19d4SFlorian Westphal return err; 896048d19d4SFlorian Westphal 897ca7ae891SDmytro Shytyi err = do_sendfile(infd, peerfd, size, winfo); 898048d19d4SFlorian Westphal } else { 899ca7ae891SDmytro Shytyi err = do_sendfile(infd, peerfd, size, winfo); 900048d19d4SFlorian Westphal if (err) 901048d19d4SFlorian Westphal return err; 902df9e03aeSFlorian Westphal 903df9e03aeSFlorian Westphal shut_wr(peerfd); 904df9e03aeSFlorian Westphal 905048d19d4SFlorian Westphal err = do_recvfile(peerfd, outfd); 906b6ab64b0SPaolo Abeni *in_closed_after_out = true; 907048d19d4SFlorian Westphal } 908048d19d4SFlorian Westphal 909048d19d4SFlorian Westphal return err; 910048d19d4SFlorian Westphal } 911048d19d4SFlorian Westphal 912ca7ae891SDmytro Shytyi static int copyfd_io(int infd, int peerfd, int outfd, bool close_peerfd, struct wstate *winfo) 913048d19d4SFlorian Westphal { 914b6ab64b0SPaolo Abeni bool in_closed_after_out = false; 915b6ab64b0SPaolo Abeni struct timespec start, end; 916048d19d4SFlorian Westphal int file_size; 917b6ab64b0SPaolo Abeni int ret; 918b6ab64b0SPaolo Abeni 919b6ab64b0SPaolo Abeni if (cfg_time && (clock_gettime(CLOCK_MONOTONIC, &start) < 0)) 920b6ab64b0SPaolo Abeni xerror("can not fetch start time %d", errno); 921048d19d4SFlorian Westphal 922048d19d4SFlorian Westphal switch (cfg_mode) { 923048d19d4SFlorian Westphal case CFG_MODE_POLL: 924ca7ae891SDmytro Shytyi ret = copyfd_io_poll(infd, peerfd, outfd, &in_closed_after_out, 925ca7ae891SDmytro Shytyi winfo); 926b6ab64b0SPaolo Abeni break; 927b6ab64b0SPaolo Abeni 928048d19d4SFlorian Westphal case CFG_MODE_MMAP: 929048d19d4SFlorian Westphal file_size = get_infd_size(infd); 930048d19d4SFlorian Westphal if (file_size < 0) 931048d19d4SFlorian Westphal return file_size; 932ca7ae891SDmytro Shytyi ret = copyfd_io_mmap(infd, peerfd, outfd, file_size, 933ca7ae891SDmytro Shytyi &in_closed_after_out, winfo); 934b6ab64b0SPaolo Abeni break; 935b6ab64b0SPaolo Abeni 936048d19d4SFlorian Westphal case CFG_MODE_SENDFILE: 937048d19d4SFlorian Westphal file_size = get_infd_size(infd); 938048d19d4SFlorian Westphal if (file_size < 0) 939048d19d4SFlorian Westphal return file_size; 940ca7ae891SDmytro Shytyi ret = copyfd_io_sendfile(infd, peerfd, outfd, file_size, 941ca7ae891SDmytro Shytyi &in_closed_after_out, winfo); 942b6ab64b0SPaolo Abeni break; 943048d19d4SFlorian Westphal 944b6ab64b0SPaolo Abeni default: 945048d19d4SFlorian Westphal fprintf(stderr, "Invalid mode %d\n", cfg_mode); 946048d19d4SFlorian Westphal 947048d19d4SFlorian Westphal die_usage(); 948048d19d4SFlorian Westphal return 1; 949048d19d4SFlorian Westphal } 950048d19d4SFlorian Westphal 951b6ab64b0SPaolo Abeni if (ret) 952b6ab64b0SPaolo Abeni return ret; 953b6ab64b0SPaolo Abeni 95405be5e27SPaolo Abeni if (close_peerfd) 95505be5e27SPaolo Abeni close(peerfd); 95605be5e27SPaolo Abeni 957b6ab64b0SPaolo Abeni if (cfg_time) { 958b6ab64b0SPaolo Abeni unsigned int delta_ms; 959b6ab64b0SPaolo Abeni 960b6ab64b0SPaolo Abeni if (clock_gettime(CLOCK_MONOTONIC, &end) < 0) 961b6ab64b0SPaolo Abeni xerror("can not fetch end time %d", errno); 962b6ab64b0SPaolo Abeni delta_ms = (end.tv_sec - start.tv_sec) * 1000 + (end.tv_nsec - start.tv_nsec) / 1000000; 963b6ab64b0SPaolo Abeni if (delta_ms > cfg_time) { 964b6ab64b0SPaolo Abeni xerror("transfer slower than expected! runtime %d ms, expected %d ms", 965b6ab64b0SPaolo Abeni delta_ms, cfg_time); 966b6ab64b0SPaolo Abeni } 967b6ab64b0SPaolo Abeni 968b6ab64b0SPaolo Abeni /* show the runtime only if this end shutdown(wr) before receiving the EOF, 969b6ab64b0SPaolo Abeni * (that is, if this end got the longer runtime) 970b6ab64b0SPaolo Abeni */ 971b6ab64b0SPaolo Abeni if (in_closed_after_out) 972b6ab64b0SPaolo Abeni fprintf(stderr, "%d", delta_ms); 973b6ab64b0SPaolo Abeni } 974b6ab64b0SPaolo Abeni 975b6ab64b0SPaolo Abeni return 0; 976b6ab64b0SPaolo Abeni } 977b6ab64b0SPaolo Abeni 978048d19d4SFlorian Westphal static void check_sockaddr(int pf, struct sockaddr_storage *ss, 979048d19d4SFlorian Westphal socklen_t salen) 980048d19d4SFlorian Westphal { 981048d19d4SFlorian Westphal struct sockaddr_in6 *sin6; 982048d19d4SFlorian Westphal struct sockaddr_in *sin; 983048d19d4SFlorian Westphal socklen_t wanted_size = 0; 984048d19d4SFlorian Westphal 985048d19d4SFlorian Westphal switch (pf) { 986048d19d4SFlorian Westphal case AF_INET: 987048d19d4SFlorian Westphal wanted_size = sizeof(*sin); 988048d19d4SFlorian Westphal sin = (void *)ss; 989048d19d4SFlorian Westphal if (!sin->sin_port) 990048d19d4SFlorian Westphal fprintf(stderr, "accept: something wrong: ip connection from port 0"); 991048d19d4SFlorian Westphal break; 992048d19d4SFlorian Westphal case AF_INET6: 993048d19d4SFlorian Westphal wanted_size = sizeof(*sin6); 994048d19d4SFlorian Westphal sin6 = (void *)ss; 995048d19d4SFlorian Westphal if (!sin6->sin6_port) 996048d19d4SFlorian Westphal fprintf(stderr, "accept: something wrong: ipv6 connection from port 0"); 997048d19d4SFlorian Westphal break; 998048d19d4SFlorian Westphal default: 999048d19d4SFlorian Westphal fprintf(stderr, "accept: Unknown pf %d, salen %u\n", pf, salen); 1000048d19d4SFlorian Westphal return; 1001048d19d4SFlorian Westphal } 1002048d19d4SFlorian Westphal 1003048d19d4SFlorian Westphal if (salen != wanted_size) 1004048d19d4SFlorian Westphal fprintf(stderr, "accept: size mismatch, got %d expected %d\n", 1005048d19d4SFlorian Westphal (int)salen, wanted_size); 1006048d19d4SFlorian Westphal 1007048d19d4SFlorian Westphal if (ss->ss_family != pf) 1008048d19d4SFlorian Westphal fprintf(stderr, "accept: pf mismatch, expect %d, ss_family is %d\n", 1009048d19d4SFlorian Westphal (int)ss->ss_family, pf); 1010048d19d4SFlorian Westphal } 1011048d19d4SFlorian Westphal 1012048d19d4SFlorian Westphal static void check_getpeername(int fd, struct sockaddr_storage *ss, socklen_t salen) 1013048d19d4SFlorian Westphal { 1014048d19d4SFlorian Westphal struct sockaddr_storage peerss; 1015048d19d4SFlorian Westphal socklen_t peersalen = sizeof(peerss); 1016048d19d4SFlorian Westphal 1017048d19d4SFlorian Westphal if (getpeername(fd, (struct sockaddr *)&peerss, &peersalen) < 0) { 1018048d19d4SFlorian Westphal perror("getpeername"); 1019048d19d4SFlorian Westphal return; 1020048d19d4SFlorian Westphal } 1021048d19d4SFlorian Westphal 1022048d19d4SFlorian Westphal if (peersalen != salen) { 1023048d19d4SFlorian Westphal fprintf(stderr, "%s: %d vs %d\n", __func__, peersalen, salen); 1024048d19d4SFlorian Westphal return; 1025048d19d4SFlorian Westphal } 1026048d19d4SFlorian Westphal 1027048d19d4SFlorian Westphal if (memcmp(ss, &peerss, peersalen)) { 1028048d19d4SFlorian Westphal char a[INET6_ADDRSTRLEN]; 1029048d19d4SFlorian Westphal char b[INET6_ADDRSTRLEN]; 1030048d19d4SFlorian Westphal char c[INET6_ADDRSTRLEN]; 1031048d19d4SFlorian Westphal char d[INET6_ADDRSTRLEN]; 1032048d19d4SFlorian Westphal 1033048d19d4SFlorian Westphal xgetnameinfo((struct sockaddr *)ss, salen, 1034048d19d4SFlorian Westphal a, sizeof(a), b, sizeof(b)); 1035048d19d4SFlorian Westphal 1036048d19d4SFlorian Westphal xgetnameinfo((struct sockaddr *)&peerss, peersalen, 1037048d19d4SFlorian Westphal c, sizeof(c), d, sizeof(d)); 1038048d19d4SFlorian Westphal 1039048d19d4SFlorian Westphal fprintf(stderr, "%s: memcmp failure: accept %s vs peername %s, %s vs %s salen %d vs %d\n", 1040048d19d4SFlorian Westphal __func__, a, c, b, d, peersalen, salen); 1041048d19d4SFlorian Westphal } 1042048d19d4SFlorian Westphal } 1043048d19d4SFlorian Westphal 1044048d19d4SFlorian Westphal static void check_getpeername_connect(int fd) 1045048d19d4SFlorian Westphal { 1046048d19d4SFlorian Westphal struct sockaddr_storage ss; 1047048d19d4SFlorian Westphal socklen_t salen = sizeof(ss); 1048048d19d4SFlorian Westphal char a[INET6_ADDRSTRLEN]; 1049048d19d4SFlorian Westphal char b[INET6_ADDRSTRLEN]; 1050048d19d4SFlorian Westphal 1051048d19d4SFlorian Westphal if (getpeername(fd, (struct sockaddr *)&ss, &salen) < 0) { 1052048d19d4SFlorian Westphal perror("getpeername"); 1053048d19d4SFlorian Westphal return; 1054048d19d4SFlorian Westphal } 1055048d19d4SFlorian Westphal 1056048d19d4SFlorian Westphal xgetnameinfo((struct sockaddr *)&ss, salen, 1057048d19d4SFlorian Westphal a, sizeof(a), b, sizeof(b)); 1058048d19d4SFlorian Westphal 1059048d19d4SFlorian Westphal if (strcmp(cfg_host, a) || strcmp(cfg_port, b)) 1060048d19d4SFlorian Westphal fprintf(stderr, "%s: %s vs %s, %s vs %s\n", __func__, 1061048d19d4SFlorian Westphal cfg_host, a, cfg_port, b); 1062048d19d4SFlorian Westphal } 1063048d19d4SFlorian Westphal 1064b0519de8SFlorian Westphal static void maybe_close(int fd) 1065b0519de8SFlorian Westphal { 1066b0519de8SFlorian Westphal unsigned int r = rand(); 1067b0519de8SFlorian Westphal 106805be5e27SPaolo Abeni if (!(cfg_join || cfg_remove || cfg_repeat > 1) && (r & 1)) 1069b0519de8SFlorian Westphal close(fd); 1070b0519de8SFlorian Westphal } 1071b0519de8SFlorian Westphal 1072048d19d4SFlorian Westphal int main_loop_s(int listensock) 1073048d19d4SFlorian Westphal { 1074048d19d4SFlorian Westphal struct sockaddr_storage ss; 1075ca7ae891SDmytro Shytyi struct wstate winfo; 1076048d19d4SFlorian Westphal struct pollfd polls; 1077048d19d4SFlorian Westphal socklen_t salen; 1078048d19d4SFlorian Westphal int remotesock; 107905be5e27SPaolo Abeni int fd = 0; 1080048d19d4SFlorian Westphal 108105be5e27SPaolo Abeni again: 1082048d19d4SFlorian Westphal polls.fd = listensock; 1083048d19d4SFlorian Westphal polls.events = POLLIN; 1084048d19d4SFlorian Westphal 1085048d19d4SFlorian Westphal switch (poll(&polls, 1, poll_timeout)) { 1086048d19d4SFlorian Westphal case -1: 1087048d19d4SFlorian Westphal perror("poll"); 1088048d19d4SFlorian Westphal return 1; 1089048d19d4SFlorian Westphal case 0: 1090048d19d4SFlorian Westphal fprintf(stderr, "%s: timed out\n", __func__); 1091048d19d4SFlorian Westphal close(listensock); 1092048d19d4SFlorian Westphal return 2; 1093048d19d4SFlorian Westphal } 1094048d19d4SFlorian Westphal 1095048d19d4SFlorian Westphal salen = sizeof(ss); 1096048d19d4SFlorian Westphal remotesock = accept(listensock, (struct sockaddr *)&ss, &salen); 1097048d19d4SFlorian Westphal if (remotesock >= 0) { 1098b0519de8SFlorian Westphal maybe_close(listensock); 1099048d19d4SFlorian Westphal check_sockaddr(pf, &ss, salen); 1100048d19d4SFlorian Westphal check_getpeername(remotesock, &ss, salen); 1101048d19d4SFlorian Westphal 110205be5e27SPaolo Abeni if (cfg_input) { 110305be5e27SPaolo Abeni fd = open(cfg_input, O_RDONLY); 110405be5e27SPaolo Abeni if (fd < 0) 110505be5e27SPaolo Abeni xerror("can't open %s: %d", cfg_input, errno); 1106048d19d4SFlorian Westphal } 1107048d19d4SFlorian Westphal 110805be5e27SPaolo Abeni SOCK_TEST_TCPULP(remotesock, 0); 1109048d19d4SFlorian Westphal 1110ca7ae891SDmytro Shytyi memset(&winfo, 0, sizeof(winfo)); 1111ca7ae891SDmytro Shytyi copyfd_io(fd, remotesock, 1, true, &winfo); 111205be5e27SPaolo Abeni } else { 111305be5e27SPaolo Abeni perror("accept"); 1114048d19d4SFlorian Westphal return 1; 1115048d19d4SFlorian Westphal } 1116048d19d4SFlorian Westphal 111705be5e27SPaolo Abeni if (--cfg_repeat > 0) { 111805be5e27SPaolo Abeni if (cfg_input) 111905be5e27SPaolo Abeni close(fd); 112005be5e27SPaolo Abeni goto again; 112105be5e27SPaolo Abeni } 112205be5e27SPaolo Abeni 112305be5e27SPaolo Abeni return 0; 112405be5e27SPaolo Abeni } 112505be5e27SPaolo Abeni 1126048d19d4SFlorian Westphal static void init_rng(void) 1127048d19d4SFlorian Westphal { 1128048d19d4SFlorian Westphal int fd = open("/dev/urandom", O_RDONLY); 1129048d19d4SFlorian Westphal unsigned int foo; 1130048d19d4SFlorian Westphal 1131048d19d4SFlorian Westphal if (fd > 0) { 1132048d19d4SFlorian Westphal int ret = read(fd, &foo, sizeof(foo)); 1133048d19d4SFlorian Westphal 1134048d19d4SFlorian Westphal if (ret < 0) 1135048d19d4SFlorian Westphal srand(fd + foo); 1136048d19d4SFlorian Westphal close(fd); 1137048d19d4SFlorian Westphal } 1138048d19d4SFlorian Westphal 1139048d19d4SFlorian Westphal srand(foo); 1140048d19d4SFlorian Westphal } 1141048d19d4SFlorian Westphal 11425e6af0a7SFlorian Westphal static void xsetsockopt(int fd, int level, int optname, const void *optval, socklen_t optlen) 11435e6af0a7SFlorian Westphal { 11445e6af0a7SFlorian Westphal int err; 11455e6af0a7SFlorian Westphal 11465e6af0a7SFlorian Westphal err = setsockopt(fd, level, optname, optval, optlen); 11475e6af0a7SFlorian Westphal if (err) { 11485e6af0a7SFlorian Westphal perror("setsockopt"); 11495e6af0a7SFlorian Westphal exit(1); 11505e6af0a7SFlorian Westphal } 11515e6af0a7SFlorian Westphal } 11525e6af0a7SFlorian Westphal 11535e6af0a7SFlorian Westphal static void apply_cmsg_types(int fd, const struct cfg_cmsg_types *cmsg) 11545e6af0a7SFlorian Westphal { 11555e6af0a7SFlorian Westphal static const unsigned int on = 1; 11565e6af0a7SFlorian Westphal 11575e6af0a7SFlorian Westphal if (cmsg->timestampns) 11585e6af0a7SFlorian Westphal xsetsockopt(fd, SOL_SOCKET, SO_TIMESTAMPNS_NEW, &on, sizeof(on)); 11595cbd886cSFlorian Westphal if (cmsg->tcp_inq) 11605cbd886cSFlorian Westphal xsetsockopt(fd, IPPROTO_TCP, TCP_INQ, &on, sizeof(on)); 11615e6af0a7SFlorian Westphal } 11625e6af0a7SFlorian Westphal 11635e6af0a7SFlorian Westphal static void parse_cmsg_types(const char *type) 11645e6af0a7SFlorian Westphal { 11655e6af0a7SFlorian Westphal char *next = strchr(type, ','); 11665e6af0a7SFlorian Westphal unsigned int len = 0; 11675e6af0a7SFlorian Westphal 11685e6af0a7SFlorian Westphal cfg_cmsg_types.cmsg_enabled = 1; 11695e6af0a7SFlorian Westphal 11705e6af0a7SFlorian Westphal if (next) { 11715e6af0a7SFlorian Westphal parse_cmsg_types(next + 1); 11725e6af0a7SFlorian Westphal len = next - type; 11735e6af0a7SFlorian Westphal } else { 11745e6af0a7SFlorian Westphal len = strlen(type); 11755e6af0a7SFlorian Westphal } 11765e6af0a7SFlorian Westphal 11775e6af0a7SFlorian Westphal if (strncmp(type, "TIMESTAMPNS", len) == 0) { 11785e6af0a7SFlorian Westphal cfg_cmsg_types.timestampns = 1; 11795e6af0a7SFlorian Westphal return; 11805e6af0a7SFlorian Westphal } 11815e6af0a7SFlorian Westphal 11825cbd886cSFlorian Westphal if (strncmp(type, "TCPINQ", len) == 0) { 11835cbd886cSFlorian Westphal cfg_cmsg_types.tcp_inq = 1; 11845cbd886cSFlorian Westphal return; 11855cbd886cSFlorian Westphal } 11865cbd886cSFlorian Westphal 11875e6af0a7SFlorian Westphal fprintf(stderr, "Unrecognized cmsg option %s\n", type); 11885e6af0a7SFlorian Westphal exit(1); 11895e6af0a7SFlorian Westphal } 11905e6af0a7SFlorian Westphal 11915fb62e9cSFlorian Westphal static void parse_setsock_options(const char *name) 11925fb62e9cSFlorian Westphal { 11935fb62e9cSFlorian Westphal char *next = strchr(name, ','); 11945fb62e9cSFlorian Westphal unsigned int len = 0; 11955fb62e9cSFlorian Westphal 11965fb62e9cSFlorian Westphal if (next) { 11975fb62e9cSFlorian Westphal parse_setsock_options(next + 1); 11985fb62e9cSFlorian Westphal len = next - name; 11995fb62e9cSFlorian Westphal } else { 12005fb62e9cSFlorian Westphal len = strlen(name); 12015fb62e9cSFlorian Westphal } 12025fb62e9cSFlorian Westphal 12035fb62e9cSFlorian Westphal if (strncmp(name, "TRANSPARENT", len) == 0) { 12045fb62e9cSFlorian Westphal cfg_sockopt_types.transparent = 1; 12055fb62e9cSFlorian Westphal return; 12065fb62e9cSFlorian Westphal } 12075fb62e9cSFlorian Westphal 1208ca7ae891SDmytro Shytyi if (strncmp(name, "MPTFO", len) == 0) { 1209ca7ae891SDmytro Shytyi cfg_sockopt_types.mptfo = 1; 1210ca7ae891SDmytro Shytyi return; 1211ca7ae891SDmytro Shytyi } 1212ca7ae891SDmytro Shytyi 12135fb62e9cSFlorian Westphal fprintf(stderr, "Unrecognized setsockopt option %s\n", name); 12145fb62e9cSFlorian Westphal exit(1); 12155fb62e9cSFlorian Westphal } 12165fb62e9cSFlorian Westphal 121705be5e27SPaolo Abeni void xdisconnect(int fd, int addrlen) 121805be5e27SPaolo Abeni { 121905be5e27SPaolo Abeni struct sockaddr_storage empty; 122005be5e27SPaolo Abeni int msec_sleep = 10; 122105be5e27SPaolo Abeni int queued = 1; 122205be5e27SPaolo Abeni int i; 122305be5e27SPaolo Abeni 122405be5e27SPaolo Abeni shutdown(fd, SHUT_WR); 122505be5e27SPaolo Abeni 122605be5e27SPaolo Abeni /* while until the pending data is completely flushed, the later 122705be5e27SPaolo Abeni * disconnect will bypass/ignore/drop any pending data. 122805be5e27SPaolo Abeni */ 122905be5e27SPaolo Abeni for (i = 0; ; i += msec_sleep) { 123005be5e27SPaolo Abeni if (ioctl(fd, SIOCOUTQ, &queued) < 0) 123105be5e27SPaolo Abeni xerror("can't query out socket queue: %d", errno); 123205be5e27SPaolo Abeni 123305be5e27SPaolo Abeni if (!queued) 123405be5e27SPaolo Abeni break; 123505be5e27SPaolo Abeni 123605be5e27SPaolo Abeni if (i > poll_timeout) 123705be5e27SPaolo Abeni xerror("timeout while waiting for spool to complete"); 123805be5e27SPaolo Abeni usleep(msec_sleep * 1000); 123905be5e27SPaolo Abeni } 124005be5e27SPaolo Abeni 124105be5e27SPaolo Abeni memset(&empty, 0, sizeof(empty)); 124205be5e27SPaolo Abeni empty.ss_family = AF_UNSPEC; 124305be5e27SPaolo Abeni if (connect(fd, (struct sockaddr *)&empty, addrlen) < 0) 124405be5e27SPaolo Abeni xerror("can't disconnect: %d", errno); 124505be5e27SPaolo Abeni } 124605be5e27SPaolo Abeni 1247048d19d4SFlorian Westphal int main_loop(void) 1248048d19d4SFlorian Westphal { 1249ca7ae891SDmytro Shytyi int fd = 0, ret, fd_in = 0; 125005be5e27SPaolo Abeni struct addrinfo *peer; 1251ca7ae891SDmytro Shytyi struct wstate winfo; 1252048d19d4SFlorian Westphal 1253ca7ae891SDmytro Shytyi if (cfg_input && cfg_sockopt_types.mptfo) { 1254ca7ae891SDmytro Shytyi fd_in = open(cfg_input, O_RDONLY); 1255ca7ae891SDmytro Shytyi if (fd < 0) 1256ca7ae891SDmytro Shytyi xerror("can't open %s:%d", cfg_input, errno); 1257ca7ae891SDmytro Shytyi } 1258ca7ae891SDmytro Shytyi 1259ca7ae891SDmytro Shytyi memset(&winfo, 0, sizeof(winfo)); 1260ca7ae891SDmytro Shytyi fd = sock_connect_mptcp(cfg_host, cfg_port, cfg_sock_proto, &peer, fd_in, &winfo); 1261048d19d4SFlorian Westphal if (fd < 0) 1262048d19d4SFlorian Westphal return 2; 1263048d19d4SFlorian Westphal 126405be5e27SPaolo Abeni again: 1265048d19d4SFlorian Westphal check_getpeername_connect(fd); 1266048d19d4SFlorian Westphal 1267f730b65cSFlorian Westphal SOCK_TEST_TCPULP(fd, cfg_sock_proto); 1268f730b65cSFlorian Westphal 12698a4b910dSFlorian Westphal if (cfg_rcvbuf) 12708a4b910dSFlorian Westphal set_rcvbuf(fd, cfg_rcvbuf); 1271048d19d4SFlorian Westphal if (cfg_sndbuf) 1272048d19d4SFlorian Westphal set_sndbuf(fd, cfg_sndbuf); 12735e6af0a7SFlorian Westphal if (cfg_cmsg_types.cmsg_enabled) 12745e6af0a7SFlorian Westphal apply_cmsg_types(fd, &cfg_cmsg_types); 1275048d19d4SFlorian Westphal 1276ca7ae891SDmytro Shytyi if (cfg_input && !cfg_sockopt_types.mptfo) { 127705be5e27SPaolo Abeni fd_in = open(cfg_input, O_RDONLY); 127805be5e27SPaolo Abeni if (fd < 0) 127905be5e27SPaolo Abeni xerror("can't open %s:%d", cfg_input, errno); 128005be5e27SPaolo Abeni } 128105be5e27SPaolo Abeni 1282ca7ae891SDmytro Shytyi ret = copyfd_io(fd_in, fd, 1, 0, &winfo); 128305be5e27SPaolo Abeni if (ret) 128405be5e27SPaolo Abeni return ret; 128505be5e27SPaolo Abeni 12866bf41020SPaolo Abeni if (cfg_truncate > 0) { 12876bf41020SPaolo Abeni xdisconnect(fd, peer->ai_addrlen); 12886bf41020SPaolo Abeni } else if (--cfg_repeat > 0) { 128905be5e27SPaolo Abeni xdisconnect(fd, peer->ai_addrlen); 129005be5e27SPaolo Abeni 129105be5e27SPaolo Abeni /* the socket could be unblocking at this point, we need the 129205be5e27SPaolo Abeni * connect to be blocking 129305be5e27SPaolo Abeni */ 129405be5e27SPaolo Abeni set_nonblock(fd, false); 129505be5e27SPaolo Abeni if (connect(fd, peer->ai_addr, peer->ai_addrlen)) 129605be5e27SPaolo Abeni xerror("can't reconnect: %d", errno); 129705be5e27SPaolo Abeni if (cfg_input) 129805be5e27SPaolo Abeni close(fd_in); 1299ca7ae891SDmytro Shytyi memset(&winfo, 0, sizeof(winfo)); 130005be5e27SPaolo Abeni goto again; 13016bf41020SPaolo Abeni } else { 13026bf41020SPaolo Abeni close(fd); 130305be5e27SPaolo Abeni } 13046bf41020SPaolo Abeni 130505be5e27SPaolo Abeni return 0; 1306048d19d4SFlorian Westphal } 1307048d19d4SFlorian Westphal 1308048d19d4SFlorian Westphal int parse_proto(const char *proto) 1309048d19d4SFlorian Westphal { 1310048d19d4SFlorian Westphal if (!strcasecmp(proto, "MPTCP")) 1311048d19d4SFlorian Westphal return IPPROTO_MPTCP; 1312048d19d4SFlorian Westphal if (!strcasecmp(proto, "TCP")) 1313048d19d4SFlorian Westphal return IPPROTO_TCP; 1314048d19d4SFlorian Westphal 1315048d19d4SFlorian Westphal fprintf(stderr, "Unknown protocol: %s\n.", proto); 1316048d19d4SFlorian Westphal die_usage(); 1317048d19d4SFlorian Westphal 1318048d19d4SFlorian Westphal /* silence compiler warning */ 1319048d19d4SFlorian Westphal return 0; 1320048d19d4SFlorian Westphal } 1321048d19d4SFlorian Westphal 1322048d19d4SFlorian Westphal int parse_mode(const char *mode) 1323048d19d4SFlorian Westphal { 1324048d19d4SFlorian Westphal if (!strcasecmp(mode, "poll")) 1325048d19d4SFlorian Westphal return CFG_MODE_POLL; 1326048d19d4SFlorian Westphal if (!strcasecmp(mode, "mmap")) 1327048d19d4SFlorian Westphal return CFG_MODE_MMAP; 1328048d19d4SFlorian Westphal if (!strcasecmp(mode, "sendfile")) 1329048d19d4SFlorian Westphal return CFG_MODE_SENDFILE; 1330048d19d4SFlorian Westphal 1331048d19d4SFlorian Westphal fprintf(stderr, "Unknown test mode: %s\n", mode); 1332048d19d4SFlorian Westphal fprintf(stderr, "Supported modes are:\n"); 1333048d19d4SFlorian Westphal fprintf(stderr, "\t\t\"poll\" - interleaved read/write using poll()\n"); 1334048d19d4SFlorian Westphal fprintf(stderr, "\t\t\"mmap\" - send entire input file (mmap+write), then read response (-l will read input first)\n"); 1335048d19d4SFlorian Westphal fprintf(stderr, "\t\t\"sendfile\" - send entire input file (sendfile), then read response (-l will read input first)\n"); 1336048d19d4SFlorian Westphal 1337048d19d4SFlorian Westphal die_usage(); 1338048d19d4SFlorian Westphal 1339048d19d4SFlorian Westphal /* silence compiler warning */ 1340048d19d4SFlorian Westphal return 0; 1341048d19d4SFlorian Westphal } 1342048d19d4SFlorian Westphal 1343df8aee6dSYonglong Li int parse_peek(const char *mode) 1344df8aee6dSYonglong Li { 1345df8aee6dSYonglong Li if (!strcasecmp(mode, "saveWithPeek")) 1346df8aee6dSYonglong Li return CFG_WITH_PEEK; 1347df8aee6dSYonglong Li if (!strcasecmp(mode, "saveAfterPeek")) 1348df8aee6dSYonglong Li return CFG_AFTER_PEEK; 1349df8aee6dSYonglong Li 1350df8aee6dSYonglong Li fprintf(stderr, "Unknown: %s\n", mode); 1351df8aee6dSYonglong Li fprintf(stderr, "Supported MSG_PEEK mode are:\n"); 1352df8aee6dSYonglong Li fprintf(stderr, 1353df8aee6dSYonglong Li "\t\t\"saveWithPeek\" - recv data with flags 'MSG_PEEK' and save the peek data into file\n"); 1354df8aee6dSYonglong Li fprintf(stderr, 1355df8aee6dSYonglong Li "\t\t\"saveAfterPeek\" - read and save data into file after recv with flags 'MSG_PEEK'\n"); 1356df8aee6dSYonglong Li 1357df8aee6dSYonglong Li die_usage(); 1358df8aee6dSYonglong Li 1359df8aee6dSYonglong Li /* silence compiler warning */ 1360df8aee6dSYonglong Li return 0; 1361df8aee6dSYonglong Li } 1362df8aee6dSYonglong Li 13638a4b910dSFlorian Westphal static int parse_int(const char *size) 1364048d19d4SFlorian Westphal { 1365048d19d4SFlorian Westphal unsigned long s; 1366048d19d4SFlorian Westphal 1367048d19d4SFlorian Westphal errno = 0; 1368048d19d4SFlorian Westphal 1369048d19d4SFlorian Westphal s = strtoul(size, NULL, 0); 1370048d19d4SFlorian Westphal 1371048d19d4SFlorian Westphal if (errno) { 1372048d19d4SFlorian Westphal fprintf(stderr, "Invalid sndbuf size %s (%s)\n", 1373048d19d4SFlorian Westphal size, strerror(errno)); 1374048d19d4SFlorian Westphal die_usage(); 1375048d19d4SFlorian Westphal } 1376048d19d4SFlorian Westphal 1377048d19d4SFlorian Westphal if (s > INT_MAX) { 1378048d19d4SFlorian Westphal fprintf(stderr, "Invalid sndbuf size %s (%s)\n", 1379048d19d4SFlorian Westphal size, strerror(ERANGE)); 1380048d19d4SFlorian Westphal die_usage(); 1381048d19d4SFlorian Westphal } 1382048d19d4SFlorian Westphal 13838a4b910dSFlorian Westphal return (int)s; 1384048d19d4SFlorian Westphal } 1385048d19d4SFlorian Westphal 1386048d19d4SFlorian Westphal static void parse_opts(int argc, char **argv) 1387048d19d4SFlorian Westphal { 1388048d19d4SFlorian Westphal int c; 1389048d19d4SFlorian Westphal 13906bf41020SPaolo Abeni while ((c = getopt(argc, argv, "6c:f:hi:I:jlm:M:o:p:P:r:R:s:S:t:T:w:")) != -1) { 1391048d19d4SFlorian Westphal switch (c) { 13926bf41020SPaolo Abeni case 'f': 13936bf41020SPaolo Abeni cfg_truncate = atoi(optarg); 13946bf41020SPaolo Abeni 13956bf41020SPaolo Abeni /* when receiving a fastclose, ignore PIPE signals and 13966bf41020SPaolo Abeni * all the I/O errors later in the code 13976bf41020SPaolo Abeni */ 13986bf41020SPaolo Abeni if (cfg_truncate < 0) { 13996bf41020SPaolo Abeni cfg_rcv_trunc = true; 14006bf41020SPaolo Abeni signal(SIGPIPE, handle_signal); 14016bf41020SPaolo Abeni } 14026bf41020SPaolo Abeni break; 1403b08fbf24SPaolo Abeni case 'j': 1404b08fbf24SPaolo Abeni cfg_join = true; 1405b08fbf24SPaolo Abeni cfg_mode = CFG_MODE_POLL; 1406b08fbf24SPaolo Abeni break; 140713153324SGeliang Tang case 'r': 140813153324SGeliang Tang cfg_remove = true; 140913153324SGeliang Tang cfg_mode = CFG_MODE_POLL; 141013153324SGeliang Tang cfg_wait = 400000; 14112e580a63SGeliang Tang cfg_do_w = atoi(optarg); 14122e580a63SGeliang Tang if (cfg_do_w <= 0) 14132e580a63SGeliang Tang cfg_do_w = 50; 141413153324SGeliang Tang break; 141505be5e27SPaolo Abeni case 'i': 141605be5e27SPaolo Abeni cfg_input = optarg; 141705be5e27SPaolo Abeni break; 141805be5e27SPaolo Abeni case 'I': 141905be5e27SPaolo Abeni cfg_repeat = atoi(optarg); 142005be5e27SPaolo Abeni break; 1421048d19d4SFlorian Westphal case 'l': 1422048d19d4SFlorian Westphal listen_mode = true; 1423048d19d4SFlorian Westphal break; 1424048d19d4SFlorian Westphal case 'p': 1425048d19d4SFlorian Westphal cfg_port = optarg; 1426048d19d4SFlorian Westphal break; 1427048d19d4SFlorian Westphal case 's': 1428048d19d4SFlorian Westphal cfg_sock_proto = parse_proto(optarg); 1429048d19d4SFlorian Westphal break; 1430048d19d4SFlorian Westphal case 'h': 1431048d19d4SFlorian Westphal die_usage(); 1432048d19d4SFlorian Westphal break; 1433048d19d4SFlorian Westphal case '6': 1434048d19d4SFlorian Westphal pf = AF_INET6; 1435048d19d4SFlorian Westphal break; 1436048d19d4SFlorian Westphal case 't': 1437048d19d4SFlorian Westphal poll_timeout = atoi(optarg) * 1000; 1438048d19d4SFlorian Westphal if (poll_timeout <= 0) 1439048d19d4SFlorian Westphal poll_timeout = -1; 1440048d19d4SFlorian Westphal break; 1441b6ab64b0SPaolo Abeni case 'T': 1442b6ab64b0SPaolo Abeni cfg_time = atoi(optarg); 1443b6ab64b0SPaolo Abeni break; 1444048d19d4SFlorian Westphal case 'm': 1445048d19d4SFlorian Westphal cfg_mode = parse_mode(optarg); 1446048d19d4SFlorian Westphal break; 14478a4b910dSFlorian Westphal case 'S': 14488a4b910dSFlorian Westphal cfg_sndbuf = parse_int(optarg); 14498a4b910dSFlorian Westphal break; 14508a4b910dSFlorian Westphal case 'R': 14518a4b910dSFlorian Westphal cfg_rcvbuf = parse_int(optarg); 1452048d19d4SFlorian Westphal break; 1453df62f2ecSPaolo Abeni case 'w': 1454df62f2ecSPaolo Abeni cfg_wait = atoi(optarg)*1000000; 1455df62f2ecSPaolo Abeni break; 1456dc65fe82SFlorian Westphal case 'M': 1457dc65fe82SFlorian Westphal cfg_mark = strtol(optarg, NULL, 0); 1458dc65fe82SFlorian Westphal break; 1459df8aee6dSYonglong Li case 'P': 1460df8aee6dSYonglong Li cfg_peek = parse_peek(optarg); 1461df8aee6dSYonglong Li break; 14625e6af0a7SFlorian Westphal case 'c': 14635e6af0a7SFlorian Westphal parse_cmsg_types(optarg); 14645e6af0a7SFlorian Westphal break; 14655fb62e9cSFlorian Westphal case 'o': 14665fb62e9cSFlorian Westphal parse_setsock_options(optarg); 14675fb62e9cSFlorian Westphal break; 1468048d19d4SFlorian Westphal } 1469048d19d4SFlorian Westphal } 1470048d19d4SFlorian Westphal 1471048d19d4SFlorian Westphal if (optind + 1 != argc) 1472048d19d4SFlorian Westphal die_usage(); 1473048d19d4SFlorian Westphal cfg_host = argv[optind]; 1474048d19d4SFlorian Westphal 1475048d19d4SFlorian Westphal if (strchr(cfg_host, ':')) 1476048d19d4SFlorian Westphal pf = AF_INET6; 1477048d19d4SFlorian Westphal } 1478048d19d4SFlorian Westphal 1479048d19d4SFlorian Westphal int main(int argc, char *argv[]) 1480048d19d4SFlorian Westphal { 1481048d19d4SFlorian Westphal init_rng(); 1482048d19d4SFlorian Westphal 1483df62f2ecSPaolo Abeni signal(SIGUSR1, handle_signal); 1484048d19d4SFlorian Westphal parse_opts(argc, argv); 1485048d19d4SFlorian Westphal 1486048d19d4SFlorian Westphal if (listen_mode) { 1487048d19d4SFlorian Westphal int fd = sock_listen_mptcp(cfg_host, cfg_port); 1488048d19d4SFlorian Westphal 1489048d19d4SFlorian Westphal if (fd < 0) 1490048d19d4SFlorian Westphal return 1; 1491048d19d4SFlorian Westphal 14928a4b910dSFlorian Westphal if (cfg_rcvbuf) 14938a4b910dSFlorian Westphal set_rcvbuf(fd, cfg_rcvbuf); 1494048d19d4SFlorian Westphal if (cfg_sndbuf) 1495048d19d4SFlorian Westphal set_sndbuf(fd, cfg_sndbuf); 1496dc65fe82SFlorian Westphal if (cfg_mark) 1497dc65fe82SFlorian Westphal set_mark(fd, cfg_mark); 14985e6af0a7SFlorian Westphal if (cfg_cmsg_types.cmsg_enabled) 14995e6af0a7SFlorian Westphal apply_cmsg_types(fd, &cfg_cmsg_types); 1500048d19d4SFlorian Westphal 1501048d19d4SFlorian Westphal return main_loop_s(fd); 1502048d19d4SFlorian Westphal } 1503048d19d4SFlorian Westphal 1504048d19d4SFlorian Westphal return main_loop(); 1505048d19d4SFlorian Westphal } 1506