]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tools/tools/netrate/tcpp/tcpp_client.c
MFV r358616:
[FreeBSD/FreeBSD.git] / tools / tools / netrate / tcpp / tcpp_client.c
1 /*-
2  * Copyright (c) 2008-2009 Robert N. M. Watson
3  * Copyright (c) 2010 Juniper Networks, Inc.
4  * All rights reserved.
5  *
6  * This software was developed by Robert N. M. Watson under contract
7  * to Juniper Networks, Inc.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  * $FreeBSD$
31  */
32
33 #include <sys/types.h>
34 #include <sys/event.h>
35 #include <sys/resource.h>
36 #include <sys/sched.h>
37 #include <sys/socket.h>
38 #include <sys/sysctl.h>
39 #include <sys/time.h>
40 #include <sys/uio.h>
41 #include <sys/wait.h>
42
43 #include <netinet/in.h>
44 #include <netinet/tcp.h>
45
46 #include <err.h>
47 #include <errno.h>
48 #include <fcntl.h>
49 #include <inttypes.h>
50 #include <signal.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <unistd.h>
55
56 #include "tcpp.h"
57
58 #define min(x, y)       (x < y ? x : y)
59
60
61 /*
62  * Gist of each client worker: build up to mflag connections at a time, and
63  * pump data in to them somewhat fairly until tflag connections have been
64  * completed.
65  */
66 #define CONNECTION_MAGIC        0x87a3f56e
67 struct connection {
68         uint32_t        conn_magic;             /* Just magic. */
69         int             conn_fd;
70         struct tcpp_header      conn_header;    /* Header buffer. */
71         u_int           conn_header_sent;       /* Header bytes sent. */
72         u_int64_t       conn_data_sent;         /* Data bytes sent.*/
73 };
74
75 static u_char                    buffer[256 * 1024];    /* Buffer to send. */
76 static pid_t                    *pid_list;
77 static int                       kq;
78 static int                       started;       /* Number started so far. */
79 static int                       finished;      /* Number finished so far. */
80 static int                       counter;       /* IP number offset. */
81 static uint64_t                  payload_len;
82
83 static struct connection *
84 tcpp_client_newconn(void)
85 {
86         struct sockaddr_in sin;
87         struct connection *conn;
88         struct kevent kev;
89         int fd, i;
90
91         /*
92          * Spread load over available IPs, rotating through them as we go.  No
93          * attempt to localize IPs to particular workers.
94          */
95         sin = localipbase;
96         sin.sin_addr.s_addr = htonl(ntohl(localipbase.sin_addr.s_addr) +
97             (counter++ % Mflag));
98
99         fd = socket(PF_INET, SOCK_STREAM, 0);
100         if (fd < 0)
101                 err(-1, "socket");
102
103         if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
104                 err(-1, "fcntl");
105
106         i = 1;
107         if (setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, &i, sizeof(i)) < 0)
108                 err(-1, "setsockopt");
109         i = 1;
110         if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &i, sizeof(i)) < 0)
111                 err(-1, "setsockopt");
112 #if 0
113         i = 1;
114         if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i)) < 0)
115                 err(-1, "setsockopt");
116 #endif
117
118         if (lflag) {
119                 if (bind(fd, (struct sockaddr *)&sin, sizeof(sin)) < 0)
120                         err(-1, "bind");
121         }
122
123         if (connect(fd, (struct sockaddr *)&remoteip, sizeof(remoteip)) < 0 &&
124             errno != EINPROGRESS)
125                 err(-1, "connect");
126
127         conn = malloc(sizeof(*conn));
128         if (conn == NULL)
129                 return (NULL);
130         bzero(conn, sizeof(*conn));
131         conn->conn_magic = CONNECTION_MAGIC;
132         conn->conn_fd = fd;
133         conn->conn_header.th_magic = TCPP_MAGIC;
134         conn->conn_header.th_len = payload_len;
135         tcpp_header_encode(&conn->conn_header);
136
137         EV_SET(&kev, fd, EVFILT_WRITE, EV_ADD, 0, 0, conn);
138         if (kevent(kq, &kev, 1, NULL, 0, NULL) < 0)
139                 err(-1, "newconn kevent");
140
141         started++;
142         return (conn);
143 }
144
145 static void
146 tcpp_client_closeconn(struct connection *conn)
147 {
148
149         close(conn->conn_fd);
150         bzero(conn, sizeof(*conn));
151         free(conn);
152         finished++;
153 }
154
155 static void
156 tcpp_client_handleconn(struct kevent *kev)
157 {
158         struct connection *conn;
159         struct iovec iov[2];
160         ssize_t len, header_left;
161
162         conn = kev->udata;
163         if (conn->conn_magic != CONNECTION_MAGIC)
164                 errx(-1, "tcpp_client_handleconn: magic");
165
166         if (conn->conn_header_sent < sizeof(conn->conn_header)) {
167                 header_left = sizeof(conn->conn_header) -
168                     conn->conn_header_sent;
169                 iov[0].iov_base = ((u_char *)&conn->conn_header) +
170                     conn->conn_header_sent;
171                 iov[0].iov_len = header_left;
172                 iov[1].iov_base = buffer;
173                 iov[1].iov_len = min(sizeof(buffer), payload_len);
174                 len = writev(conn->conn_fd, iov, 2);
175                 if (len < 0) {
176                         tcpp_client_closeconn(conn);
177                         err(-1, "tcpp_client_handleconn: header write");
178                 }
179                 if (len == 0) {
180                         tcpp_client_closeconn(conn);
181                         errx(-1, "tcpp_client_handleconn: header write "
182                             "premature EOF");
183                 }
184                 if (len > header_left) {
185                         conn->conn_data_sent += (len - header_left);
186                         conn->conn_header_sent += header_left;
187                 } else
188                         conn->conn_header_sent += len;
189         } else {
190                 len = write(conn->conn_fd, buffer, min(sizeof(buffer),
191                     payload_len - conn->conn_data_sent));
192                 if (len < 0) {
193                         tcpp_client_closeconn(conn);
194                         err(-1, "tcpp_client_handleconn: data write");
195                 }
196                 if (len == 0) {
197                         tcpp_client_closeconn(conn);
198                         errx(-1, "tcpp_client_handleconn: data write: "
199                             "premature EOF");
200                 }
201                 conn->conn_data_sent += len;
202         }
203         if (conn->conn_data_sent >= payload_len) {
204                 /*
205                  * All is well.
206                  */
207                 tcpp_client_closeconn(conn);
208         }
209 }
210
211 static void
212 tcpp_client_worker(int workernum)
213 {
214         struct kevent *kev_array;
215         int i, numevents, kev_bytes;
216 #if defined(CPU_SETSIZE) && 0
217         cpu_set_t mask;
218         int ncpus;
219         size_t len;
220
221         if (Pflag) {
222                 len = sizeof(ncpus);
223                 if (sysctlbyname(SYSCTLNAME_CPUS, &ncpus, &len, NULL, 0) < 0)
224                         err(-1, "sysctlbyname: %s", SYSCTLNAME_CPUS);
225                 if (len != sizeof(ncpus))
226                         errx(-1, "sysctlbyname: %s: len %jd", SYSCTLNAME_CPUS,
227                             (intmax_t)len);
228
229                 CPU_ZERO(&mask);
230                 CPU_SET(workernum % ncpus, &mask);
231                 if (sched_setaffinity(0, CPU_SETSIZE, &mask) < 0)
232                         err(-1, "sched_setaffinity");
233         }
234 #endif
235         setproctitle("tcpp_client %d", workernum);
236
237         /*
238          * Add the worker number to the remote port.
239          */
240         remoteip.sin_port = htons(rflag + workernum);
241
242         kev_bytes = sizeof(*kev_array) * mflag;
243         kev_array = malloc(kev_bytes);
244         if (kev_array == NULL)
245                 err(-1, "malloc");
246         bzero(kev_array, kev_bytes);
247
248         kq = kqueue();
249         if (kq < 0)
250                 err(-1, "kqueue");
251
252         while (finished < tflag) {
253                 while ((started - finished < mflag) && (started < tflag))
254                         (void)tcpp_client_newconn();
255                 numevents = kevent(kq, NULL, 0, kev_array, mflag, NULL);
256                 if (numevents < 0)
257                         err(-1, "kevent");
258                 if (numevents > mflag)
259                         errx(-1, "kevent: %d", numevents);
260                 for (i = 0; i < numevents; i++)
261                         tcpp_client_handleconn(&kev_array[i]);
262         }
263         /* printf("Worker %d done - %d finished\n", workernum, finished); */
264 }
265
266 void
267 tcpp_client(void)
268 {
269         struct timespec ts_start, ts_finish;
270         long cp_time_start[CPUSTATES], cp_time_finish[CPUSTATES];
271         long ticks;
272         size_t size;
273         pid_t pid;
274         int i, failed, status;
275
276         if (bflag < sizeof(struct tcpp_header))
277                 errx(-1, "Can't use -b less than %zu\n",
278                    sizeof(struct tcpp_header));
279         payload_len = bflag - sizeof(struct tcpp_header);
280
281         pid_list = malloc(sizeof(*pid_list) * pflag);
282         if (pid_list == NULL)
283                 err(-1, "malloc pid_list");
284         bzero(pid_list, sizeof(*pid_list) * pflag);
285
286         /*
287          * Start workers.
288          */
289         size = sizeof(cp_time_start);
290         if (sysctlbyname(SYSCTLNAME_CPTIME, &cp_time_start, &size, NULL, 0)
291             < 0)
292                 err(-1, "sysctlbyname: %s", SYSCTLNAME_CPTIME);
293         if (clock_gettime(CLOCK_REALTIME, &ts_start) < 0)
294                 err(-1, "clock_gettime");
295         for (i = 0; i < pflag; i++) {
296                 pid = fork();
297                 if (pid < 0) {
298                         warn("fork");
299                         for (i = 0; i < pflag; i++) {
300                                 if (pid_list[i] != 0)
301                                         (void)kill(pid_list[i], SIGKILL);
302                         }
303                         exit(-1);
304                 }
305                 if (pid == 0) {
306                         tcpp_client_worker(i);
307                         exit(0);
308                 }
309                 pid_list[i] = pid;
310         }
311
312         /*
313          * GC workers.
314          */
315         failed = 0;
316         for (i = 0; i < pflag; i++) {
317                 if (pid_list[i] != 0) {
318                         while (waitpid(pid_list[i], &status, 0) != pid_list[i]);
319                         if (WEXITSTATUS(status) != 0)
320                                 failed = 1;
321                 }
322         }
323         if (clock_gettime(CLOCK_REALTIME, &ts_finish) < 0)
324                 err(-1, "clock_gettime");
325         size = sizeof(cp_time_finish);
326         if (sysctlbyname(SYSCTLNAME_CPTIME, &cp_time_finish, &size, NULL, 0)
327             < 0)
328                 err(-1, "sysctlbyname: %s", SYSCTLNAME_CPTIME);
329         timespecsub(&ts_finish, &ts_start, &ts_finish);
330
331         if (failed)
332                 errx(-1, "Too many errors");
333
334         if (hflag)
335                 printf("bytes,seconds,conn/s,Gb/s,user%%,nice%%,sys%%,"
336                     "intr%%,idle%%\n");
337
338         /*
339          * Configuration parameters.
340          */
341         printf("%jd,", bflag * tflag * pflag);
342         printf("%jd.%09jd,", (intmax_t)ts_finish.tv_sec,
343             (intmax_t)(ts_finish.tv_nsec));
344
345         /*
346          * Effective transmit rates.
347          */
348         printf("%f,", (double)(pflag * tflag)/
349             (ts_finish.tv_sec + ts_finish.tv_nsec * 1e-9));
350         printf("%f,", (double)(bflag * tflag * pflag * 8) /
351             (ts_finish.tv_sec + ts_finish.tv_nsec * 1e-9) * 1e-9);
352
353         /*
354          * CPU time (est).
355          */
356         ticks = 0;
357         for (i = 0; i < CPUSTATES; i++) {
358                 cp_time_finish[i] -= cp_time_start[i];
359                 ticks += cp_time_finish[i];
360         }
361         printf("%0.02f,", (float)(100 * cp_time_finish[CP_USER]) / ticks);
362         printf("%0.02f,", (float)(100 * cp_time_finish[CP_NICE]) / ticks);
363         printf("%0.02f,", (float)(100 * cp_time_finish[CP_SYS]) / ticks);
364         printf("%0.02f,", (float)(100 * cp_time_finish[CP_INTR]) / ticks);
365         printf("%0.02f", (float)(100 * cp_time_finish[CP_IDLE]) / ticks);
366         printf("\n");
367 }