tcpp_server.c revision 189623
1/*-
2 * Copyright (c) 2008-2009 Robert N. M. Watson
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: head/tools/tools/netrate/tcpp/tcpp_server.c 189623 2009-03-10 14:52:17Z rwatson $
27 */
28
29#include <sys/types.h>
30#include <sys/endian.h>
31#include <sys/event.h>
32#include <sys/resource.h>
33#include <sys/sched.h>
34#include <sys/socket.h>
35#include <sys/sysctl.h>
36#include <sys/time.h>
37#include <sys/wait.h>
38
39#include <netinet/in.h>
40
41#include <err.h>
42#include <fcntl.h>
43#include <inttypes.h>
44#include <signal.h>
45#include <stdio.h>
46#include <stdlib.h>
47#include <string.h>
48#include <unistd.h>
49
50#include "tcpp.h"
51
52/*
53 * Server side -- create a pool of processes, each listening on its own TCP
54 * port number for new connections.  The first 8 bytes of each connection
55 * will be a network byte order length, then there will be that number of
56 * bytes of data.  We use non-blocking sockets with kqueue to to avoid the
57 * overhead of threading or more than one process per processor, which makes
58 * things a bit awkward when dealing with data we care about.  As such, we
59 * read into a small character buffer which we then convert to a length once
60 * we have all the data.
61 */
62#define	CONNECTION_MAGIC	0x6392af27
63struct connection {
64	uint32_t	conn_magic;		/* Just magic. */
65	int		conn_fd;
66	struct tcpp_header	conn_header;	/* Header buffer. */
67	u_int		conn_header_len;	/* Bytes so far. */
68	u_int64_t	conn_data_len;		/* How much to sink. */
69	u_int64_t	conn_data_received;	/* How much so far. */
70};
71
72static pid_t			*pid_list;
73static int			 kq;
74
75static struct connection *
76tcpp_server_newconn(int listen_fd)
77{
78	struct connection *conn;
79	struct kevent kev;
80	int fd;
81
82	fd = accept(listen_fd, NULL, NULL);
83	if (fd < 0) {
84		warn("accept");
85		return (NULL);
86	}
87
88	if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
89		err(-1, "fcntl");
90
91	conn = malloc(sizeof(*conn));
92	if (conn == NULL)
93		return (NULL);
94	bzero(conn, sizeof(*conn));
95	conn->conn_magic = CONNECTION_MAGIC;
96	conn->conn_fd = fd;
97
98	/*
99	 * Register to read on the socket, and set our conn pointer as the
100	 * udata so we can find it quickly in the future.
101	 */
102	EV_SET(&kev, fd, EVFILT_READ, EV_ADD, 0, 0, conn);
103	if (kevent(kq, &kev, 1, NULL, 0, NULL) < 0)
104		err(-1, "kevent");
105
106	return (conn);
107}
108
109static void
110tcpp_server_closeconn(struct connection *conn)
111{
112
113	/*
114	 * Kqueue cleans up after itself once we close the socket, and since
115	 * we are processing only one kevent at a time, we don't need to
116	 * worry about watching out for future kevents referring to it.
117	 *
118	 * ... right?
119	 */
120	close(conn->conn_fd);
121	bzero(conn, sizeof(*conn));
122	free(conn);
123}
124
125static u_char buffer[256*1024];	/* Buffer in which to sink data. */
126static void
127tcpp_server_handleconn(struct kevent *kev)
128{
129	struct connection *conn;
130	ssize_t len;
131
132	conn = kev->udata;
133	if (conn->conn_magic != CONNECTION_MAGIC)
134		errx(-1, "tcpp_server_handleconn: magic");
135
136	if (conn->conn_header_len < sizeof(conn->conn_header)) {
137		len = read(conn->conn_fd,
138		    ((u_char *)&conn->conn_header) + conn->conn_header_len,
139		    sizeof(conn->conn_header) - conn->conn_header_len);
140		if (len < 0) {
141			warn("tcpp_server_handleconn: header read");
142			tcpp_server_closeconn(conn);
143			return;
144		}
145		if (len == 0) {
146			warnx("tcpp_server_handleconn: header premature eof");
147			tcpp_server_closeconn(conn);
148			return;
149		}
150		conn->conn_header_len += len;
151		if (conn->conn_header_len == sizeof(conn->conn_header)) {
152			tcpp_header_decode(&conn->conn_header);
153			if (conn->conn_header.th_magic != TCPP_MAGIC) {
154				warnx("tcpp_server_handleconn: bad magic");
155				tcpp_server_closeconn(conn);
156				return;
157			}
158		}
159	} else {
160		/*
161		 * Drain up to a buffer from the connection, so that we pay
162		 * attention to other connections too.
163		 */
164		len = read(conn->conn_fd, buffer, sizeof(buffer));
165		if (len < 0) {
166			warn("tcpp_server_handleconn: data bad read");
167			tcpp_server_closeconn(conn);
168			return;
169		}
170		if (len == 0 && conn->conn_data_received <
171		    conn->conn_header.th_len) {
172			warnx("tcpp_server_handleconn: data premature eof");
173			tcpp_server_closeconn(conn);
174			return;
175		}
176		conn->conn_data_received += len;
177		if (conn->conn_data_received > conn->conn_header.th_len) {
178			warnx("tcpp_server_handleconn: too much data");
179			tcpp_server_closeconn(conn);
180			return;
181		}
182		if (conn->conn_data_received == conn->conn_header.th_len) {
183			/*
184			 * All is well.
185			 */
186			tcpp_server_closeconn(conn);
187			return;
188		}
189	}
190}
191
192static void
193tcpp_server_worker(int workernum)
194{
195	int i, listen_sock, numevents;
196	struct kevent kev, *kev_array;
197	int kev_bytes;
198#if defined(CPU_SETSIZE) && 0
199	cpu_set_t mask;
200	int ncpus;
201	ssize_t len;
202
203	len = sizeof(ncpus);
204	if (sysctlbyname(SYSCTLNAME_CPUS, &ncpus, &len, NULL, 0) < 0)
205		err(-1, "sysctlbyname: %s", SYSCTLNAME_CPUS);
206	if (len != sizeof(ncpus))
207		errx(-1, "sysctlbyname: %s: len %jd", SYSCTLNAME_CPUS,
208		    (intmax_t)len);
209
210	CPU_ZERO(&mask);
211	CPU_SET(workernum % ncpus, &mask);
212	if (sched_setaffinity(0, CPU_SETSIZE, &mask) < 0)
213		err(-1, "sched_setaffinity");
214#endif
215	setproctitle("tcpp_server %d", workernum);
216
217	/* Allow an extra kevent for the listen socket. */
218	kev_bytes = sizeof(*kev_array) * (mflag + 1);
219	kev_array = malloc(kev_bytes);
220	if (kev_array == NULL)
221		err(-1, "malloc");
222	bzero(kev_array, kev_bytes);
223
224	/* XXXRW: Want to set and pin the CPU here. */
225
226	/*
227	 * Add the worker number to the local port.
228	 */
229	localipbase.sin_port = htons(rflag + workernum);
230
231	listen_sock = socket(PF_INET, SOCK_STREAM, 0);
232	if (listen_sock < 0)
233		err(-1, "socket");
234	i = 1;
235	if (setsockopt(listen_sock, SOL_SOCKET, SO_NOSIGPIPE, &i, sizeof(i))
236	    < 0)
237		err(-1, "setsockopt");
238	i = 1;
239	if (setsockopt(listen_sock, SOL_SOCKET, SO_REUSEPORT, &i, sizeof(i))
240	    < 0)
241		err(-1, "setsockopt");
242	if (bind(listen_sock, (struct sockaddr *)&localipbase,
243	    sizeof(localipbase)) < 0)
244		err(-1, "bind");
245	if (listen(listen_sock, 16384))
246		err(-1, "listen");
247	if (fcntl(listen_sock, F_SETFL, O_NONBLOCK) < 0)
248		err(-1, "fcntl");
249
250	kq = kqueue();
251	if (kq < 0)
252		err(-1, "kqueue");
253
254	EV_SET(&kev, listen_sock, EVFILT_READ, EV_ADD, 0, 0, NULL);
255	if (kevent(kq, &kev, 1, NULL, 0, NULL) < 0)
256		err(-1, "kevent");
257
258	while ((numevents = kevent(kq, NULL, 0, kev_array, mflag + 1, NULL))
259	    > 0) {
260		for (i = 0; i < numevents; i++) {
261			if (kev_array[i].ident == (u_int)listen_sock)
262				(void)tcpp_server_newconn(listen_sock);
263			else
264				tcpp_server_handleconn(&kev_array[i]);
265		}
266	}
267	printf("Worker %d done\n", workernum);
268}
269
270void
271tcpp_server(void)
272{
273	long cp_time_last[CPUSTATES], cp_time_now[CPUSTATES], ticks;
274	size_t size;
275	pid_t pid;
276	int i;
277
278	pid_list = malloc(sizeof(*pid_list) * pflag);
279	if (pid_list == NULL)
280		err(-1, "malloc pid_list");
281	bzero(pid_list, sizeof(*pid_list) * pflag);
282
283	/*
284	 * Start workers.
285	 */
286	for (i = 0; i < pflag; i++) {
287		pid = fork();
288		if (pid < 0) {
289			warn("fork");
290			for (i = 0; i < pflag; i++) {
291				if (pid_list[i] != 0)
292					(void)kill(pid_list[i], SIGKILL);
293			}
294			exit(-1);
295		}
296		if (pid == 0) {
297			tcpp_server_worker(i);
298			exit(0);
299		}
300		pid_list[i] = pid;
301	}
302
303	if (Tflag) {
304		size = sizeof(cp_time_last);
305		if (sysctlbyname(SYSCTLNAME_CPTIME, &cp_time_last, &size,
306		    NULL, 0) < 0)
307			err(-1, "sysctlbyname: %s", SYSCTLNAME_CPTIME);
308		while (1) {
309			sleep(10);
310			size = sizeof(cp_time_last);
311			if (sysctlbyname(SYSCTLNAME_CPTIME, &cp_time_now,
312			    &size, NULL, 0) < 0)
313				err(-1, "sysctlbyname: %s",
314				    SYSCTLNAME_CPTIME);
315			ticks = 0;
316			for (i = 0; i < CPUSTATES; i++) {
317				cp_time_last[i] = cp_time_now[i] -
318				    cp_time_last[i];
319				ticks += cp_time_last[i];
320			}
321			printf("user%% %lu nice%% %lu sys%% %lu intr%% %lu "
322			    "idle%% %lu\n",
323			    (100 * cp_time_last[CP_USER]) / ticks,
324			    (100 * cp_time_last[CP_NICE]) / ticks,
325			    (100 * cp_time_last[CP_SYS]) / ticks,
326			    (100 * cp_time_last[CP_INTR]) / ticks,
327			    (100 * cp_time_last[CP_IDLE]) / ticks);
328			bcopy(cp_time_now, cp_time_last, sizeof(cp_time_last));
329		}
330	}
331
332	/*
333	 * GC workers.
334	 */
335	for (i = 0; i < pflag; i++) {
336		if (pid_list[i] != 0) {
337			while (waitpid(pid_list[i], NULL, 0) != pid_list[i]);
338		}
339	}
340}
341