summaryrefslogtreecommitdiffstats
path: root/tools/tools/netrate/tcpp/tcpp_server.c
blob: 0a79fbba5e3024625047c2904aa79249caeb6008 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*-
 * Copyright (c) 2008-2009 Robert N. M. Watson
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $FreeBSD$
 */

#include <sys/types.h>
#include <sys/endian.h>
#include <sys/event.h>
#include <sys/resource.h>
#include <sys/sched.h>
#include <sys/socket.h>
#include <sys/sysctl.h>
#include <sys/time.h>
#include <sys/wait.h>

#include <netinet/in.h>
#include <netinet/tcp.h>

#include <err.h>
#include <fcntl.h>
#include <inttypes.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "tcpp.h"

/*
 * Server side -- create a pool of processes, each listening on its own TCP
 * port number for new connections.  The first 8 bytes of each connection
 * will be a network byte order length, then there will be that number of
 * bytes of data.  We use non-blocking sockets with kqueue to to avoid the
 * overhead of threading or more than one process per processor, which makes
 * things a bit awkward when dealing with data we care about.  As such, we
 * read into a small character buffer which we then convert to a length once
 * we have all the data.
 */
#define	CONNECTION_MAGIC	0x6392af27
struct connection {
	uint32_t	conn_magic;		/* Just magic. */
	int		conn_fd;
	struct tcpp_header	conn_header;	/* Header buffer. */
	u_int		conn_header_len;	/* Bytes so far. */
	u_int64_t	conn_data_len;		/* How much to sink. */
	u_int64_t	conn_data_received;	/* How much so far. */
};

static pid_t			*pid_list;
static int			 kq;

static struct connection *
tcpp_server_newconn(int listen_fd)
{
	struct connection *conn;
	struct kevent kev;
	int fd;

	fd = accept(listen_fd, NULL, NULL);
	if (fd < 0) {
		warn("accept");
		return (NULL);
	}

	if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
		err(-1, "fcntl");

	conn = malloc(sizeof(*conn));
	if (conn == NULL)
		return (NULL);
	bzero(conn, sizeof(*conn));
	conn->conn_magic = CONNECTION_MAGIC;
	conn->conn_fd = fd;

	/*
	 * Register to read on the socket, and set our conn pointer as the
	 * udata so we can find it quickly in the future.
	 */
	EV_SET(&kev, fd, EVFILT_READ, EV_ADD, 0, 0, conn);
	if (kevent(kq, &kev, 1, NULL, 0, NULL) < 0)
		err(-1, "kevent");

	return (conn);
}

static void
tcpp_server_closeconn(struct connection *conn)
{

	/*
	 * Kqueue cleans up after itself once we close the socket, and since
	 * we are processing only one kevent at a time, we don't need to
	 * worry about watching out for future kevents referring to it.
	 *
	 * ... right?
	 */
	close(conn->conn_fd);
	bzero(conn, sizeof(*conn));
	free(conn);
}

static u_char buffer[256*1024];	/* Buffer in which to sink data. */
static void
tcpp_server_handleconn(struct kevent *kev)
{
	struct connection *conn;
	ssize_t len;

	conn = kev->udata;
	if (conn->conn_magic != CONNECTION_MAGIC)
		errx(-1, "tcpp_server_handleconn: magic");

	if (conn->conn_header_len < sizeof(conn->conn_header)) {
		len = read(conn->conn_fd,
		    ((u_char *)&conn->conn_header) + conn->conn_header_len,
		    sizeof(conn->conn_header) - conn->conn_header_len);
		if (len < 0) {
			warn("tcpp_server_handleconn: header read");
			tcpp_server_closeconn(conn);
			return;
		}
		if (len == 0) {
			warnx("tcpp_server_handleconn: header premature eof");
			tcpp_server_closeconn(conn);
			return;
		}
		conn->conn_header_len += len;
		if (conn->conn_header_len == sizeof(conn->conn_header)) {
			tcpp_header_decode(&conn->conn_header);
			if (conn->conn_header.th_magic != TCPP_MAGIC) {
				warnx("tcpp_server_handleconn: bad magic");
				tcpp_server_closeconn(conn);
				return;
			}
		}
	} else {
		/*
		 * Drain up to a buffer from the connection, so that we pay
		 * attention to other connections too.
		 */
		len = read(conn->conn_fd, buffer, sizeof(buffer));
		if (len < 0) {
			warn("tcpp_server_handleconn: data bad read");
			tcpp_server_closeconn(conn);
			return;
		}
		if (len == 0 && conn->conn_data_received <
		    conn->conn_header.th_len) {
			warnx("tcpp_server_handleconn: data premature eof");
			tcpp_server_closeconn(conn);
			return;
		}
		conn->conn_data_received += len;
		if (conn->conn_data_received > conn->conn_header.th_len) {
			warnx("tcpp_server_handleconn: too much data");
			tcpp_server_closeconn(conn);
			return;
		}
		if (conn->conn_data_received == conn->conn_header.th_len) {
			/*
			 * All is well.
			 */
			tcpp_server_closeconn(conn);
			return;
		}
	}
}

static void
tcpp_server_worker(int workernum)
{
	int i, listen_sock, numevents;
	struct kevent kev, *kev_array;
	int kev_bytes;
#if defined(CPU_SETSIZE) && 0
	cpu_set_t mask;
	int ncpus;
	ssize_t len;

	if (Pflag) {
		len = sizeof(ncpus);
		if (sysctlbyname(SYSCTLNAME_CPUS, &ncpus, &len, NULL, 0) < 0)
			err(-1, "sysctlbyname: %s", SYSCTLNAME_CPUS);
		if (len != sizeof(ncpus))
			errx(-1, "sysctlbyname: %s: len %jd", SYSCTLNAME_CPUS,
			    (intmax_t)len);

		CPU_ZERO(&mask);
		CPU_SET(workernum % ncpus, &mask);
		if (sched_setaffinity(0, CPU_SETSIZE, &mask) < 0)
			err(-1, "sched_setaffinity");
	}
#endif
	setproctitle("tcpp_server %d", workernum);

	/* Allow an extra kevent for the listen socket. */
	kev_bytes = sizeof(*kev_array) * (mflag + 1);
	kev_array = malloc(kev_bytes);
	if (kev_array == NULL)
		err(-1, "malloc");
	bzero(kev_array, kev_bytes);

	/* XXXRW: Want to set and pin the CPU here. */

	/*
	 * Add the worker number to the local port.
	 */
	localipbase.sin_port = htons(rflag + workernum);

	listen_sock = socket(PF_INET, SOCK_STREAM, 0);
	if (listen_sock < 0)
		err(-1, "socket");
	i = 1;
	if (setsockopt(listen_sock, SOL_SOCKET, SO_NOSIGPIPE, &i, sizeof(i))
	    < 0)
		err(-1, "setsockopt");
	i = 1;
	if (setsockopt(listen_sock, SOL_SOCKET, SO_REUSEPORT, &i, sizeof(i))
	    < 0)
		err(-1, "setsockopt");
	i = 1;
	if (setsockopt(listen_sock, IPPROTO_TCP, TCP_NODELAY, &i, sizeof(i))
	    < 0)
		err(-1, "setsockopt");
	if (bind(listen_sock, (struct sockaddr *)&localipbase,
	    sizeof(localipbase)) < 0)
		err(-1, "bind");
	if (listen(listen_sock, 16384))
		err(-1, "listen");
	if (fcntl(listen_sock, F_SETFL, O_NONBLOCK) < 0)
		err(-1, "fcntl");

	kq = kqueue();
	if (kq < 0)
		err(-1, "kqueue");

	EV_SET(&kev, listen_sock, EVFILT_READ, EV_ADD, 0, 0, NULL);
	if (kevent(kq, &kev, 1, NULL, 0, NULL) < 0)
		err(-1, "kevent");

	while ((numevents = kevent(kq, NULL, 0, kev_array, mflag + 1, NULL))
	    > 0) {
		for (i = 0; i < numevents; i++) {
			if (kev_array[i].ident == (u_int)listen_sock)
				(void)tcpp_server_newconn(listen_sock);
			else
				tcpp_server_handleconn(&kev_array[i]);
		}
	}
	printf("Worker %d done\n", workernum);
}

void
tcpp_server(void)
{
#if 0
	long cp_time_last[CPUSTATES], cp_time_now[CPUSTATES], ticks;
	size_t size;
#endif
	pid_t pid;
	int i;

	pid_list = malloc(sizeof(*pid_list) * pflag);
	if (pid_list == NULL)
		err(-1, "malloc pid_list");
	bzero(pid_list, sizeof(*pid_list) * pflag);

	/*
	 * Start workers.
	 */
	for (i = 0; i < pflag; i++) {
		pid = fork();
		if (pid < 0) {
			warn("fork");
			for (i = 0; i < pflag; i++) {
				if (pid_list[i] != 0)
					(void)kill(pid_list[i], SIGKILL);
			}
			exit(-1);
		}
		if (pid == 0) {
			tcpp_server_worker(i);
			exit(0);
		}
		pid_list[i] = pid;
	}

#if 0
		size = sizeof(cp_time_last);
		if (sysctlbyname(SYSCTLNAME_CPTIME, &cp_time_last, &size,
		    NULL, 0) < 0)
			err(-1, "sysctlbyname: %s", SYSCTLNAME_CPTIME);
		while (1) {
			sleep(10);
			size = sizeof(cp_time_last);
			if (sysctlbyname(SYSCTLNAME_CPTIME, &cp_time_now,
			    &size, NULL, 0) < 0)
				err(-1, "sysctlbyname: %s",
				    SYSCTLNAME_CPTIME);
			ticks = 0;
			for (i = 0; i < CPUSTATES; i++) {
				cp_time_last[i] = cp_time_now[i] -
				    cp_time_last[i];
				ticks += cp_time_last[i];
			}
			printf("user%% %lu nice%% %lu sys%% %lu intr%% %lu "
			    "idle%% %lu\n",
			    (100 * cp_time_last[CP_USER]) / ticks,
			    (100 * cp_time_last[CP_NICE]) / ticks,
			    (100 * cp_time_last[CP_SYS]) / ticks,
			    (100 * cp_time_last[CP_INTR]) / ticks,
			    (100 * cp_time_last[CP_IDLE]) / ticks);
			bcopy(cp_time_now, cp_time_last, sizeof(cp_time_last));
		}
#endif

	/*
	 * GC workers.
	 */
	for (i = 0; i < pflag; i++) {
		if (pid_list[i] != 0) {
			while (waitpid(pid_list[i], NULL, 0) != pid_list[i]);
		}
	}
}
OpenPOWER on IntegriCloud