listen_backlog.c revision 281974
1156283Srwatson/*-
2156283Srwatson * Copyright (c) 2005 Robert N. M. Watson
3156283Srwatson * All rights reserved.
4156283Srwatson *
5156283Srwatson * Redistribution and use in source and binary forms, with or without
6156283Srwatson * modification, are permitted provided that the following conditions
7156283Srwatson * are met:
8156283Srwatson * 1. Redistributions of source code must retain the above copyright
9156283Srwatson *    notice, this list of conditions and the following disclaimer.
10156283Srwatson * 2. Redistributions in binary form must reproduce the above copyright
11156283Srwatson *    notice, this list of conditions and the following disclaimer in the
12156283Srwatson *    documentation and/or other materials provided with the distribution.
13156283Srwatson *
14156283Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15156283Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16156283Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17156283Srwatson * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18156283Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19156283Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20156283Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21156283Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22156283Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23156283Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24156283Srwatson * SUCH DAMAGE.
25156283Srwatson *
26156283Srwatson * $FreeBSD: stable/10/tools/regression/sockets/listen_backlog/listen_backlog.c 281974 2015-04-25 05:31:52Z ngie $
27156283Srwatson */
28156283Srwatson
29156283Srwatson#include <sys/types.h>
30156283Srwatson#include <sys/socket.h>
31156283Srwatson#include <sys/sysctl.h>
32156283Srwatson
33156283Srwatson#include <err.h>
34156283Srwatson#include <errno.h>
35156283Srwatson#include <stdio.h>
36156283Srwatson#include <stdlib.h>
37156283Srwatson#include <unistd.h>
38156283Srwatson
39156283Srwatson/*
40156283Srwatson * This regression test is intended to validate that the backlog parameter
41156283Srwatson * set by listen() is properly set, can be retrieved using SO_LISTENQLIMIT,
42156283Srwatson * and that it can be updated by later calls to listen().  We also check that
43156283Srwatson * SO_LISTENQLIMIT cannot be set.
44156283Srwatson *
45156283Srwatson * Future things to test:
46156283Srwatson *
47156283Srwatson * - That if we change the value of kern.ipc.somaxconn, the limits really
48156283Srwatson *   do change.
49156283Srwatson *
50156283Srwatson * - That limits are, approximately, enforced and implemented.
51156283Srwatson *
52156283Srwatson * - All this on multiple socket types -- i.e., PF_LOCAL.
53156283Srwatson *
54156283Srwatson * - That we also test SO_LISTENQLEN and SO_LISTENINCQLEN.
55156283Srwatson */
56156283Srwatson
57156283Srwatson/*
58156283Srwatson * We retrieve kern.ipc.somaxconn before running the tests in order to use a
59156283Srwatson * run-time set value of SOMAXCONN, rather than compile-time set.  We assume
60156283Srwatson * that no other process will be simultaneously frobbing it, and these tests
61156283Srwatson * may fail if that assumption is not held.
62156283Srwatson */
63156283Srwatsonstatic int	somaxconn;
64156283Srwatson
65156283Srwatson/*
66156283Srwatson * Retrieve the current socket listen queue limit using SO_LISTENQLIMIT.
67156283Srwatson */
68156283Srwatsonstatic int
69156283Srwatsonsocket_get_backlog(int sock, int *backlogp, const char *testclass,
70156283Srwatson    const char *test, const char *testfunc)
71156283Srwatson{
72156283Srwatson	socklen_t len;
73156283Srwatson	int i;
74156283Srwatson
75156283Srwatson	len = sizeof(i);
76156283Srwatson	if (getsockopt(sock, SOL_SOCKET, SO_LISTENQLIMIT, &i, &len) < 0) {
77156283Srwatson		warn("%s: %s: %s: socket_get_backlog: getsockopt("
78156283Srwatson		    "SOL_SOCKET, SO_LISTENQLIMIT)", testclass, test,
79156283Srwatson		    testfunc);
80156283Srwatson		return (-1);
81156283Srwatson	}
82156283Srwatson
83156283Srwatson	if (len != sizeof(i)) {
84156283Srwatson		warnx("%s: %s: %s: socket_get_backlog: getsockopt("
85156283Srwatson		    "SOL_SOCKET, SO_LISTENQLIMIT): returned size %d",
86156283Srwatson		    testclass, test, testfunc, len);
87156283Srwatson		return (-1);
88156283Srwatson	}
89156283Srwatson
90156283Srwatson	*backlogp = i;
91156283Srwatson
92156283Srwatson	return (0);
93156283Srwatson}
94156283Srwatson
95156283Srwatson/*
96156283Srwatson * Create a socket, check the queue limit on creation, perform a listen(),
97156283Srwatson * and make sure that the limit was set as expected by listen().
98156283Srwatson */
99156283Srwatsonstatic int
100156283Srwatsonsocket_listen(int domain, int type, int protocol, int backlog,
101156283Srwatson    int create_backlog_assertion, int listen_backlog_assertion, int *sockp,
102156283Srwatson    const char *domainstring, const char *typestring, const char *testclass,
103156283Srwatson    const char *test)
104156283Srwatson{
105156283Srwatson	int backlog_retrieved, sock;
106156283Srwatson
107156283Srwatson	sock = socket(domain, type, protocol);
108156283Srwatson	if (sock < 0) {
109156283Srwatson		warn("%s: %s: socket_listen: socket(%s, %s)", testclass,
110156283Srwatson		    test, domainstring, typestring);
111156283Srwatson		close(sock);
112156283Srwatson		return (-1);
113156283Srwatson	}
114156283Srwatson
115156283Srwatson	if (socket_get_backlog(sock, &backlog_retrieved, testclass, test,
116156283Srwatson	    "socket_listen") < 0) {
117156283Srwatson		close(sock);
118156283Srwatson		return (-1);
119156283Srwatson	}
120156283Srwatson
121156283Srwatson	if (backlog_retrieved != create_backlog_assertion) {
122156283Srwatson		warnx("%s: %s: socket_listen: create backlog is %d not %d",
123156283Srwatson		    testclass, test, backlog_retrieved,
124156283Srwatson		    create_backlog_assertion);
125156283Srwatson		close(sock);
126156283Srwatson		return (-1);
127156283Srwatson	}
128156283Srwatson
129156283Srwatson	if (listen(sock, backlog) < 0) {
130156283Srwatson		warn("%s: %s: socket_listen: listen(, %d)", testclass, test,
131156283Srwatson		    backlog);
132156283Srwatson		close(sock);
133156283Srwatson		return (-1);
134156283Srwatson	}
135156283Srwatson
136156283Srwatson	if (socket_get_backlog(sock, &backlog_retrieved, testclass, test,
137156283Srwatson	    "socket_listen") < 0) {
138156283Srwatson		close(sock);
139156283Srwatson		return (-1);
140156283Srwatson	}
141156283Srwatson
142156283Srwatson	if (backlog_retrieved != listen_backlog_assertion) {
143156283Srwatson		warnx("%s: %s: socket_listen: listen backlog is %d not %d",
144156283Srwatson		    testclass, test, backlog_retrieved,
145156283Srwatson		    listen_backlog_assertion);
146156283Srwatson		close(sock);
147156283Srwatson		return (-1);
148156283Srwatson	}
149156283Srwatson
150156283Srwatson	*sockp = sock;
151156283Srwatson	return (0);
152156283Srwatson}
153156283Srwatson
154156283Srwatson/*
155156283Srwatson * This test creates sockets and tests default states before and after
156156283Srwatson * listen().  Specifically, we expect a queue limit of 0 before listen, and
157156283Srwatson * then various settings for after listen().  If the passed backlog was
158156283Srwatson * either < 0 or > somaxconn, it should be set to somaxconn; otherwise, the
159156283Srwatson * passed queue depth.
160156283Srwatson */
161156283Srwatsonstatic void
162156283Srwatsontest_defaults(void)
163156283Srwatson{
164156283Srwatson	int sock;
165156283Srwatson
166156283Srwatson	/*
167156283Srwatson	 * First pass.  Confirm the default is 0.  Listen with a backlog of
168156283Srwatson	 * 0 and confirm it gets set that way.
169156283Srwatson	 */
170156283Srwatson	if (socket_listen(PF_INET, SOCK_STREAM, 0, 0, 0, 0, &sock, "PF_INET",
171156283Srwatson	    "SOCK_STREAM", "test_defaults", "default_0_listen_0") < 0)
172156283Srwatson		exit(-1);
173156283Srwatson	close(sock);
174156283Srwatson
175156283Srwatson	/*
176156283Srwatson	 * Second pass.  Listen with a backlog of -1 and make sure it is set
177156283Srwatson	 * to somaxconn.
178156283Srwatson	 */
179156283Srwatson	if (socket_listen(PF_INET, SOCK_STREAM, 0, -1, 0, somaxconn, &sock,
180156283Srwatson	    "PF_INET", "SOCK_STREAM", "test_defaults", "default_0_listen_-1")
181156283Srwatson	    < 0)
182156283Srwatson		exit(-1);
183156283Srwatson	close(sock);
184156283Srwatson
185156283Srwatson	/*
186156283Srwatson	 * Third pass.  Listen with a backlog of 1 and make sure it is set to
187156283Srwatson	 * 1.
188156283Srwatson	 */
189156283Srwatson	if (socket_listen(PF_INET, SOCK_STREAM, 0, 1, 0, 1, &sock, "PF_INET",
190156283Srwatson	    "SOCK_STREAM", "test_defaults", "default_0_listen_1") < 0)
191156283Srwatson		exit(-1);
192156283Srwatson	close(sock);
193156283Srwatson
194156283Srwatson	/*
195156283Srwatson	 * Fourth pass.  Listen with a backlog of somaxconn and make sure it
196156283Srwatson	 * is set to somaxconn.
197156283Srwatson	 */
198156283Srwatson	if (socket_listen(PF_INET, SOCK_STREAM, 0, somaxconn, 0, somaxconn,
199156283Srwatson	    &sock, "PF_INET", "SOCK_STREAM", "test_defaults",
200156283Srwatson	    "default_0_listen_somaxconn") < 0)
201156283Srwatson		exit(-1);
202156283Srwatson	close(sock);
203156283Srwatson
204156283Srwatson	/*
205156283Srwatson	 * Fifth pass.  Listen with a backlog of somaxconn+1 and make sure it
206156283Srwatson	 * is set to somaxconn.
207156283Srwatson	 */
208156283Srwatson	if (socket_listen(PF_INET, SOCK_STREAM, 0, somaxconn+1, 0, somaxconn,
209156283Srwatson	    &sock, "PF_INET", "SOCK_STREAM", "test_defaults",
210156283Srwatson	    "default_0_listen_somaxconn+1") < 0)
211156283Srwatson		exit(-1);
212156283Srwatson	close(sock);
213156283Srwatson}
214156283Srwatson
215156283Srwatson/*
216156283Srwatson * Create a socket, set the initial listen() state, then update the queue
217156283Srwatson * depth using listen().  Check that the backlog is as expected after both
218156283Srwatson * the first and second listen().
219156283Srwatson */
220156283Srwatsonstatic int
221156283Srwatsonsocket_listen_update(int domain __unused, int type __unused,
222156283Srwatson    int protocol __unused, int backlog,
223156283Srwatson    int update_backlog, int listen_backlog_assertion,
224156283Srwatson    int update_backlog_assertion, int *sockp, const char *domainstring,
225156283Srwatson    const char *typestring, const char *testclass, const char *test)
226156283Srwatson{
227156283Srwatson	int backlog_retrieved, sock;
228156283Srwatson
229156283Srwatson	sock = socket(PF_INET, SOCK_STREAM, 0);
230156283Srwatson	if (sock < 0) {
231156283Srwatson		warn("%s: %s: socket_listen_update: socket(%s, %s)",
232156283Srwatson		    testclass, test, domainstring, typestring);
233156283Srwatson		return (-1);
234156283Srwatson	}
235156283Srwatson
236156283Srwatson	if (listen(sock, backlog) < 0) {
237156283Srwatson		warn("%s: %s: socket_listen_update: initial listen(, %d)",
238156283Srwatson		    testclass, test, backlog);
239156283Srwatson		close(sock);
240156283Srwatson		return (-1);
241156283Srwatson	}
242156283Srwatson
243156283Srwatson	if (socket_get_backlog(sock, &backlog_retrieved, testclass, test,
244156283Srwatson	    "socket_listen_update") < 0) {
245156283Srwatson		close(sock);
246156283Srwatson		return (-1);
247156283Srwatson	}
248156283Srwatson
249156283Srwatson	if (backlog_retrieved != listen_backlog_assertion) {
250156283Srwatson		warnx("%s: %s: socket_listen_update: initial backlog is %d "
251156283Srwatson		    "not %d", testclass, test, backlog_retrieved,
252156283Srwatson		    listen_backlog_assertion);
253156283Srwatson		close(sock);
254156283Srwatson		return (-1);
255156283Srwatson	}
256156283Srwatson
257156283Srwatson	if (listen(sock, update_backlog) < 0) {
258156283Srwatson		warn("%s: %s: socket_listen_update: update listen(, %d)",
259156283Srwatson		    testclass, test, update_backlog);
260156283Srwatson		close(sock);
261156283Srwatson		return (-1);
262156283Srwatson	}
263156283Srwatson
264156283Srwatson	if (socket_get_backlog(sock, &backlog_retrieved, testclass, test,
265156283Srwatson	    "socket_listen_update") < 0) {
266156283Srwatson		close(sock);
267156283Srwatson		return (-1);
268156283Srwatson	}
269156283Srwatson
270156283Srwatson	if (backlog_retrieved != update_backlog_assertion) {
271156283Srwatson		warnx("%s: %s: socket_listen_update: updated backlog is %d "
272156283Srwatson		    "not %d", testclass, test, backlog_retrieved,
273156283Srwatson		    update_backlog_assertion);
274156283Srwatson		close(sock);
275156283Srwatson		return (-1);
276156283Srwatson	}
277156283Srwatson
278156283Srwatson	*sockp = sock;
279156283Srwatson	return (0);
280156283Srwatson}
281156283Srwatson
282156283Srwatson/*
283156283Srwatson * This test tests using listen() to update the queue depth after a socket
284156283Srwatson * has already been marked as listening.  We test several cases: setting the
285156283Srwatson * socket < 0, 0, 1, somaxconn, and somaxconn + 1.
286156283Srwatson */
287156283Srwatsonstatic void
288156283Srwatsontest_listen_update(void)
289156283Srwatson{
290156283Srwatson	int sock;
291156283Srwatson
292156283Srwatson	/*
293156283Srwatson	 * Set to 5, update to -1, which should give somaxconn.
294156283Srwatson	 */
295156283Srwatson	if (socket_listen_update(PF_INET, SOCK_STREAM, 0, 5, -1, 5, somaxconn,
296156283Srwatson	    &sock, "PF_INET", "SOCK_STREAM", "test_listen_update",
297156283Srwatson	    "update_5,-1") < 0)
298156283Srwatson		exit(-1);
299156283Srwatson	close(sock);
300156283Srwatson
301156283Srwatson	/*
302156283Srwatson	 * Set to 5, update to 0, which should give 0.
303156283Srwatson	 */
304156283Srwatson	if (socket_listen_update(PF_INET, SOCK_STREAM, 0, 5, 0, 5, 0, &sock,
305156283Srwatson	    "PF_INET", "SOCK_STREAM", "test_listen_update", "update_5,0")
306156283Srwatson	    < 0)
307156283Srwatson		exit(-1);
308156283Srwatson	close(sock);
309156283Srwatson
310156283Srwatson	/*
311156283Srwatson	 * Set to 5, update to 1, which should give 1.
312156283Srwatson	 */
313156283Srwatson	if (socket_listen_update(PF_INET, SOCK_STREAM, 0, 5, 1, 5, 1, &sock,
314156283Srwatson	    "PF_INET", "SOCK_STREAM", "test_listen_update", "update_5,1")
315156283Srwatson	    < 0)
316156283Srwatson		exit(-1);
317156283Srwatson	close(sock);
318156283Srwatson
319156283Srwatson	/*
320156283Srwatson	 * Set to 5, update to somaxconn, which should give somaxconn.
321156283Srwatson	 */
322156283Srwatson	if (socket_listen_update(PF_INET, SOCK_STREAM, 0, 5, somaxconn, 5,
323156283Srwatson	    somaxconn, &sock, "PF_INET", "SOCK_STREAM", "test_listen_update",
324156283Srwatson	    "update_5,somaxconn") < 0)
325156283Srwatson		exit(-1);
326156283Srwatson	close(sock);
327156283Srwatson
328156283Srwatson	/*
329156283Srwatson	 * Set to 5, update to somaxconn+1, which should give somaxconn.
330156283Srwatson	 */
331156283Srwatson	if (socket_listen_update(PF_INET, SOCK_STREAM, 0, 5, somaxconn+1, 5,
332156283Srwatson	    somaxconn, &sock, "PF_INET", "SOCK_STREAM", "test_listen_update",
333156283Srwatson	    "update_5,somaxconn+1") < 0)
334156283Srwatson		exit(-1);
335156283Srwatson	close(sock);
336156283Srwatson}
337156283Srwatson
338156283Srwatson/*
339156283Srwatson * SO_LISTENQLIMIT is a read-only socket option, so make sure we get an error
340156283Srwatson * if we try to write it.
341156283Srwatson */
342156283Srwatsonstatic void
343156283Srwatsontest_set_qlimit(void)
344156283Srwatson{
345156283Srwatson	int i, ret, sock;
346156283Srwatson
347156283Srwatson	sock = socket(PF_INET, SOCK_STREAM, 0);
348156283Srwatson	if (sock < 0)
349156283Srwatson		err(-1, "test_set_qlimit: socket(PF_INET, SOCK_STREAM)");
350156283Srwatson
351156283Srwatson	i = 0;
352156283Srwatson	ret = setsockopt(sock, SOL_SOCKET, SO_LISTENQLIMIT, &i, sizeof(i));
353156283Srwatson	if (ret < 0 && errno != ENOPROTOOPT) {
354156283Srwatson		warn("test_set_qlimit: setsockopt(SOL_SOCKET, "
355156283Srwatson		    "SO_LISTENQLIMIT, 0): unexpected error");
356156283Srwatson		close(sock);
357156283Srwatson	}
358156283Srwatson
359156283Srwatson	if (ret == 0) {
360156283Srwatson		warnx("test_set_qlimit: setsockopt(SOL_SOCKET, "
361156283Srwatson		    "SO_LISTENQLIMIT, 0) succeeded");
362156283Srwatson		close(sock);
363156283Srwatson		exit(-1);
364156283Srwatson	}
365156283Srwatson	close(sock);
366156283Srwatson}
367156283Srwatson
368156283Srwatsonint
369156283Srwatsonmain(void)
370156283Srwatson{
371156283Srwatson	size_t len;
372156283Srwatson
373156283Srwatson	len = sizeof(somaxconn);
374156283Srwatson	if (sysctlbyname("kern.ipc.somaxconn", &somaxconn, &len, NULL, 0)
375156283Srwatson	    < 0)
376156283Srwatson		err(-1, "sysctlbyname(kern.ipc.somaxconn)");
377156283Srwatson
378156283Srwatson	test_defaults();
379156283Srwatson	test_listen_update();
380156283Srwatson	test_set_qlimit();
381156283Srwatson
382156283Srwatson	return (0);
383156283Srwatson}
384156283Srwatson