summaryrefslogtreecommitdiffstats
path: root/tools/regression/sockets/listen_backlog/listen_backlog.c
blob: 2276393ee6f1bd32250da82d8e44ca23e8f3d1c9 (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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*-
 * Copyright (c) 2005 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/socket.h>
#include <sys/sysctl.h>

#include <err.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

/*
 * This regression test is intended to validate that the backlog parameter
 * set by listen() is properly set, can be retrieved using SO_LISTENQLIMIT,
 * and that it can be updated by later calls to listen().  We also check that
 * SO_LISTENQLIMIT cannot be set.
 *
 * Future things to test:
 *
 * - That if we change the value of kern.ipc.somaxconn, the limits really
 *   do change.
 *
 * - That limits are, approximately, enforced and implemented.
 *
 * - All this on multiple socket types -- i.e., PF_LOCAL.
 *
 * - That we also test SO_LISTENQLEN and SO_LISTENINCQLEN.
 */

/*
 * We retrieve kern.ipc.somaxconn before running the tests in order to use a
 * run-time set value of SOMAXCONN, rather than compile-time set.  We assume
 * that no other process will be simultaneously frobbing it, and these tests
 * may fail if that assumption is not held.
 */
static int	somaxconn;

/*
 * Retrieve the current socket listen queue limit using SO_LISTENQLIMIT.
 */
static int
socket_get_backlog(int sock, int *backlogp, const char *testclass,
    const char *test, const char *testfunc)
{
	socklen_t len;
	int i;

	len = sizeof(i);
	if (getsockopt(sock, SOL_SOCKET, SO_LISTENQLIMIT, &i, &len) < 0) {
		warn("%s: %s: %s: socket_get_backlog: getsockopt("
		    "SOL_SOCKET, SO_LISTENQLIMIT)", testclass, test,
		    testfunc);
		return (-1);
	}

	if (len != sizeof(i)) {
		warnx("%s: %s: %s: socket_get_backlog: getsockopt("
		    "SOL_SOCKET, SO_LISTENQLIMIT): returned size %d",
		    testclass, test, testfunc, len);
		return (-1);
	}

	*backlogp = i;

	return (0);
}

/*
 * Create a socket, check the queue limit on creation, perform a listen(),
 * and make sure that the limit was set as expected by listen().
 */
static int
socket_listen(int domain, int type, int protocol, int backlog,
    int create_backlog_assertion, int listen_backlog_assertion, int *sockp,
    const char *domainstring, const char *typestring, const char *testclass,
    const char *test)
{
	int backlog_retrieved, sock;

	sock = socket(domain, type, protocol);
	if (sock < 0) {
		warn("%s: %s: socket_listen: socket(%s, %s)", testclass,
		    test, domainstring, typestring);
		close(sock);
		return (-1);
	}

	if (socket_get_backlog(sock, &backlog_retrieved, testclass, test,
	    "socket_listen") < 0) {
		close(sock);
		return (-1);
	}

	if (backlog_retrieved != create_backlog_assertion) {
		warnx("%s: %s: socket_listen: create backlog is %d not %d",
		    testclass, test, backlog_retrieved,
		    create_backlog_assertion);
		close(sock);
		return (-1);
	}

	if (listen(sock, backlog) < 0) {
		warn("%s: %s: socket_listen: listen(, %d)", testclass, test,
		    backlog);
		close(sock);
		return (-1);
	}

	if (socket_get_backlog(sock, &backlog_retrieved, testclass, test,
	    "socket_listen") < 0) {
		close(sock);
		return (-1);
	}

	if (backlog_retrieved != listen_backlog_assertion) {
		warnx("%s: %s: socket_listen: listen backlog is %d not %d",
		    testclass, test, backlog_retrieved,
		    listen_backlog_assertion);
		close(sock);
		return (-1);
	}

	*sockp = sock;
	return (0);
}

/*
 * This test creates sockets and tests default states before and after
 * listen().  Specifically, we expect a queue limit of 0 before listen, and
 * then various settings for after listen().  If the passed backlog was
 * either < 0 or > somaxconn, it should be set to somaxconn; otherwise, the
 * passed queue depth.
 */
static void
test_defaults(void)
{
	int sock;

	/*
	 * First pass.  Confirm the default is 0.  Listen with a backlog of
	 * 0 and confirm it gets set that way.
	 */
	if (socket_listen(PF_INET, SOCK_STREAM, 0, 0, 0, 0, &sock, "PF_INET",
	    "SOCK_STREAM", "test_defaults", "default_0_listen_0") < 0)
		exit(-1);
	close(sock);

	/*
	 * Second pass.  Listen with a backlog of -1 and make sure it is set
	 * to somaxconn.
	 */
	if (socket_listen(PF_INET, SOCK_STREAM, 0, -1, 0, somaxconn, &sock,
	    "PF_INET", "SOCK_STREAM", "test_defaults", "default_0_listen_-1")
	    < 0)
		exit(-1);
	close(sock);

	/*
	 * Third pass.  Listen with a backlog of 1 and make sure it is set to
	 * 1.
	 */
	if (socket_listen(PF_INET, SOCK_STREAM, 0, 1, 0, 1, &sock, "PF_INET",
	    "SOCK_STREAM", "test_defaults", "default_0_listen_1") < 0)
		exit(-1);
	close(sock);

	/*
	 * Fourth pass.  Listen with a backlog of somaxconn and make sure it
	 * is set to somaxconn.
	 */
	if (socket_listen(PF_INET, SOCK_STREAM, 0, somaxconn, 0, somaxconn,
	    &sock, "PF_INET", "SOCK_STREAM", "test_defaults",
	    "default_0_listen_somaxconn") < 0)
		exit(-1);
	close(sock);

	/*
	 * Fifth pass.  Listen with a backlog of somaxconn+1 and make sure it
	 * is set to somaxconn.
	 */
	if (socket_listen(PF_INET, SOCK_STREAM, 0, somaxconn+1, 0, somaxconn,
	    &sock, "PF_INET", "SOCK_STREAM", "test_defaults",
	    "default_0_listen_somaxconn+1") < 0)
		exit(-1);
	close(sock);
}

/*
 * Create a socket, set the initial listen() state, then update the queue
 * depth using listen().  Check that the backlog is as expected after both
 * the first and second listen().
 */
static int
socket_listen_update(int domain __unused, int type __unused,
    int protocol __unused, int backlog,
    int update_backlog, int listen_backlog_assertion,
    int update_backlog_assertion, int *sockp, const char *domainstring,
    const char *typestring, const char *testclass, const char *test)
{
	int backlog_retrieved, sock;

	sock = socket(PF_INET, SOCK_STREAM, 0);
	if (sock < 0) {
		warn("%s: %s: socket_listen_update: socket(%s, %s)",
		    testclass, test, domainstring, typestring);
		return (-1);
	}

	if (listen(sock, backlog) < 0) {
		warn("%s: %s: socket_listen_update: initial listen(, %d)",
		    testclass, test, backlog);
		close(sock);
		return (-1);
	}

	if (socket_get_backlog(sock, &backlog_retrieved, testclass, test,
	    "socket_listen_update") < 0) {
		close(sock);
		return (-1);
	}

	if (backlog_retrieved != listen_backlog_assertion) {
		warnx("%s: %s: socket_listen_update: initial backlog is %d "
		    "not %d", testclass, test, backlog_retrieved,
		    listen_backlog_assertion);
		close(sock);
		return (-1);
	}

	if (listen(sock, update_backlog) < 0) {
		warn("%s: %s: socket_listen_update: update listen(, %d)",
		    testclass, test, update_backlog);
		close(sock);
		return (-1);
	}

	if (socket_get_backlog(sock, &backlog_retrieved, testclass, test,
	    "socket_listen_update") < 0) {
		close(sock);
		return (-1);
	}

	if (backlog_retrieved != update_backlog_assertion) {
		warnx("%s: %s: socket_listen_update: updated backlog is %d "
		    "not %d", testclass, test, backlog_retrieved,
		    update_backlog_assertion);
		close(sock);
		return (-1);
	}

	*sockp = sock;
	return (0);
}

/*
 * This test tests using listen() to update the queue depth after a socket
 * has already been marked as listening.  We test several cases: setting the
 * socket < 0, 0, 1, somaxconn, and somaxconn + 1.
 */
static void
test_listen_update(void)
{
	int sock;

	/*
	 * Set to 5, update to -1, which should give somaxconn.
	 */
	if (socket_listen_update(PF_INET, SOCK_STREAM, 0, 5, -1, 5, somaxconn,
	    &sock, "PF_INET", "SOCK_STREAM", "test_listen_update",
	    "update_5,-1") < 0)
		exit(-1);
	close(sock);

	/*
	 * Set to 5, update to 0, which should give 0.
	 */
	if (socket_listen_update(PF_INET, SOCK_STREAM, 0, 5, 0, 5, 0, &sock,
	    "PF_INET", "SOCK_STREAM", "test_listen_update", "update_5,0")
	    < 0)
		exit(-1);
	close(sock);

	/*
	 * Set to 5, update to 1, which should give 1.
	 */
	if (socket_listen_update(PF_INET, SOCK_STREAM, 0, 5, 1, 5, 1, &sock,
	    "PF_INET", "SOCK_STREAM", "test_listen_update", "update_5,1")
	    < 0)
		exit(-1);
	close(sock);

	/*
	 * Set to 5, update to somaxconn, which should give somaxconn.
	 */
	if (socket_listen_update(PF_INET, SOCK_STREAM, 0, 5, somaxconn, 5,
	    somaxconn, &sock, "PF_INET", "SOCK_STREAM", "test_listen_update",
	    "update_5,somaxconn") < 0)
		exit(-1);
	close(sock);

	/*
	 * Set to 5, update to somaxconn+1, which should give somaxconn.
	 */
	if (socket_listen_update(PF_INET, SOCK_STREAM, 0, 5, somaxconn+1, 5,
	    somaxconn, &sock, "PF_INET", "SOCK_STREAM", "test_listen_update",
	    "update_5,somaxconn+1") < 0)
		exit(-1);
	close(sock);
}

/*
 * SO_LISTENQLIMIT is a read-only socket option, so make sure we get an error
 * if we try to write it.
 */
static void
test_set_qlimit(void)
{
	int i, ret, sock;

	sock = socket(PF_INET, SOCK_STREAM, 0);
	if (sock < 0)
		err(-1, "test_set_qlimit: socket(PF_INET, SOCK_STREAM)");

	i = 0;
	ret = setsockopt(sock, SOL_SOCKET, SO_LISTENQLIMIT, &i, sizeof(i));
	if (ret < 0 && errno != ENOPROTOOPT) {
		warn("test_set_qlimit: setsockopt(SOL_SOCKET, "
		    "SO_LISTENQLIMIT, 0): unexpected error");
		close(sock);
	}

	if (ret == 0) {
		warnx("test_set_qlimit: setsockopt(SOL_SOCKET, "
		    "SO_LISTENQLIMIT, 0) succeeded");
		close(sock);
		exit(-1);
	}
	close(sock);
}

int
main(void)
{
	size_t len;

	len = sizeof(somaxconn);
	if (sysctlbyname("kern.ipc.somaxconn", &somaxconn, &len, NULL, 0)
	    < 0)
		err(-1, "sysctlbyname(kern.ipc.somaxconn)");

	test_defaults();
	test_listen_update();
	test_set_qlimit();

	return (0);
}
OpenPOWER on IntegriCloud