2 * Copyright (c) 2005 Robert N. M. Watson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/types.h>
30 #include <sys/socket.h>
31 #include <sys/sysctl.h>
40 * This regression test is intended to validate that the backlog parameter
41 * set by listen() is properly set, can be retrieved using SO_LISTENQLIMIT,
42 * and that it can be updated by later calls to listen(). We also check that
43 * SO_LISTENQLIMIT cannot be set.
45 * Future things to test:
47 * - That if we change the value of kern.ipc.somaxconn, the limits really
50 * - That limits are, approximately, enforced and implemented.
52 * - All this on multiple socket types -- i.e., PF_LOCAL.
54 * - That we also test SO_LISTENQLEN and SO_LISTENINCQLEN.
58 * We retrieve kern.ipc.somaxconn before running the tests in order to use a
59 * run-time set value of SOMAXCONN, rather than compile-time set. We assume
60 * that no other process will be simultaneously frobbing it, and these tests
61 * may fail if that assumption is not held.
66 * Retrieve the current socket listen queue limit using SO_LISTENQLIMIT.
69 socket_get_backlog(int sock, int *backlogp, const char *testclass,
70 const char *test, const char *testfunc)
76 if (getsockopt(sock, SOL_SOCKET, SO_LISTENQLIMIT, &i, &len) < 0) {
77 warn("%s: %s: %s: socket_get_backlog: getsockopt("
78 "SOL_SOCKET, SO_LISTENQLIMIT)", testclass, test,
83 if (len != sizeof(i)) {
84 warnx("%s: %s: %s: socket_get_backlog: getsockopt("
85 "SOL_SOCKET, SO_LISTENQLIMIT): returned size %d",
86 testclass, test, testfunc, len);
96 * Create a socket, check the queue limit on creation, perform a listen(),
97 * and make sure that the limit was set as expected by listen().
100 socket_listen(int domain, int type, int protocol, int backlog,
101 int create_backlog_assertion, int listen_backlog_assertion, int *sockp,
102 const char *domainstring, const char *typestring, const char *testclass,
105 int backlog_retrieved, sock;
107 sock = socket(domain, type, protocol);
109 warn("%s: %s: socket_listen: socket(%s, %s)", testclass,
110 test, domainstring, typestring);
115 if (socket_get_backlog(sock, &backlog_retrieved, testclass, test,
116 "socket_listen") < 0) {
121 if (backlog_retrieved != create_backlog_assertion) {
122 warnx("%s: %s: socket_listen: create backlog is %d not %d",
123 testclass, test, backlog_retrieved,
124 create_backlog_assertion);
129 if (listen(sock, backlog) < 0) {
130 warn("%s: %s: socket_listen: listen(, %d)", testclass, test,
136 if (socket_get_backlog(sock, &backlog_retrieved, testclass, test,
137 "socket_listen") < 0) {
142 if (backlog_retrieved != listen_backlog_assertion) {
143 warnx("%s: %s: socket_listen: listen backlog is %d not %d",
144 testclass, test, backlog_retrieved,
145 listen_backlog_assertion);
155 * This test creates sockets and tests default states before and after
156 * listen(). Specifically, we expect a queue limit of 0 before listen, and
157 * then various settings for after listen(). If the passed backlog was
158 * either < 0 or > somaxconn, it should be set to somaxconn; otherwise, the
159 * passed queue depth.
167 * First pass. Confirm the default is 0. Listen with a backlog of
168 * 0 and confirm it gets set that way.
170 if (socket_listen(PF_INET, SOCK_STREAM, 0, 0, 0, 0, &sock, "PF_INET",
171 "SOCK_STREAM", "test_defaults", "default_0_listen_0") < 0)
176 * Second pass. Listen with a backlog of -1 and make sure it is set
179 if (socket_listen(PF_INET, SOCK_STREAM, 0, -1, 0, somaxconn, &sock,
180 "PF_INET", "SOCK_STREAM", "test_defaults", "default_0_listen_-1")
186 * Third pass. Listen with a backlog of 1 and make sure it is set to
189 if (socket_listen(PF_INET, SOCK_STREAM, 0, 1, 0, 1, &sock, "PF_INET",
190 "SOCK_STREAM", "test_defaults", "default_0_listen_1") < 0)
195 * Fourth pass. Listen with a backlog of somaxconn and make sure it
196 * is set to somaxconn.
198 if (socket_listen(PF_INET, SOCK_STREAM, 0, somaxconn, 0, somaxconn,
199 &sock, "PF_INET", "SOCK_STREAM", "test_defaults",
200 "default_0_listen_somaxconn") < 0)
205 * Fifth pass. Listen with a backlog of somaxconn+1 and make sure it
206 * is set to somaxconn.
208 if (socket_listen(PF_INET, SOCK_STREAM, 0, somaxconn+1, 0, somaxconn,
209 &sock, "PF_INET", "SOCK_STREAM", "test_defaults",
210 "default_0_listen_somaxconn+1") < 0)
216 * Create a socket, set the initial listen() state, then update the queue
217 * depth using listen(). Check that the backlog is as expected after both
218 * the first and second listen().
221 socket_listen_update(int domain, int type, int protocol, int backlog,
222 int update_backlog, int listen_backlog_assertion,
223 int update_backlog_assertion, int *sockp, const char *domainstring,
224 const char *typestring, const char *testclass, const char *test)
226 int backlog_retrieved, sock;
228 sock = socket(PF_INET, SOCK_STREAM, 0);
230 warn("%s: %s: socket_listen_update: socket(%s, %s)",
231 testclass, test, domainstring, typestring);
235 if (listen(sock, backlog) < 0) {
236 warn("%s: %s: socket_listen_update: initial listen(, %d)",
237 testclass, test, backlog);
242 if (socket_get_backlog(sock, &backlog_retrieved, testclass, test,
243 "socket_listen_update") < 0) {
248 if (backlog_retrieved != listen_backlog_assertion) {
249 warnx("%s: %s: socket_listen_update: initial backlog is %d "
250 "not %d", testclass, test, backlog_retrieved,
251 listen_backlog_assertion);
256 if (listen(sock, update_backlog) < 0) {
257 warn("%s: %s: socket_listen_update: update listen(, %d)",
258 testclass, test, update_backlog);
263 if (socket_get_backlog(sock, &backlog_retrieved, testclass, test,
264 "socket_listen_update") < 0) {
269 if (backlog_retrieved != update_backlog_assertion) {
270 warnx("%s: %s: socket_listen_update: updated backlog is %d "
271 "not %d", testclass, test, backlog_retrieved,
272 update_backlog_assertion);
282 * This test tests using listen() to update the queue depth after a socket
283 * has already been marked as listening. We test several cases: setting the
284 * socket < 0, 0, 1, somaxconn, and somaxconn + 1.
287 test_listen_update(void)
292 * Set to 5, update to -1, which should give somaxconn.
294 if (socket_listen_update(PF_INET, SOCK_STREAM, 0, 5, -1, 5, somaxconn,
295 &sock, "PF_INET", "SOCK_STREAM", "test_listen_update",
301 * Set to 5, update to 0, which should give 0.
303 if (socket_listen_update(PF_INET, SOCK_STREAM, 0, 5, 0, 5, 0, &sock,
304 "PF_INET", "SOCK_STREAM", "test_listen_update", "update_5,0")
310 * Set to 5, update to 1, which should give 1.
312 if (socket_listen_update(PF_INET, SOCK_STREAM, 0, 5, 1, 5, 1, &sock,
313 "PF_INET", "SOCK_STREAM", "test_listen_update", "update_5,1")
319 * Set to 5, update to somaxconn, which should give somaxconn.
321 if (socket_listen_update(PF_INET, SOCK_STREAM, 0, 5, somaxconn, 5,
322 somaxconn, &sock, "PF_INET", "SOCK_STREAM", "test_listen_update",
323 "update_5,somaxconn") < 0)
328 * Set to 5, update to somaxconn+1, which should give somaxconn.
330 if (socket_listen_update(PF_INET, SOCK_STREAM, 0, 5, somaxconn+1, 5,
331 somaxconn, &sock, "PF_INET", "SOCK_STREAM", "test_listen_update",
332 "update_5,somaxconn+1") < 0)
338 * SO_LISTENQLIMIT is a read-only socket option, so make sure we get an error
339 * if we try to write it.
342 test_set_qlimit(void)
346 sock = socket(PF_INET, SOCK_STREAM, 0);
348 err(-1, "test_set_qlimit: socket(PF_INET, SOCK_STREAM)");
351 ret = setsockopt(sock, SOL_SOCKET, SO_LISTENQLIMIT, &i, sizeof(i));
352 if (ret < 0 && errno != ENOPROTOOPT) {
353 warn("test_set_qlimit: setsockopt(SOL_SOCKET, "
354 "SO_LISTENQLIMIT, 0): unexpected error");
359 warnx("test_set_qlimit: setsockopt(SOL_SOCKET, "
360 "SO_LISTENQLIMIT, 0) succeeded");
368 main(int argc, char *argv[])
372 len = sizeof(somaxconn);
373 if (sysctlbyname("kern.ipc.somaxconn", &somaxconn, &len, NULL, 0)
375 err(-1, "sysctlbyname(kern.ipc.somaxconn)");
378 test_listen_update();