├── .github └── workflows │ └── c-cpp.yml ├── Changes ├── GNUmakefile ├── LICENSE ├── Makefile ├── README ├── tcpbench.1 └── tcpbench.c /.github/workflows/c-cpp.yml: -------------------------------------------------------------------------------- 1 | name: C/C++ CI 2 | 3 | on: 4 | push: 5 | branches: [ "master" ] 6 | pull_request: 7 | branches: [ "master" ] 8 | 9 | jobs: 10 | build: 11 | 12 | runs-on: ubuntu-latest 13 | 14 | steps: 15 | - name: Install packages 16 | run: sudo apt-get -y install libbsd-dev libevent-dev libtls-dev pkgconf 17 | - name: Checkout sources 18 | uses: actions/checkout@v4 19 | - name: Make build 20 | run: make 21 | - name: Make test 22 | run: make test 23 | - name: Make install 24 | run: sudo make install 25 | -------------------------------------------------------------------------------- /Changes: -------------------------------------------------------------------------------- 1 | Revision history for port from OpenBSD tcpbench to GNU/Linux. 2 | 3 | 3.01 4 | 5 | 3.00 2024-12-30 6 | * Do not include sys/socketvar.h on Linux. 7 | * Update to OpenBSD tcpbench.c revision 1.72. 8 | * OpenBSD tcpbench provides testing TLS connections. 9 | * For that libtls from the LibreSSL project is needed. 10 | * Add TLS test for OpenBSD and GNU/Linux. 11 | 12 | 2.02 2024-03-22 13 | * Use pkgconf to build on GNU/Linux. 14 | * Update to OpenBSD tcpbench.c revision 1.70. 15 | 16 | 2.01 2023-06-12 17 | * Provide gpg signature for release tar ball. 18 | * Disable IPv6 mapped address to avoid "Address already in use" 19 | error on Linux. 20 | 21 | 2.00 2023-05-22 22 | * Remove many #ifdef OpenBSD as kvm PCB table was replaced with 23 | getsockopt(TCP_INFO) which also exists on Linux. 24 | * Update to OpenBSD tcpbench.c revision 1.69. 25 | * Respect environment CFLAGS, CPPFLAGS and LDFLAGS in GNU makefile. 26 | 27 | 1.01 2022-08-08 28 | * Link with libevent2 instead of libev on GNU/Linux. 29 | * Fix build on Ubuntu. 30 | 31 | 1.00 2022-08-07 32 | * Initial public release based on OpenBSD tcpbench.c 1.59. 33 | -------------------------------------------------------------------------------- /GNUmakefile: -------------------------------------------------------------------------------- 1 | CFLAGS+= -D_DEFAULT_SOURCE -D_GNU_SOURCE -Wall \ 2 | $(shell pkgconf --cflags libbsd-overlay) \ 3 | $(shell pkgconf --cflags libtls) 4 | LDFLAGS+= -levent -lm -ltls -lcrypto \ 5 | $(shell pkgconf --libs libbsd-overlay) \ 6 | $(shell pkgconf --libs libtls) 7 | BINDIR?= /usr/local/bin 8 | MANDIR?= /usr/local/man/man 9 | 10 | .PHONY: all clean install 11 | all: tcpbench 12 | 13 | tcpbench: tcpbench.c 14 | $(CC) $(CPPFLAGS) $(CFLAGS) $< $(LDFLAGS) -o $@ 15 | 16 | clean: 17 | rm -f tcpbench tcpbench.o out *.crt *.key *.req *.srl 18 | 19 | install: 20 | install -c -m 555 -s tcpbench -D -t ${DESTDIR}${BINDIR} 21 | install -c -m 444 tcpbench.1 -D -t ${DESTDIR}${MANDIR}1 22 | 23 | .PHONY: test test-localhost test-localhost6 test-tls test-ciphers 24 | test: test-localhost test-localhost6 test-tls test-ciphers 25 | 26 | test-localhost: 27 | @echo -e '\n==== $@ ====' 28 | ./tcpbench -s -4 >out & \ 29 | trap "kill -TERM $$!" EXIT; \ 30 | sleep 1; \ 31 | ./tcpbench -t2 127.0.0.1 || exit 1; \ 32 | true 33 | grep '^Conn:' out 34 | 35 | test-localhost6: 36 | @echo -e '\n==== $@ ====' 37 | ./tcpbench -s -6 >out & \ 38 | trap "kill -TERM $$!" EXIT; \ 39 | sleep 1; \ 40 | ./tcpbench -t2 ::1 || exit 1; \ 41 | true 42 | grep '^Conn:' out 43 | 44 | test-tls: server.crt 45 | @echo '\n==== $@ ====' 46 | ./tcpbench -c -C server.crt -K server.key -s -4 >out & \ 47 | trap "kill -TERM $$!" EXIT; \ 48 | sleep 1; \ 49 | ./tcpbench -c -t2 127.0.0.1 || exit 1; \ 50 | true 51 | grep '^Conn:' out 52 | 53 | test-ciphers: server.crt 54 | @echo '\n==== $@ ====' 55 | ./tcpbench -T ciphers=AES128-GCM-SHA256 \ 56 | -c -C server.crt -K server.key -s -4 >out & \ 57 | trap "kill -TERM $$!" EXIT; \ 58 | sleep 1; \ 59 | ./tcpbench -T ciphers=AES128-GCM-SHA256 \ 60 | -c -t2 127.0.0.1 || exit 1; \ 61 | true 62 | grep '^Conn:' out 63 | 64 | ca.crt: 65 | openssl req -batch -new \ 66 | -subj /L=OpenBSD/O=tcpbench-regress/OU=ca/CN=root/ \ 67 | -nodes -newkey rsa -keyout ca.key -x509 \ 68 | -out ca.crt 69 | 70 | server.req: 71 | openssl req -batch -new \ 72 | -subj /L=OpenBSD/O=tcpbench-regress/OU=server/CN=localhost/ \ 73 | -nodes -newkey rsa -keyout server.key \ 74 | -out server.req 75 | 76 | server.crt: ca.crt server.req 77 | openssl x509 -CAcreateserial -CAkey ca.key -CA ca.crt \ 78 | -req -in server.req \ 79 | -out server.crt 80 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2023-2024 Tobias Heider 2 | Copyright (c) 2019-2024 Alexander Bluhm 3 | Copyright (c) 2008 Damien Miller 4 | Copyright (c) 2011 Christiano F. Haesbaert 5 | 6 | Permission to use, copy, modify, and distribute this software for any 7 | purpose with or without fee is hereby granted, provided that the above 8 | copyright notice and this permission notice appear in all copies. 9 | 10 | THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 | WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 | MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 | ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | PROG= tcpbench 2 | WARNINGS= yes 3 | LDADD= -lm -levent -ltls -lcrypto 4 | DPADD= ${LIBM} ${LIBEVENT} ${LIBTLS} ${LIBCRYPTO} 5 | BINDIR?= /usr/local/bin 6 | MANDIR?= /usr/local/man/man 7 | 8 | VERSION= 3.01 9 | CLEANFILES= tcpbench-${VERSION}.tar.gz* 10 | 11 | .PHONY: dist tcpbench-${VERSION}.tar.gz 12 | dist: tcpbench-${VERSION}.tar.gz 13 | gpg --armor --detach-sign tcpbench-${VERSION}.tar.gz 14 | @echo ${.OBJDIR}/tcpbench-${VERSION}.tar.gz 15 | 16 | tcpbench-${VERSION}.tar.gz: 17 | rm -rf tcpbench-${VERSION} 18 | mkdir tcpbench-${VERSION} 19 | .for f in README LICENSE Changes Makefile GNUmakefile tcpbench.c tcpbench.1 20 | cp ${.CURDIR}/$f tcpbench-${VERSION}/ 21 | .endfor 22 | tar -czvf $@ tcpbench-${VERSION} 23 | rm -rf tcpbench-${VERSION} 24 | 25 | CLEANFILES+= out *.crt *.key *.req *.srl 26 | 27 | .PHONY: test test-localhost test-localhost6 test-tls test-ciphers 28 | test: test-localhost test-localhost6 test-tls test-ciphers 29 | 30 | test-localhost: 31 | @echo '\n==== $@ ====' 32 | ./tcpbench -s -4 >out & \ 33 | trap "kill -TERM $$!" EXIT; \ 34 | sleep 1; \ 35 | ./tcpbench -t2 127.0.0.1 || exit 1; \ 36 | true 37 | grep '^Conn:' out 38 | 39 | test-localhost6: 40 | @echo '\n==== $@ ====' 41 | ./tcpbench -s -6 >out & \ 42 | trap "kill -TERM $$!" EXIT; \ 43 | sleep 1; \ 44 | ./tcpbench -t2 ::1 || exit 1; \ 45 | true 46 | grep '^Conn:' out 47 | 48 | test-tls: server.crt 49 | @echo '\n==== $@ ====' 50 | ./tcpbench -c -C server.crt -K server.key -s -4 >out & \ 51 | trap "kill -TERM $$!" EXIT; \ 52 | sleep 1; \ 53 | ./tcpbench -c -t2 127.0.0.1 || exit 1; \ 54 | true 55 | grep '^Conn:' out 56 | 57 | test-ciphers: server.crt 58 | @echo '\n==== $@ ====' 59 | ./tcpbench -T ciphers=AES128-GCM-SHA256 \ 60 | -c -C server.crt -K server.key -s -4 >out & \ 61 | trap "kill -TERM $$!" EXIT; \ 62 | sleep 1; \ 63 | ./tcpbench -T ciphers=AES128-GCM-SHA256 \ 64 | -c -t2 127.0.0.1 || exit 1; \ 65 | true 66 | grep '^Conn:' out 67 | 68 | ca.crt: 69 | openssl req -batch -new \ 70 | -subj /L=OpenBSD/O=tcpbench-regress/OU=ca/CN=root/ \ 71 | -nodes -newkey rsa -keyout ca.key -x509 \ 72 | -out ca.crt 73 | 74 | server.req: 75 | openssl req -batch -new \ 76 | -subj /L=OpenBSD/O=tcpbench-regress/OU=server/CN=localhost/ \ 77 | -nodes -newkey rsa -keyout server.key \ 78 | -out server.req 79 | 80 | server.crt: ca.crt server.req 81 | openssl x509 -CAcreateserial -CAkey ca.key -CA ca.crt \ 82 | -req -in server.req \ 83 | -out server.crt 84 | 85 | .include 86 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | Port OpenBSD tcpbench to GNU/Linux and possibly other systems. Keep 2 | the differences to upstream small. Use libbsd to provide features 3 | missing in GNU glibc. Use #ifdef __OpenBSD__ to disable things 4 | that cannot be easily done with Linux kernel. 5 | -------------------------------------------------------------------------------- /tcpbench.1: -------------------------------------------------------------------------------- 1 | .\" $OpenBSD: tcpbench.1,v 1.32 2024/11/06 06:40:16 jmc Exp $ 2 | .\" 3 | .\" Copyright (c) 2008 Damien Miller 4 | .\" 5 | .\" Permission to use, copy, modify, and distribute this software for any 6 | .\" purpose with or without fee is hereby granted, provided that the above 7 | .\" copyright notice and this permission notice appear in all copies. 8 | .\" 9 | .\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 | .\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 | .\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 | .\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 | .\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 | .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 | .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 | .\" 17 | .Dd $Mdocdate: November 6 2024 $ 18 | .Dt TCPBENCH 1 19 | .Os 20 | .Sh NAME 21 | .Nm tcpbench 22 | .Nd TCP/UDP benchmarking and measurement tool 23 | .Sh SYNOPSIS 24 | .Nm 25 | .Fl l 26 | .Nm 27 | .Op Fl 46cDRUuv 28 | .Op Fl B Ar buf 29 | .Op Fl b Ar sourceaddr 30 | .Op Fl k Ar kvars 31 | .Op Fl n Ar connections 32 | .Op Fl p Ar port 33 | .Op Fl r Ar interval 34 | .Op Fl S Ar space 35 | .Op Fl T Ar keyword 36 | .Op Fl t Ar secs 37 | .Op Fl V Ar rtable 38 | .Ar hostname 39 | .Nm 40 | .Bk -words 41 | .Fl s 42 | .Op Fl 46cDUuv 43 | .Op Fl B Ar buf 44 | .Op Fl C Ar certfile Fl K Ar keyfile 45 | .Op Fl k Ar kvars 46 | .Op Fl p Ar port 47 | .Op Fl r Ar interval 48 | .Op Fl S Ar space 49 | .Op Fl T Ar keyword 50 | .Op Fl V Ar rtable 51 | .Op Ar hostname 52 | .Ek 53 | .Sh DESCRIPTION 54 | .Nm 55 | is a small tool that performs throughput benchmarking and concurrent 56 | sampling of kernel network variables. 57 | .Pp 58 | .Nm 59 | is run as a client/server pair. 60 | The server must be invoked with the 61 | .Fl s 62 | flag, which will cause it to listen for incoming connections. 63 | The client must be invoked with the 64 | .Ar hostname 65 | of a listening server to connect to. 66 | .Pp 67 | Once connected, the client will send TCP or UDP traffic as fast as possible to 68 | the server. 69 | Both the client and server will periodically compute and display throughput 70 | statistics. 71 | The server starts computing these for UDP on receipt of the first datagram, 72 | and stops for TCP when it has no connections. 73 | This display also includes any kernel variables the user has selected to 74 | sample (using the 75 | .Fl k 76 | option, which is only available in TCP mode). 77 | A list of available kernel variables may be obtained using the 78 | .Fl l 79 | option. 80 | .Pp 81 | A summary over the periodic throughput statistics is displayed on exit. 82 | Its accuracy may be increased by decreasing the 83 | .Ar interval . 84 | The summary bytes and duration cover the interval from transfer start 85 | to process exit. 86 | The summary information can also be displayed while 87 | .Nm 88 | is running by sending it a 89 | .Dv SIGINFO 90 | signal (see the 91 | .Cm status 92 | argument of 93 | .Xr stty 1 94 | for more information). 95 | .Pp 96 | The options are as follows: 97 | .Bl -tag -width Ds 98 | .It Fl 4 99 | Force 100 | .Nm 101 | to use IPv4 addresses only. 102 | .It Fl 6 103 | Force 104 | .Nm 105 | to use IPv6 addresses only. 106 | .It Fl B Ar buf 107 | Specify the size of the internal read/write buffer used by 108 | .Nm . 109 | The default is 262144 bytes for TCP client/server and UDP server. 110 | In UDP client mode this may be used to specify the packet size on the test 111 | stream. 112 | .It Fl b Ar sourceaddr 113 | Specify the IP address to send the packets from, 114 | which is useful on machines with multiple interfaces. 115 | .It Fl C Ar certfile 116 | Load the public key part of the TLS peer certificate from 117 | .Ar certfile , 118 | in PEM format. 119 | Requires 120 | .Fl s 121 | and 122 | .Fl c . 123 | .It Fl c 124 | Use TLS to connect or listen. 125 | .It Fl D 126 | Enable debugging on the socket. 127 | .It Fl K Ar keyfile 128 | Load the TLS private key from 129 | .Ar keyfile , 130 | in PEM format. 131 | Requires 132 | .Fl s 133 | and 134 | .Fl c . 135 | .It Fl k Ar kvars 136 | Specify one or more kernel variables to monitor; multiple variables must be 137 | separated with commas. 138 | This option is only valid in TCP mode. 139 | The default is not to monitor any variables. 140 | .It Fl l 141 | List the name of kernel variables available for monitoring and exit. 142 | .It Fl n Ar connections 143 | Use the given number of TCP connections (default: 1). 144 | UDP is connectionless so this option isn't valid. 145 | .It Fl p Ar port 146 | Specify the port used for the test stream (default: 12345). 147 | .It Fl R 148 | In client mode the write buffer size is randomized up to the size specified via 149 | .Fl B . 150 | .It Fl r Ar interval 151 | Specify the statistics interval reporting rate in milliseconds (default: 1000). 152 | If set to 0, nothing is printed. 153 | .It Fl S Ar space 154 | Set the size of the socket buffer used for the test stream. 155 | On the client this option will resize the send buffer; 156 | on the server it will resize the receive buffer. 157 | .It Fl s 158 | Place 159 | .Nm 160 | in server mode, where it will listen on all interfaces for incoming 161 | connections. 162 | It defaults to using TCP if 163 | .Fl u 164 | is not specified. 165 | .It Fl T Ar keyword 166 | Change the IPv4 TOS or IPv6 TCLASS value. 167 | .Ar keyword 168 | may be one of 169 | .Ar critical , 170 | .Ar inetcontrol , 171 | .Ar lowdelay , 172 | .Ar netcontrol , 173 | .Ar throughput , 174 | .Ar reliability , 175 | or one of the DiffServ Code Points: 176 | .Ar ef , 177 | .Ar af11 ... af43 , 178 | .Ar cs0 ... cs7 ; 179 | or a number in either hex or decimal. 180 | .Pp 181 | For TLS options, 182 | .Ar keyword 183 | specifies a value in the form of a 184 | .Ar key Ns = Ns Ar value 185 | pair: 186 | .Cm ciphers , 187 | which allows the supported TLS ciphers to be specified (see 188 | .Xr tls_config_set_ciphers 3 189 | for further details) or 190 | .Cm protocols , 191 | which allows the supported TLS protocols to be specified (see 192 | .Xr tls_config_parse_protocols 3 193 | for further details). 194 | Specifying TLS options requires 195 | .Fl c . 196 | .It Fl t Ar secs 197 | Stop after 198 | .Ar secs 199 | seconds. 200 | .It Fl U 201 | Use AF_UNIX sockets instead of IPv4 or IPv6 sockets. 202 | In client and server mode 203 | .Ar hostname 204 | is used as the path to the AF_UNIX socket. 205 | .It Fl u 206 | Use UDP instead of TCP; this must be specified on both the client 207 | and the server. 208 | Transmitted packets per second (TX PPS) will be accounted on the client 209 | side, while received packets per second (RX PPS) will be accounted on the 210 | server side. 211 | UDP has no Protocol Control Block (PCB) so the 212 | .Fl k 213 | flags don't apply. 214 | .It Fl V Ar rtable 215 | Set the routing table to be used. 216 | .It Fl v 217 | Display verbose output. 218 | If specified more than once, increase the detail of information displayed. 219 | .El 220 | .Sh SEE ALSO 221 | .Xr netstat 1 , 222 | .Xr pstat 8 223 | .Sh HISTORY 224 | The 225 | .Nm 226 | program first appeared in 227 | .Ox 4.4 . 228 | .Sh AUTHORS 229 | .An -nosplit 230 | The 231 | .Nm 232 | program was written by 233 | .An Damien Miller Aq Mt djm@openbsd.org . 234 | .Pp 235 | UDP mode and libevent port by 236 | .An Christiano F. Haesbaert Aq Mt haesbaert@haesbaert.org . 237 | -------------------------------------------------------------------------------- /tcpbench.c: -------------------------------------------------------------------------------- 1 | /* $OpenBSD: tcpbench.c,v 1.72 2024/12/28 11:51:21 bluhm Exp $ */ 2 | 3 | /* 4 | * Copyright (c) 2008 Damien Miller 5 | * Copyright (c) 2011 Christiano F. Haesbaert 6 | * 7 | * Permission to use, copy, modify, and distribute this software for any 8 | * purpose with or without fee is hereby granted, provided that the above 9 | * copyright notice and this permission notice appear in all copies. 10 | * 11 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 12 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 13 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 14 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 15 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 16 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 17 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 18 | */ 19 | 20 | #include 21 | #include 22 | #include 23 | #ifdef __OpenBSD__ 24 | #include 25 | #endif 26 | #include 27 | #include 28 | #include 29 | 30 | #include 31 | 32 | #include 33 | #include 34 | #include 35 | #ifdef __OpenBSD__ 36 | #include 37 | #include 38 | #include 39 | #include 40 | #endif 41 | 42 | #include 43 | 44 | #include 45 | #include 46 | #include 47 | #include 48 | #include 49 | #include 50 | #ifdef __OpenBSD__ 51 | #include 52 | #else 53 | #include 54 | #include 55 | #include 56 | #endif 57 | #include 58 | #include 59 | #include 60 | #include 61 | #include 62 | #include 63 | #include 64 | #include 65 | 66 | #include 67 | #include 68 | #include 69 | #include 70 | 71 | #define DEFAULT_PORT "12345" 72 | #define DEFAULT_STATS_INTERVAL 1000 /* ms */ 73 | #define DEFAULT_BUF (256 * 1024) 74 | #define DEFAULT_UDP_PKT (1500 - 28) /* TODO don't hardcode this */ 75 | #define TCP_MODE !ptb->uflag 76 | #define UDP_MODE ptb->uflag 77 | #define MAX_FD 1024 78 | 79 | /* Our tcpbench globals */ 80 | struct { 81 | int Dflag; /* Socket debug */ 82 | int Sflag; /* Socket buffer size */ 83 | u_int rflag; /* Report rate (ms) */ 84 | int sflag; /* True if server */ 85 | int Tflag; /* ToS if != -1 */ 86 | int vflag; /* Verbose */ 87 | int uflag; /* UDP mode */ 88 | int Uflag; /* UNIX (AF_LOCAL) mode */ 89 | int Rflag; /* randomize client write size */ 90 | char **kvars; /* Kvm enabled vars */ 91 | char *dummybuf; /* IO buffer */ 92 | size_t dummybuf_len; /* IO buffer len */ 93 | struct tls_config *tls_cfg; 94 | } tcpbench, *ptb; 95 | 96 | struct tcpservsock { 97 | struct event ev; 98 | struct event evt; 99 | int fd; 100 | }; 101 | 102 | /* stats for a single tcp connection, udp uses only one */ 103 | struct statctx { 104 | TAILQ_ENTRY(statctx) entry; 105 | struct timeval t_start, t_last; 106 | unsigned long long bytes; 107 | int fd; 108 | char *buf; 109 | size_t buflen; 110 | struct event ev; 111 | /* TCP only */ 112 | struct tcpservsock *tcp_ts; 113 | /* UDP only */ 114 | u_long udp_slice_pkts; 115 | /* TLS context */ 116 | struct tls *tls; 117 | }; 118 | 119 | char *tls_ciphers; 120 | char *tls_protocols; 121 | struct statctx *udp_sc; /* singleton */ 122 | 123 | static void signal_handler(int, short, void *); 124 | static void saddr_ntop(const struct sockaddr *, socklen_t, char *, size_t); 125 | static void set_slice_timer(int); 126 | static void print_tcp_header(void); 127 | static void list_kvars(void); 128 | static void check_kvar(const char *); 129 | static char ** check_prepare_kvars(char *); 130 | static void stats_prepare(struct statctx *); 131 | static void summary_display(void); 132 | static void tcp_stats_display(unsigned long long, long double, float, 133 | struct statctx *, struct tcp_info *); 134 | static void tcp_process_slice(int, short, void *); 135 | static void tcp_server_handle_sc(int, short, void *); 136 | static int timeout_tls(int, struct tls *, int (*)(struct tls *)); 137 | static void tcp_server_accept(int, short, void *); 138 | static void server_init(struct addrinfo *); 139 | static void client_handle_sc(int, short, void *); 140 | static void client_init(struct addrinfo *, int, struct addrinfo *); 141 | static int clock_gettime_tv(clockid_t, struct timeval *); 142 | static void udp_server_handle_sc(int, short, void *); 143 | static void udp_process_slice(int, short, void *); 144 | #ifdef __OpenBSD__ 145 | static int map_tos(char *, int *); 146 | #endif 147 | static void quit(int, short, void *); 148 | static void wrapup(int); 149 | static int process_tls_opt(char *); 150 | 151 | /* 152 | * We account the mainstats here, that is the stats 153 | * for all connections, all variables starting with slice 154 | * are used to account information for the timeslice 155 | * between each output. Peak variables record the highest 156 | * between all slices so far. 157 | */ 158 | static struct { 159 | struct timeval t_first; /* first connect / packet */ 160 | unsigned long long total_bytes; /* bytes since t_first */ 161 | unsigned long long n_slices; /* slices since start */ 162 | unsigned long long slice_bytes; /* bytes since slice reset */ 163 | long double peak_mbps; /* peak mbps so far */ 164 | long double floor_mbps; /* floor mbps so far */ 165 | long double mean_mbps; /* mean mbps so far */ 166 | long double nvariance_mbps; /* for online std dev */ 167 | int nconns; /* connected clients */ 168 | struct event timer; /* process timer */ 169 | const char *host; /* remote server for display */ 170 | } mainstats; 171 | 172 | /* When adding variables, also add to tcp_stats_display() */ 173 | static const char *allowed_kvars[] = { 174 | "last_ack_recv", 175 | "last_ack_sent", 176 | "last_data_recv", 177 | "last_data_sent", 178 | #ifdef __OpenBSD__ 179 | "max_sndwnd", 180 | #endif 181 | "options", 182 | #ifdef __OpenBSD__ 183 | "rcv_adv", 184 | #endif 185 | "rcv_mss", 186 | #ifdef __OpenBSD__ 187 | "rcv_nxt", 188 | "rcv_ooopack", 189 | #endif 190 | "rcv_space", 191 | #ifdef __OpenBSD__ 192 | "rcv_up", 193 | #endif 194 | "rcv_wscale", 195 | #ifdef __OpenBSD__ 196 | "rfbuf_cnt", 197 | "rfbuf_ts", 198 | #endif 199 | "rtt", 200 | #ifdef __OpenBSD__ 201 | "rttmin", 202 | #endif 203 | "rttvar", 204 | "snd_cwnd", 205 | #ifdef __OpenBSD__ 206 | "snd_max", 207 | #endif 208 | "snd_mss", 209 | #ifdef __OpenBSD__ 210 | "snd_nxt", 211 | "snd_rexmitpack", 212 | #endif 213 | "snd_ssthresh", 214 | #ifdef __OpenBSD__ 215 | "snd_una", 216 | "snd_wl1", 217 | "snd_wl2", 218 | "snd_wnd", 219 | "snd_wscale", 220 | "snd_zerowin", 221 | "so_rcv_sb_cc", 222 | "so_rcv_sb_hiwat", 223 | "so_rcv_sb_lowat", 224 | "so_rcv_sb_wat", 225 | "so_snd_sb_cc", 226 | "so_snd_sb_hiwat", 227 | "so_snd_sb_lowat", 228 | "so_snd_sb_wat", 229 | "ts_recent", 230 | "ts_recent_age", 231 | #endif 232 | NULL 233 | }; 234 | 235 | TAILQ_HEAD(, statctx) sc_queue; 236 | 237 | static void 238 | usage(void) 239 | { 240 | fprintf(stderr, 241 | "usage: tcpbench -l\n" 242 | " tcpbench [-46cDRUuv] [-B buf] [-b sourceaddr] [-k kvars] [-n connections]\n" 243 | " [-p port] [-r interval] [-S space] [-T keyword]\n" 244 | " [-t secs] [-V rtable] hostname\n" 245 | " tcpbench -s [-46cDUuv] [-B buf] [-C certfile -K keyfile] [-k kvars] [-p port] [-r interval]\n" 246 | " [-S space] [-T keyword] [-V rtable] [hostname]\n"); 247 | exit(1); 248 | } 249 | 250 | static void 251 | signal_handler(int sig, short event, void *bula) 252 | { 253 | /* 254 | * signal handler rules don't apply, libevent decouples for us 255 | */ 256 | switch (sig) { 257 | #ifdef __OpenBSD__ 258 | case SIGINFO: 259 | printf("\n"); 260 | wrapup(-1); 261 | break; 262 | #endif 263 | case SIGINT: 264 | printf("\n"); 265 | wrapup(0); 266 | break; /* NOTREACHED */ 267 | case SIGTERM: 268 | case SIGHUP: 269 | warnx("Terminated by signal %d", sig); 270 | wrapup(0); 271 | break; /* NOTREACHED */ 272 | default: 273 | errx(1, "unexpected signal %d", sig); 274 | break; /* NOTREACHED */ 275 | } 276 | } 277 | 278 | static void 279 | saddr_ntop(const struct sockaddr *addr, socklen_t alen, char *buf, size_t len) 280 | { 281 | char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV]; 282 | int herr; 283 | 284 | if (addr->sa_family == AF_UNIX) { 285 | struct sockaddr_un *sun = (struct sockaddr_un *)addr; 286 | snprintf(buf, len, "%s", sun->sun_path); 287 | return; 288 | } 289 | if ((herr = getnameinfo(addr, alen, hbuf, sizeof(hbuf), 290 | pbuf, sizeof(pbuf), NI_NUMERICHOST|NI_NUMERICSERV)) != 0) { 291 | if (herr == EAI_SYSTEM) 292 | err(1, "getnameinfo"); 293 | else 294 | errx(1, "getnameinfo: %s", gai_strerror(herr)); 295 | } 296 | snprintf(buf, len, "[%s]:%s", hbuf, pbuf); 297 | } 298 | 299 | static void 300 | set_slice_timer(int on) 301 | { 302 | struct timeval tv; 303 | 304 | if (ptb->rflag == 0) 305 | return; 306 | 307 | if (on) { 308 | if (evtimer_pending(&mainstats.timer, NULL)) 309 | return; 310 | /* XXX Is there a better way to do this ? */ 311 | tv.tv_sec = ptb->rflag / 1000; 312 | tv.tv_usec = (ptb->rflag % 1000) * 1000; 313 | 314 | evtimer_add(&mainstats.timer, &tv); 315 | } else if (evtimer_pending(&mainstats.timer, NULL)) 316 | evtimer_del(&mainstats.timer); 317 | } 318 | 319 | static int 320 | clock_gettime_tv(clockid_t clock_id, struct timeval *tv) 321 | { 322 | struct timespec ts; 323 | 324 | if (clock_gettime(clock_id, &ts) == -1) 325 | return (-1); 326 | 327 | TIMESPEC_TO_TIMEVAL(tv, &ts); 328 | 329 | return (0); 330 | } 331 | 332 | static void 333 | print_tcp_header(void) 334 | { 335 | char **kv; 336 | 337 | if (ptb->rflag == 0) 338 | return; 339 | 340 | printf("%12s %14s %12s %8s ", "elapsed_ms", "bytes", "mbps", 341 | "bwidth"); 342 | for (kv = ptb->kvars; ptb->kvars != NULL && *kv != NULL; kv++) 343 | printf("%s%s", kv != ptb->kvars ? "," : "", *kv); 344 | printf("\n"); 345 | } 346 | 347 | static void 348 | check_kvar(const char *var) 349 | { 350 | u_int i; 351 | 352 | for (i = 0; allowed_kvars[i] != NULL; i++) 353 | if (strcmp(allowed_kvars[i], var) == 0) 354 | return; 355 | errx(1, "Unrecognised kvar: %s", var); 356 | } 357 | 358 | static void 359 | list_kvars(void) 360 | { 361 | u_int i; 362 | 363 | printf("Supported kernel variables:\n"); 364 | for (i = 0; allowed_kvars[i] != NULL; i++) 365 | printf("\t%s\n", allowed_kvars[i]); 366 | } 367 | 368 | static char ** 369 | check_prepare_kvars(char *list) 370 | { 371 | char *item, **ret = NULL; 372 | u_int n = 0; 373 | 374 | while ((item = strsep(&list, ", \t\n")) != NULL) { 375 | check_kvar(item); 376 | if ((ret = reallocarray(ret, (++n + 1), sizeof(*ret))) == NULL) 377 | err(1, "reallocarray(kvars)"); 378 | if ((ret[n - 1] = strdup(item)) == NULL) 379 | err(1, "strdup"); 380 | ret[n] = NULL; 381 | } 382 | return (ret); 383 | } 384 | 385 | static void 386 | stats_prepare(struct statctx *sc) 387 | { 388 | sc->buf = ptb->dummybuf; 389 | sc->buflen = ptb->dummybuf_len; 390 | 391 | if (clock_gettime_tv(CLOCK_MONOTONIC, &sc->t_start) == -1) 392 | err(1, "clock_gettime_tv"); 393 | sc->t_last = sc->t_start; 394 | if (!timerisset(&mainstats.t_first)) 395 | mainstats.t_first = sc->t_start; 396 | } 397 | 398 | static void 399 | summary_display(void) 400 | { 401 | struct timeval t_cur, t_diff; 402 | long double std_dev; 403 | unsigned long long total_elapsed; 404 | char *direction; 405 | 406 | if (!ptb->sflag) { 407 | direction = "sent"; 408 | printf("--- %s tcpbench statistics ---\n", mainstats.host); 409 | } else { 410 | direction = "received"; 411 | printf("--- tcpbench server statistics ---\n"); 412 | } 413 | 414 | std_dev = sqrtl(mainstats.nvariance_mbps / mainstats.n_slices); 415 | 416 | if (clock_gettime_tv(CLOCK_MONOTONIC, &t_cur) == -1) 417 | err(1, "clock_gettime_tv"); 418 | timersub(&t_cur, &mainstats.t_first, &t_diff); 419 | total_elapsed = t_diff.tv_sec * 1000 + t_diff.tv_usec / 1000; 420 | 421 | printf("%llu bytes %s over %.3Lf seconds\n", 422 | mainstats.total_bytes, direction, total_elapsed/1000.0L); 423 | printf("bandwidth min/avg/max/std-dev = %.3Lf/%.3Lf/%.3Lf/%.3Lf Mbps\n", 424 | mainstats.floor_mbps, mainstats.mean_mbps, mainstats.peak_mbps, 425 | std_dev); 426 | } 427 | 428 | static void 429 | tcp_stats_display(unsigned long long total_elapsed, long double mbps, 430 | float bwperc, struct statctx *sc, struct tcp_info *tcpi) 431 | { 432 | int j; 433 | 434 | printf("%12llu %14llu %12.3Lf %7.2f%% ", total_elapsed, sc->bytes, 435 | mbps, bwperc); 436 | 437 | if (ptb->kvars != NULL) { 438 | for (j = 0; ptb->kvars[j] != NULL; j++) { 439 | #define S(a) #a 440 | #define P(b, v, f) \ 441 | if (strcmp(ptb->kvars[j], S(v)) == 0) { \ 442 | printf("%s"f, j > 0 ? "," : "", b->tcpi_##v); \ 443 | continue; \ 444 | } 445 | P(tcpi, last_ack_recv, "%u") 446 | P(tcpi, last_ack_sent, "%u") 447 | P(tcpi, last_data_recv, "%u") 448 | P(tcpi, last_data_sent, "%u") 449 | #ifdef __OpenBSD__ 450 | P(tcpi, max_sndwnd, "%u") 451 | #endif 452 | P(tcpi, options, "%hhu") 453 | #ifdef __OpenBSD__ 454 | P(tcpi, rcv_adv, "%u") 455 | #endif 456 | P(tcpi, rcv_mss, "%u") 457 | #ifdef __OpenBSD__ 458 | P(tcpi, rcv_nxt, "%u") 459 | P(tcpi, rcv_ooopack, "%u") 460 | #endif 461 | P(tcpi, rcv_space, "%u") 462 | #ifdef __OpenBSD__ 463 | P(tcpi, rcv_up, "%u") 464 | #endif 465 | P(tcpi, rcv_wscale, "%hhu") 466 | #ifdef __OpenBSD__ 467 | P(tcpi, rfbuf_cnt, "%u") 468 | P(tcpi, rfbuf_ts, "%u") 469 | #endif 470 | P(tcpi, rtt, "%u") 471 | #ifdef __OpenBSD__ 472 | P(tcpi, rttmin, "%u") 473 | #endif 474 | P(tcpi, rttvar, "%u") 475 | P(tcpi, snd_cwnd, "%u") 476 | #ifdef __OpenBSD__ 477 | P(tcpi, snd_max, "%u") 478 | #endif 479 | P(tcpi, snd_mss, "%u") 480 | #ifdef __OpenBSD__ 481 | P(tcpi, snd_nxt, "%u") 482 | P(tcpi, snd_rexmitpack, "%u") 483 | #endif 484 | P(tcpi, snd_ssthresh, "%u") 485 | #ifdef __OpenBSD__ 486 | P(tcpi, snd_una, "%u") 487 | P(tcpi, snd_wl1, "%u") 488 | P(tcpi, snd_wl2, "%u") 489 | P(tcpi, snd_wnd, "%u") 490 | P(tcpi, snd_wscale, "%hhu") 491 | P(tcpi, snd_zerowin, "%u") 492 | P(tcpi, so_rcv_sb_cc, "%u") 493 | P(tcpi, so_rcv_sb_hiwat, "%u") 494 | P(tcpi, so_rcv_sb_lowat, "%u") 495 | P(tcpi, so_rcv_sb_wat, "%u") 496 | P(tcpi, so_snd_sb_cc, "%u") 497 | P(tcpi, so_snd_sb_hiwat, "%u") 498 | P(tcpi, so_snd_sb_lowat, "%u") 499 | P(tcpi, so_snd_sb_wat, "%u") 500 | P(tcpi, ts_recent, "%u") 501 | P(tcpi, ts_recent_age, "%u") 502 | #endif 503 | #undef S 504 | #undef P 505 | } 506 | } 507 | printf("\n"); 508 | } 509 | 510 | static void 511 | tcp_process_slice(int fd, short event, void *bula) 512 | { 513 | unsigned long long total_elapsed, since_last; 514 | long double mbps, old_mean_mbps, slice_mbps = 0; 515 | float bwperc; 516 | struct statctx *sc; 517 | struct timeval t_cur, t_diff; 518 | struct tcp_info tcpi; 519 | socklen_t tcpilen; 520 | 521 | if (TAILQ_EMPTY(&sc_queue)) 522 | return; /* don't pollute stats */ 523 | 524 | mainstats.n_slices++; 525 | 526 | TAILQ_FOREACH(sc, &sc_queue, entry) { 527 | if (clock_gettime_tv(CLOCK_MONOTONIC, &t_cur) == -1) 528 | err(1, "clock_gettime_tv"); 529 | if (ptb->kvars != NULL) { /* process kernel stats */ 530 | tcpilen = sizeof(tcpi); 531 | if (getsockopt(sc->fd, IPPROTO_TCP, TCP_INFO, 532 | &tcpi, &tcpilen) == -1) 533 | err(1, "get tcp_info"); 534 | } 535 | 536 | timersub(&t_cur, &sc->t_start, &t_diff); 537 | total_elapsed = t_diff.tv_sec * 1000 + t_diff.tv_usec / 1000; 538 | timersub(&t_cur, &sc->t_last, &t_diff); 539 | since_last = t_diff.tv_sec * 1000 + t_diff.tv_usec / 1000; 540 | if (since_last == 0) 541 | continue; 542 | bwperc = (sc->bytes * 100.0) / mainstats.slice_bytes; 543 | mbps = (sc->bytes * 8) / (since_last * 1000.0); 544 | slice_mbps += mbps; 545 | 546 | tcp_stats_display(total_elapsed, mbps, bwperc, sc, &tcpi); 547 | 548 | sc->t_last = t_cur; 549 | sc->bytes = 0; 550 | } 551 | 552 | /* process stats for this slice */ 553 | if (slice_mbps > mainstats.peak_mbps) 554 | mainstats.peak_mbps = slice_mbps; 555 | if (slice_mbps < mainstats.floor_mbps) 556 | mainstats.floor_mbps = slice_mbps; 557 | old_mean_mbps = mainstats.mean_mbps; 558 | mainstats.mean_mbps += (slice_mbps - mainstats.mean_mbps) / 559 | mainstats.n_slices; 560 | 561 | /* "Welford's method" for online variance 562 | * see Knuth, TAoCP Volume 2, 3rd edn., p232 */ 563 | mainstats.nvariance_mbps += (slice_mbps - old_mean_mbps) * 564 | (slice_mbps - mainstats.mean_mbps); 565 | 566 | printf("Conn: %3d Mbps: %12.3Lf Peak Mbps: %12.3Lf Avg Mbps: %12.3Lf\n", 567 | mainstats.nconns, slice_mbps, mainstats.peak_mbps, 568 | mainstats.nconns ? slice_mbps / mainstats.nconns : 0); 569 | 570 | mainstats.slice_bytes = 0; 571 | set_slice_timer(mainstats.nconns > 0); 572 | } 573 | 574 | static void 575 | udp_process_slice(int fd, short event, void *bula) 576 | { 577 | unsigned long long total_elapsed, since_last, pps; 578 | long double old_mean_mbps, slice_mbps; 579 | struct timeval t_cur, t_diff; 580 | 581 | mainstats.n_slices++; 582 | 583 | if (clock_gettime_tv(CLOCK_MONOTONIC, &t_cur) == -1) 584 | err(1, "clock_gettime_tv"); 585 | 586 | timersub(&t_cur, &udp_sc->t_start, &t_diff); 587 | total_elapsed = t_diff.tv_sec * 1000 + t_diff.tv_usec / 1000; 588 | 589 | timersub(&t_cur, &udp_sc->t_last, &t_diff); 590 | since_last = t_diff.tv_sec * 1000 + t_diff.tv_usec / 1000; 591 | if (since_last == 0) 592 | return; 593 | 594 | slice_mbps = (udp_sc->bytes * 8) / (since_last * 1000.0); 595 | pps = (udp_sc->udp_slice_pkts * 1000) / since_last; 596 | 597 | if (slice_mbps > mainstats.peak_mbps) 598 | mainstats.peak_mbps = slice_mbps; 599 | if (slice_mbps < mainstats.floor_mbps) 600 | mainstats.floor_mbps = slice_mbps; 601 | old_mean_mbps = mainstats.mean_mbps; 602 | mainstats.mean_mbps += (slice_mbps - mainstats.mean_mbps) / 603 | mainstats.n_slices; 604 | 605 | /* "Welford's method" for online variance 606 | * see Knuth, TAoCP Volume 2, 3rd edn., p232 */ 607 | mainstats.nvariance_mbps += (slice_mbps - old_mean_mbps) * 608 | (slice_mbps - mainstats.mean_mbps); 609 | 610 | printf("Elapsed: %11llu Mbps: %11.3Lf Peak Mbps: %11.3Lf %s PPS: %7llu\n", 611 | total_elapsed, slice_mbps, mainstats.peak_mbps, 612 | ptb->sflag ? "Rx" : "Tx", pps); 613 | 614 | /* Clean up this slice time */ 615 | udp_sc->t_last = t_cur; 616 | udp_sc->bytes = 0; 617 | udp_sc->udp_slice_pkts = 0; 618 | 619 | mainstats.slice_bytes = 0; 620 | set_slice_timer(1); 621 | } 622 | 623 | static void 624 | udp_server_handle_sc(int fd, short event, void *bula) 625 | { 626 | static int first_read = 1; 627 | ssize_t n; 628 | 629 | n = read(fd, ptb->dummybuf, ptb->dummybuf_len); 630 | if (n == 0) 631 | return; 632 | else if (n == -1) { 633 | if (errno != EINTR && errno != EWOULDBLOCK) 634 | warn("fd %d read error", fd); 635 | return; 636 | } 637 | 638 | if (ptb->vflag >= 3) 639 | fprintf(stderr, "read: %zd bytes\n", n); 640 | if (first_read) { 641 | first_read = 0; 642 | stats_prepare(udp_sc); 643 | set_slice_timer(1); 644 | } 645 | /* Account packet */ 646 | udp_sc->udp_slice_pkts++; 647 | udp_sc->bytes += n; 648 | mainstats.slice_bytes += n; 649 | mainstats.total_bytes += n; 650 | } 651 | 652 | static void 653 | tcp_server_handle_sc(int fd, short event, void *v_sc) 654 | { 655 | struct statctx *sc = v_sc; 656 | ssize_t n; 657 | 658 | if (sc->tls) 659 | n = tls_read(sc->tls, sc->buf, sc->buflen); 660 | else 661 | n = read(sc->fd, sc->buf, sc->buflen); 662 | if (n == -1) { 663 | if (sc->tls) 664 | err(1, "tls_read: %s", tls_error(sc->tls)); 665 | if (errno != EINTR && errno != EWOULDBLOCK) 666 | warn("fd %d read error", sc->fd); 667 | return; 668 | } else if (n == 0) { 669 | if (ptb->vflag) 670 | fprintf(stderr, "%8d closed by remote end\n", sc->fd); 671 | 672 | TAILQ_REMOVE(&sc_queue, sc, entry); 673 | 674 | event_del(&sc->ev); 675 | close(sc->fd); 676 | 677 | /* Some file descriptors are available again. */ 678 | if (evtimer_pending(&sc->tcp_ts->evt, NULL)) { 679 | evtimer_del(&sc->tcp_ts->evt); 680 | event_add(&sc->tcp_ts->ev, NULL); 681 | } 682 | 683 | free(sc); 684 | mainstats.nconns--; 685 | return; 686 | } 687 | if (ptb->vflag >= 3) 688 | fprintf(stderr, "read: %zd bytes\n", n); 689 | sc->bytes += n; 690 | mainstats.slice_bytes += n; 691 | mainstats.total_bytes += n; 692 | } 693 | 694 | static int 695 | timeout_tls(int s, struct tls *tls_ctx, int (*func)(struct tls *)) 696 | { 697 | struct pollfd pfd; 698 | int ret; 699 | 700 | while ((ret = func(tls_ctx)) != 0) { 701 | if (ret == TLS_WANT_POLLIN) 702 | pfd.events = POLLIN; 703 | else if (ret == TLS_WANT_POLLOUT) 704 | pfd.events = POLLOUT; 705 | else 706 | break; 707 | pfd.fd = s; 708 | if ((ret = poll(&pfd, 1, -1)) == 1) 709 | continue; 710 | else if (ret == 0) { 711 | errno = ETIMEDOUT; 712 | ret = -1; 713 | break; 714 | } else 715 | err(1, "poll failed"); 716 | } 717 | 718 | return ret; 719 | } 720 | 721 | static void 722 | tcp_server_accept(int fd, short event, void *arg) 723 | { 724 | struct tcpservsock *ts = arg; 725 | int sock; 726 | struct statctx *sc; 727 | struct sockaddr_storage ss; 728 | socklen_t sslen; 729 | char tmp[NI_MAXHOST + 2 + NI_MAXSERV]; 730 | static struct tls *tls = NULL; 731 | 732 | if (ptb->tls_cfg && tls == NULL) { 733 | tls = tls_server(); 734 | if (tls == NULL) 735 | err(1, "Unable to create TLS context."); 736 | if (tls_configure(tls, ptb->tls_cfg) == -1) 737 | errx(1, "tls_configure: %s", tls_error(tls)); 738 | } 739 | 740 | sslen = sizeof(ss); 741 | 742 | event_add(&ts->ev, NULL); 743 | if (event & EV_TIMEOUT) 744 | return; 745 | if ((sock = accept4(fd, (struct sockaddr *)&ss, &sslen, SOCK_NONBLOCK)) 746 | == -1) { 747 | /* 748 | * Pause accept if we are out of file descriptors, or 749 | * libevent will haunt us here too. 750 | */ 751 | if (errno == ENFILE || errno == EMFILE) { 752 | struct timeval evtpause = { 1, 0 }; 753 | 754 | event_del(&ts->ev); 755 | evtimer_add(&ts->evt, &evtpause); 756 | } else if (errno != EWOULDBLOCK && errno != EINTR && 757 | errno != ECONNABORTED) 758 | warn("accept"); 759 | return; 760 | } 761 | saddr_ntop((struct sockaddr *)&ss, sslen, 762 | tmp, sizeof(tmp)); 763 | if (ptb->Tflag != -1 && ss.ss_family == AF_INET) { 764 | if (setsockopt(sock, IPPROTO_IP, IP_TOS, 765 | &ptb->Tflag, sizeof(ptb->Tflag))) 766 | err(1, "setsockopt IP_TOS"); 767 | } 768 | if (ptb->Tflag != -1 && ss.ss_family == AF_INET6) { 769 | if (setsockopt(sock, IPPROTO_IPV6, IPV6_TCLASS, 770 | &ptb->Tflag, sizeof(ptb->Tflag))) 771 | err(1, "setsockopt IPV6_TCLASS"); 772 | } 773 | /* Alloc client structure and register reading callback */ 774 | if ((sc = calloc(1, sizeof(*sc))) == NULL) 775 | err(1, "calloc"); 776 | sc->tcp_ts = ts; 777 | sc->fd = sock; 778 | stats_prepare(sc); 779 | if (tls && tls_accept_socket(tls, &sc->tls, sc->fd) == -1) 780 | err(1, "tls_accept_socket: %s", tls_error(tls)); 781 | 782 | event_set(&sc->ev, sc->fd, EV_READ | EV_PERSIST, 783 | tcp_server_handle_sc, sc); 784 | event_add(&sc->ev, NULL); 785 | TAILQ_INSERT_TAIL(&sc_queue, sc, entry); 786 | 787 | mainstats.nconns++; 788 | if (mainstats.nconns == 1) 789 | set_slice_timer(1); 790 | if (ptb->vflag) 791 | fprintf(stderr, "Accepted connection from %s, fd = %d\n", 792 | tmp, sc->fd); 793 | } 794 | 795 | static void 796 | server_init(struct addrinfo *aitop) 797 | { 798 | int sock, on = 1; 799 | struct addrinfo *ai; 800 | struct event *ev; 801 | struct tcpservsock *ts; 802 | nfds_t lnfds; 803 | 804 | lnfds = 0; 805 | for (ai = aitop; ai != NULL; ai = ai->ai_next) { 806 | char tmp[NI_MAXHOST + 2 + NI_MAXSERV]; 807 | 808 | saddr_ntop(ai->ai_addr, ai->ai_addrlen, tmp, sizeof(tmp)); 809 | if (ptb->vflag) 810 | fprintf(stderr, "Try to bind to %s\n", tmp); 811 | if ((sock = socket(ai->ai_family, ai->ai_socktype, 812 | ai->ai_protocol)) == -1) { 813 | if (ai->ai_next == NULL) 814 | err(1, "socket"); 815 | if (ptb->vflag) 816 | warn("socket"); 817 | continue; 818 | } 819 | if (ptb->Dflag) { 820 | if (setsockopt(sock, SOL_SOCKET, SO_DEBUG, 821 | &ptb->Dflag, sizeof(ptb->Dflag))) 822 | err(1, "setsockopt SO_DEBUG"); 823 | } 824 | if (ptb->Tflag != -1 && ai->ai_family == AF_INET) { 825 | if (setsockopt(sock, IPPROTO_IP, IP_TOS, 826 | &ptb->Tflag, sizeof(ptb->Tflag))) 827 | err(1, "setsockopt IP_TOS"); 828 | } 829 | if (ptb->Tflag != -1 && ai->ai_family == AF_INET6) { 830 | if (setsockopt(sock, IPPROTO_IPV6, IPV6_TCLASS, 831 | &ptb->Tflag, sizeof(ptb->Tflag))) 832 | err(1, "setsockopt IPV6_TCLASS"); 833 | } 834 | #ifndef __OpenBSD__ 835 | if (ai->ai_family == AF_INET6) { 836 | if (setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, 837 | &on, sizeof(on)) == -1) 838 | warn("ipv6 only"); 839 | } 840 | #endif 841 | if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, 842 | &on, sizeof(on)) == -1) 843 | warn("reuse port"); 844 | if (bind(sock, ai->ai_addr, ai->ai_addrlen) != 0) { 845 | if (ai->ai_next == NULL) 846 | err(1, "bind"); 847 | if (ptb->vflag) 848 | warn("bind"); 849 | close(sock); 850 | continue; 851 | } 852 | if (ptb->Sflag) { 853 | if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF, 854 | &ptb->Sflag, sizeof(ptb->Sflag)) == -1) 855 | warn("set receive socket buffer size"); 856 | } 857 | if (TCP_MODE) { 858 | if (listen(sock, 64) == -1) { 859 | if (ai->ai_next == NULL) 860 | err(1, "listen"); 861 | if (ptb->vflag) 862 | warn("listen"); 863 | close(sock); 864 | continue; 865 | } 866 | } 867 | if (UDP_MODE) { 868 | if ((ev = calloc(1, sizeof(*ev))) == NULL) 869 | err(1, "calloc"); 870 | event_set(ev, sock, EV_READ | EV_PERSIST, 871 | udp_server_handle_sc, NULL); 872 | event_add(ev, NULL); 873 | } else { 874 | if ((ts = calloc(1, sizeof(*ts))) == NULL) 875 | err(1, "calloc"); 876 | 877 | ts->fd = sock; 878 | evtimer_set(&ts->evt, tcp_server_accept, ts); 879 | event_set(&ts->ev, ts->fd, EV_READ, 880 | tcp_server_accept, ts); 881 | event_add(&ts->ev, NULL); 882 | } 883 | if (ptb->vflag >= 3) 884 | fprintf(stderr, "bound to fd %d\n", sock); 885 | lnfds++; 886 | } 887 | if (!ptb->Uflag) 888 | freeaddrinfo(aitop); 889 | if (lnfds == 0) 890 | errx(1, "No working listen addresses found"); 891 | } 892 | 893 | static void 894 | client_handle_sc(int fd, short event, void *v_sc) 895 | { 896 | struct statctx *sc = v_sc; 897 | ssize_t n; 898 | size_t blen = sc->buflen; 899 | 900 | if (ptb->Rflag) 901 | blen = arc4random_uniform(blen) + 1; 902 | 903 | if (sc->tls) 904 | n = tls_write(sc->tls, sc->buf, blen); 905 | else 906 | n = write(sc->fd, sc->buf, blen); 907 | if (n == -1) { 908 | if (sc->tls) 909 | warn("tls_write: %s", tls_error(sc->tls)); 910 | if (errno == EINTR || errno == EWOULDBLOCK || 911 | (UDP_MODE && errno == ENOBUFS)) 912 | return; 913 | warn("write"); 914 | wrapup(1); 915 | } 916 | if (TCP_MODE && n == 0) { 917 | fprintf(stderr, "Remote end closed connection"); 918 | wrapup(1); 919 | } 920 | if (ptb->vflag >= 3) 921 | fprintf(stderr, "write: %zd bytes\n", n); 922 | sc->bytes += n; 923 | mainstats.slice_bytes += n; 924 | mainstats.total_bytes += n; 925 | if (UDP_MODE) 926 | sc->udp_slice_pkts++; 927 | } 928 | 929 | static void 930 | client_init(struct addrinfo *aitop, int nconn, struct addrinfo *aib) 931 | { 932 | struct statctx *sc; 933 | struct addrinfo *ai; 934 | int i, r, sock; 935 | 936 | for (i = 0; i < nconn; i++) { 937 | for (sock = -1, ai = aitop; ai != NULL; ai = ai->ai_next) { 938 | char tmp[NI_MAXHOST + 2 + NI_MAXSERV]; 939 | 940 | saddr_ntop(ai->ai_addr, ai->ai_addrlen, tmp, 941 | sizeof(tmp)); 942 | if (ptb->vflag && i == 0) 943 | fprintf(stderr, "Trying %s\n", tmp); 944 | if ((sock = socket(ai->ai_family, ai->ai_socktype, 945 | ai->ai_protocol)) == -1) { 946 | if (ai->ai_next == NULL) 947 | err(1, "socket"); 948 | if (ptb->vflag) 949 | warn("socket"); 950 | continue; 951 | } 952 | if (ptb->Dflag) { 953 | if (setsockopt(sock, SOL_SOCKET, SO_DEBUG, 954 | &ptb->Dflag, sizeof(ptb->Dflag))) 955 | err(1, "setsockopt SO_DEBUG"); 956 | } 957 | if (aib != NULL) { 958 | saddr_ntop(aib->ai_addr, aib->ai_addrlen, 959 | tmp, sizeof(tmp)); 960 | if (ptb->vflag) 961 | fprintf(stderr, 962 | "Try to bind to %s\n", tmp); 963 | if (bind(sock, (struct sockaddr *)aib->ai_addr, 964 | aib->ai_addrlen) == -1) 965 | err(1, "bind"); 966 | } 967 | if (ptb->Tflag != -1 && ai->ai_family == AF_INET) { 968 | if (setsockopt(sock, IPPROTO_IP, IP_TOS, 969 | &ptb->Tflag, sizeof(ptb->Tflag))) 970 | err(1, "setsockopt IP_TOS"); 971 | } 972 | if (ptb->Tflag != -1 && ai->ai_family == AF_INET6) { 973 | if (setsockopt(sock, IPPROTO_IPV6, IPV6_TCLASS, 974 | &ptb->Tflag, sizeof(ptb->Tflag))) 975 | err(1, "setsockopt IPV6_TCLASS"); 976 | } 977 | if (ptb->Sflag) { 978 | if (setsockopt(sock, SOL_SOCKET, SO_SNDBUF, 979 | &ptb->Sflag, sizeof(ptb->Sflag)) == -1) 980 | warn("set send socket buffer size"); 981 | } 982 | if (connect(sock, ai->ai_addr, ai->ai_addrlen) != 0) { 983 | if (ai->ai_next == NULL) 984 | err(1, "connect"); 985 | if (ptb->vflag) 986 | warn("connect"); 987 | close(sock); 988 | sock = -1; 989 | continue; 990 | } 991 | break; 992 | } 993 | if (sock == -1) 994 | errx(1, "No host found"); 995 | if ((r = fcntl(sock, F_GETFL)) == -1) 996 | err(1, "fcntl(F_GETFL)"); 997 | r |= O_NONBLOCK; 998 | if (fcntl(sock, F_SETFL, r) == -1) 999 | err(1, "fcntl(F_SETFL, O_NONBLOCK)"); 1000 | /* Alloc and prepare stats */ 1001 | if (TCP_MODE) { 1002 | if ((sc = calloc(1, sizeof(*sc))) == NULL) 1003 | err(1, "calloc"); 1004 | } else 1005 | sc = udp_sc; 1006 | 1007 | sc->fd = sock; 1008 | 1009 | if (ptb->tls_cfg) { 1010 | sc->tls = tls_client(); 1011 | if (sc->tls == NULL) 1012 | err(1, "Unable to create TLS context."); 1013 | 1014 | if (tls_configure(sc->tls, ptb->tls_cfg) == -1) 1015 | errx(1, "tls_configure: %s", 1016 | tls_error(sc->tls)); 1017 | 1018 | if (tls_connect_socket(sc->tls, sc->fd, 1019 | mainstats.host) == -1) 1020 | errx(1, "tls_connect_socket: %s", 1021 | tls_error(sc->tls)); 1022 | if (timeout_tls(sc->fd, sc->tls, tls_handshake) == -1) { 1023 | const char *errstr; 1024 | 1025 | if ((errstr = tls_error(sc->tls)) == NULL) 1026 | errstr = strerror(errno); 1027 | errx(1, "tls handshake failed (%s)", errstr); 1028 | } 1029 | } 1030 | 1031 | stats_prepare(sc); 1032 | 1033 | event_set(&sc->ev, sc->fd, EV_WRITE | EV_PERSIST, 1034 | client_handle_sc, sc); 1035 | event_add(&sc->ev, NULL); 1036 | TAILQ_INSERT_TAIL(&sc_queue, sc, entry); 1037 | 1038 | mainstats.nconns++; 1039 | if (mainstats.nconns == 1) 1040 | set_slice_timer(1); 1041 | } 1042 | if (!ptb->Uflag) 1043 | freeaddrinfo(aitop); 1044 | if (aib != NULL) 1045 | freeaddrinfo(aib); 1046 | 1047 | if (ptb->vflag && nconn > 1) 1048 | fprintf(stderr, "%d connections established\n", 1049 | mainstats.nconns); 1050 | } 1051 | 1052 | #ifdef __OpenBSD__ 1053 | static int 1054 | map_tos(char *s, int *val) 1055 | { 1056 | /* DiffServ Codepoints and other TOS mappings */ 1057 | const struct toskeywords { 1058 | const char *keyword; 1059 | int val; 1060 | } *t, toskeywords[] = { 1061 | { "af11", IPTOS_DSCP_AF11 }, 1062 | { "af12", IPTOS_DSCP_AF12 }, 1063 | { "af13", IPTOS_DSCP_AF13 }, 1064 | { "af21", IPTOS_DSCP_AF21 }, 1065 | { "af22", IPTOS_DSCP_AF22 }, 1066 | { "af23", IPTOS_DSCP_AF23 }, 1067 | { "af31", IPTOS_DSCP_AF31 }, 1068 | { "af32", IPTOS_DSCP_AF32 }, 1069 | { "af33", IPTOS_DSCP_AF33 }, 1070 | { "af41", IPTOS_DSCP_AF41 }, 1071 | { "af42", IPTOS_DSCP_AF42 }, 1072 | { "af43", IPTOS_DSCP_AF43 }, 1073 | { "critical", IPTOS_PREC_CRITIC_ECP }, 1074 | { "cs0", IPTOS_DSCP_CS0 }, 1075 | { "cs1", IPTOS_DSCP_CS1 }, 1076 | { "cs2", IPTOS_DSCP_CS2 }, 1077 | { "cs3", IPTOS_DSCP_CS3 }, 1078 | { "cs4", IPTOS_DSCP_CS4 }, 1079 | { "cs5", IPTOS_DSCP_CS5 }, 1080 | { "cs6", IPTOS_DSCP_CS6 }, 1081 | { "cs7", IPTOS_DSCP_CS7 }, 1082 | { "ef", IPTOS_DSCP_EF }, 1083 | { "inetcontrol", IPTOS_PREC_INTERNETCONTROL }, 1084 | { "lowdelay", IPTOS_LOWDELAY }, 1085 | { "netcontrol", IPTOS_PREC_NETCONTROL }, 1086 | { "reliability", IPTOS_RELIABILITY }, 1087 | { "throughput", IPTOS_THROUGHPUT }, 1088 | { NULL, -1 }, 1089 | }; 1090 | 1091 | for (t = toskeywords; t->keyword != NULL; t++) { 1092 | if (strcmp(s, t->keyword) == 0) { 1093 | *val = t->val; 1094 | return (1); 1095 | } 1096 | } 1097 | 1098 | return (0); 1099 | } 1100 | #endif 1101 | 1102 | static void 1103 | quit(int sig, short event, void *arg) 1104 | { 1105 | wrapup(0); 1106 | } 1107 | 1108 | static void 1109 | wrapup(int err) 1110 | { 1111 | const int transfers = timerisset(&mainstats.t_first); 1112 | const int stats = (mainstats.floor_mbps != INFINITY); 1113 | 1114 | if (transfers) { 1115 | if (!stats) { 1116 | if (UDP_MODE) 1117 | udp_process_slice(0, 0, NULL); 1118 | else 1119 | tcp_process_slice(0, 0, NULL); 1120 | } 1121 | 1122 | summary_display(); 1123 | } 1124 | 1125 | if (err != -1) 1126 | exit(err); 1127 | } 1128 | 1129 | static int 1130 | process_tls_opt(char *s) 1131 | { 1132 | size_t len; 1133 | char *v; 1134 | 1135 | const struct tlskeywords { 1136 | const char *keyword; 1137 | char **value; 1138 | } *t, tlskeywords[] = { 1139 | { "ciphers", &tls_ciphers }, 1140 | { "protocols", &tls_protocols }, 1141 | { NULL, NULL }, 1142 | }; 1143 | 1144 | len = strlen(s); 1145 | if ((v = strchr(s, '=')) != NULL) { 1146 | len = v - s; 1147 | v++; 1148 | } 1149 | 1150 | for (t = tlskeywords; t->keyword != NULL; t++) { 1151 | if (strlen(t->keyword) == len && 1152 | strncmp(s, t->keyword, len) == 0) { 1153 | if (v == NULL) 1154 | errx(1, "invalid tls value `%s'", s); 1155 | *t->value = v; 1156 | return 1; 1157 | } 1158 | } 1159 | return 0; 1160 | } 1161 | 1162 | int 1163 | main(int argc, char **argv) 1164 | { 1165 | struct timeval tv; 1166 | #ifdef __OpenBSD__ 1167 | unsigned int secs, rtable; 1168 | #else 1169 | unsigned int secs; 1170 | #endif 1171 | char *tmp; 1172 | struct addrinfo *aitop, *aib, hints; 1173 | const char *errstr; 1174 | struct rlimit rl; 1175 | int ch, herr, nconn, usetls = 0; 1176 | int family = PF_UNSPEC; 1177 | const char *host = NULL, *port = DEFAULT_PORT, *srcbind = NULL; 1178 | #ifdef __OpenBSD__ 1179 | struct event ev_sigint, ev_sigterm, ev_sighup, ev_siginfo, ev_progtimer; 1180 | #else 1181 | struct event ev_sigint, ev_sigterm, ev_sighup, ev_progtimer; 1182 | #endif 1183 | struct sockaddr_un sock_un; 1184 | char *crtfile = NULL, *keyfile = NULL; 1185 | uint8_t *crt = NULL, *key = NULL; 1186 | size_t key_size, crt_size; 1187 | 1188 | /* Init world */ 1189 | setvbuf(stdout, NULL, _IOLBF, 0); 1190 | ptb = &tcpbench; 1191 | ptb->dummybuf_len = 0; 1192 | ptb->Dflag = 0; 1193 | ptb->Sflag = ptb->sflag = ptb->vflag = ptb->Rflag = ptb->Uflag = 0; 1194 | ptb->kvars = NULL; 1195 | ptb->rflag = DEFAULT_STATS_INTERVAL; 1196 | ptb->Tflag = -1; 1197 | ptb->tls_cfg = NULL; 1198 | nconn = 1; 1199 | aib = NULL; 1200 | secs = 0; 1201 | 1202 | while ((ch = getopt(argc, argv, "46b:B:cC:Dhlk:K:n:p:Rr:sS:t:T:uUvV:")) 1203 | != -1) { 1204 | switch (ch) { 1205 | case '4': 1206 | family = PF_INET; 1207 | break; 1208 | case '6': 1209 | family = PF_INET6; 1210 | break; 1211 | case 'b': 1212 | srcbind = optarg; 1213 | break; 1214 | case 'c': 1215 | usetls = 1; 1216 | break; 1217 | case 'C': 1218 | crtfile = optarg; 1219 | break; 1220 | case 'D': 1221 | ptb->Dflag = 1; 1222 | break; 1223 | case 'l': 1224 | list_kvars(); 1225 | exit(0); 1226 | case 'k': 1227 | if ((tmp = strdup(optarg)) == NULL) 1228 | err(1, "strdup"); 1229 | ptb->kvars = check_prepare_kvars(tmp); 1230 | free(tmp); 1231 | break; 1232 | case 'K': 1233 | keyfile = optarg; 1234 | break; 1235 | case 'R': 1236 | ptb->Rflag = 1; 1237 | break; 1238 | case 'r': 1239 | ptb->rflag = strtonum(optarg, 0, 60 * 60 * 24 * 1000, 1240 | &errstr); 1241 | if (errstr != NULL) 1242 | errx(1, "statistics interval is %s: %s", 1243 | errstr, optarg); 1244 | break; 1245 | case 'p': 1246 | port = optarg; 1247 | break; 1248 | case 's': 1249 | ptb->sflag = 1; 1250 | break; 1251 | case 'S': 1252 | ptb->Sflag = strtonum(optarg, 0, 1024*1024*1024, 1253 | &errstr); 1254 | if (errstr != NULL) 1255 | errx(1, "socket buffer size is %s: %s", 1256 | errstr, optarg); 1257 | break; 1258 | case 'B': 1259 | ptb->dummybuf_len = strtonum(optarg, 0, 1024*1024*1024, 1260 | &errstr); 1261 | if (errstr != NULL) 1262 | errx(1, "read/write buffer size is %s: %s", 1263 | errstr, optarg); 1264 | break; 1265 | case 'v': 1266 | ptb->vflag++; 1267 | break; 1268 | #ifdef __OpenBSD__ 1269 | case 'V': 1270 | rtable = (unsigned int)strtonum(optarg, 0, 1271 | RT_TABLEID_MAX, &errstr); 1272 | if (errstr) 1273 | errx(1, "rtable value is %s: %s", 1274 | errstr, optarg); 1275 | if (setrtable(rtable) == -1) 1276 | err(1, "setrtable"); 1277 | break; 1278 | #endif 1279 | case 'n': 1280 | nconn = strtonum(optarg, 0, 65535, &errstr); 1281 | if (errstr != NULL) 1282 | errx(1, "number of connections is %s: %s", 1283 | errstr, optarg); 1284 | break; 1285 | case 'u': 1286 | ptb->uflag = 1; 1287 | break; 1288 | case 'U': 1289 | ptb->Uflag = 1; 1290 | break; 1291 | case 'T': 1292 | if (process_tls_opt(optarg)) 1293 | break; 1294 | #ifdef __OpenBSD__ 1295 | if (map_tos(optarg, &ptb->Tflag)) 1296 | break; 1297 | #endif 1298 | errstr = NULL; 1299 | if (strlen(optarg) > 1 && optarg[0] == '0' && 1300 | optarg[1] == 'x') 1301 | ptb->Tflag = (int)strtol(optarg, NULL, 16); 1302 | else 1303 | ptb->Tflag = (int)strtonum(optarg, 0, 255, 1304 | &errstr); 1305 | if (ptb->Tflag == -1 || ptb->Tflag > 255 || errstr) 1306 | errx(1, "illegal tos value %s", optarg); 1307 | break; 1308 | case 't': 1309 | secs = strtonum(optarg, 1, UINT_MAX, &errstr); 1310 | if (errstr != NULL) 1311 | errx(1, "secs is %s: %s", 1312 | errstr, optarg); 1313 | break; 1314 | case 'h': 1315 | default: 1316 | usage(); 1317 | } 1318 | } 1319 | 1320 | #ifdef __OpenBSD__ 1321 | if (pledge("stdio unveil rpath dns inet unix id", NULL) == -1) 1322 | err(1, "pledge"); 1323 | #endif 1324 | 1325 | argv += optind; 1326 | argc -= optind; 1327 | if ((argc != (ptb->sflag && !ptb->Uflag ? 0 : 1)) || 1328 | (UDP_MODE && (ptb->kvars || nconn != 1 || usetls))) 1329 | usage(); 1330 | 1331 | if (ptb->sflag && usetls && (crtfile == NULL || keyfile == NULL)) 1332 | usage(); 1333 | 1334 | if (crtfile != NULL && keyfile != NULL) { 1335 | if ((crt = tls_load_file(crtfile, &crt_size, NULL)) == NULL) 1336 | err(1, "tls_load_file"); 1337 | if ((key = tls_load_file(keyfile, &key_size, NULL)) == NULL) 1338 | err(1, "tls_load_file"); 1339 | } 1340 | 1341 | if (!ptb->sflag || ptb->Uflag) 1342 | mainstats.host = host = argv[0]; 1343 | 1344 | #ifdef __OpenBSD__ 1345 | if (ptb->Uflag) 1346 | if (unveil(host, "rwc") == -1) 1347 | err(1, "unveil %s", host); 1348 | 1349 | if (pledge("stdio id dns inet unix", NULL) == -1) 1350 | err(1, "pledge"); 1351 | #endif 1352 | 1353 | /* 1354 | * Rationale, 1355 | * If TCP, use a big buffer with big reads/writes. 1356 | * If UDP, use a big buffer in server and a buffer the size of a 1357 | * ethernet packet. 1358 | */ 1359 | if (!ptb->dummybuf_len) { 1360 | if (ptb->sflag || TCP_MODE) 1361 | ptb->dummybuf_len = DEFAULT_BUF; 1362 | else 1363 | ptb->dummybuf_len = DEFAULT_UDP_PKT; 1364 | } 1365 | 1366 | bzero(&hints, sizeof(hints)); 1367 | hints.ai_family = family; 1368 | if (UDP_MODE) { 1369 | hints.ai_socktype = SOCK_DGRAM; 1370 | hints.ai_protocol = IPPROTO_UDP; 1371 | } else { 1372 | hints.ai_socktype = SOCK_STREAM; 1373 | hints.ai_protocol = IPPROTO_TCP; 1374 | } 1375 | if (ptb->Uflag) { 1376 | hints.ai_family = AF_UNIX; 1377 | hints.ai_protocol = 0; 1378 | sock_un.sun_family = AF_UNIX; 1379 | if (strlcpy(sock_un.sun_path, host, sizeof(sock_un.sun_path)) >= 1380 | sizeof(sock_un.sun_path)) 1381 | errx(1, "socket name '%s' too long", host); 1382 | hints.ai_addr = (struct sockaddr *)&sock_un; 1383 | hints.ai_addrlen = sizeof(sock_un); 1384 | aitop = &hints; 1385 | } else { 1386 | if (ptb->sflag) 1387 | hints.ai_flags = AI_PASSIVE; 1388 | if (srcbind != NULL) { 1389 | hints.ai_flags |= AI_NUMERICHOST; 1390 | herr = getaddrinfo(srcbind, NULL, &hints, &aib); 1391 | hints.ai_flags &= ~AI_NUMERICHOST; 1392 | if (herr != 0) { 1393 | if (herr == EAI_SYSTEM) 1394 | err(1, "getaddrinfo"); 1395 | else 1396 | errx(1, "getaddrinfo: %s", 1397 | gai_strerror(herr)); 1398 | } 1399 | } 1400 | if ((herr = getaddrinfo(host, port, &hints, &aitop)) != 0) { 1401 | if (herr == EAI_SYSTEM) 1402 | err(1, "getaddrinfo"); 1403 | else 1404 | errx(1, "getaddrinfo: %s", gai_strerror(herr)); 1405 | } 1406 | } 1407 | 1408 | #ifdef __OpenBSD__ 1409 | if (pledge("stdio id inet unix", NULL) == -1) 1410 | err(1, "pledge"); 1411 | #endif 1412 | 1413 | if (getrlimit(RLIMIT_NOFILE, &rl) == -1) 1414 | err(1, "getrlimit"); 1415 | if (rl.rlim_cur < MAX_FD) 1416 | rl.rlim_cur = MAX_FD; 1417 | if (setrlimit(RLIMIT_NOFILE, &rl)) 1418 | err(1, "setrlimit"); 1419 | if (getrlimit(RLIMIT_NOFILE, &rl) == -1) 1420 | err(1, "getrlimit"); 1421 | 1422 | #ifdef __OpenBSD__ 1423 | if (pledge("stdio inet unix", NULL) == -1) 1424 | err(1, "pledge"); 1425 | #endif 1426 | 1427 | if (usetls) { 1428 | uint32_t protocols = 0; 1429 | 1430 | if ((ptb->tls_cfg = tls_config_new()) == NULL) 1431 | errx(1, "unable to allocate TLS config"); 1432 | 1433 | if (ptb->sflag) { 1434 | if (tls_config_set_key_mem(ptb->tls_cfg, key, 1435 | key_size) == -1) 1436 | errx(1, "%s", tls_config_error(ptb->tls_cfg)); 1437 | if (tls_config_set_cert_mem(ptb->tls_cfg, crt, 1438 | crt_size) == -1) 1439 | errx(1, "%s", tls_config_error(ptb->tls_cfg)); 1440 | } else { 1441 | /* Don't check server certificate. */ 1442 | tls_config_insecure_noverifyname(ptb->tls_cfg); 1443 | tls_config_insecure_noverifycert(ptb->tls_cfg); 1444 | } 1445 | 1446 | if (tls_config_parse_protocols(&protocols, tls_protocols) == -1) 1447 | errx(1, "invalid TLS protocols `%s'", tls_protocols); 1448 | if (tls_config_set_protocols(ptb->tls_cfg, protocols) == -1) 1449 | errx(1, "%s", tls_config_error(ptb->tls_cfg)); 1450 | if (tls_config_set_ciphers(ptb->tls_cfg, tls_ciphers) == -1) 1451 | errx(1, "%s", tls_config_error(ptb->tls_cfg)); 1452 | } 1453 | 1454 | /* Init world */ 1455 | TAILQ_INIT(&sc_queue); 1456 | if ((ptb->dummybuf = malloc(ptb->dummybuf_len)) == NULL) 1457 | err(1, "malloc"); 1458 | arc4random_buf(ptb->dummybuf, ptb->dummybuf_len); 1459 | 1460 | timerclear(&mainstats.t_first); 1461 | mainstats.floor_mbps = INFINITY; 1462 | 1463 | /* Setup libevent and signals */ 1464 | event_init(); 1465 | signal_set(&ev_sigterm, SIGTERM, signal_handler, NULL); 1466 | signal_set(&ev_sighup, SIGHUP, signal_handler, NULL); 1467 | signal_set(&ev_sigint, SIGINT, signal_handler, NULL); 1468 | #ifdef __OpenBSD__ 1469 | signal_set(&ev_siginfo, SIGINFO, signal_handler, NULL); 1470 | #endif 1471 | signal_add(&ev_sigint, NULL); 1472 | signal_add(&ev_sigterm, NULL); 1473 | signal_add(&ev_sighup, NULL); 1474 | #ifdef __OpenBSD__ 1475 | signal_add(&ev_siginfo, NULL); 1476 | #endif 1477 | signal(SIGPIPE, SIG_IGN); 1478 | 1479 | if (UDP_MODE) { 1480 | if ((udp_sc = calloc(1, sizeof(*udp_sc))) == NULL) 1481 | err(1, "calloc"); 1482 | udp_sc->fd = -1; 1483 | evtimer_set(&mainstats.timer, udp_process_slice, NULL); 1484 | } else { 1485 | print_tcp_header(); 1486 | evtimer_set(&mainstats.timer, tcp_process_slice, NULL); 1487 | } 1488 | 1489 | if (ptb->sflag) 1490 | server_init(aitop); 1491 | else { 1492 | if (secs > 0) { 1493 | timerclear(&tv); 1494 | tv.tv_sec = secs + 1; 1495 | evtimer_set(&ev_progtimer, quit, NULL); 1496 | evtimer_add(&ev_progtimer, &tv); 1497 | } 1498 | client_init(aitop, nconn, aib); 1499 | 1500 | #ifdef __OpenBSD__ 1501 | if (pledge("stdio inet", NULL) == -1) 1502 | err(1, "pledge"); 1503 | #endif 1504 | } 1505 | 1506 | /* libevent main loop*/ 1507 | event_dispatch(); 1508 | 1509 | return (0); 1510 | } 1511 | --------------------------------------------------------------------------------