Changeset View
Changeset View
Standalone View
Standalone View
tests/sys/netinet/reuseport_lb.c
- This file was added.
/*- | |||||
* Copyright (c) 2018 The FreeBSD Foundation | |||||
* | |||||
* This software was developed by Mark Johnston under sponsorship from | |||||
* the FreeBSD Foundation. | |||||
* | |||||
* 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. | |||||
*/ | |||||
#include <sys/cdefs.h> | |||||
__FBSDID("$FreeBSD$"); | |||||
#include <sys/param.h> | |||||
#include <sys/socket.h> | |||||
#include <netinet/in.h> | |||||
#include <err.h> | |||||
#include <errno.h> | |||||
#include <stdlib.h> | |||||
#include <unistd.h> | |||||
#include <atf-c.h> | |||||
/* | |||||
* Given an array of non-blocking listening sockets configured in a LB group | |||||
* for "addr", try connecting to "addr" in a loop and verify that connections | |||||
* are roughly balanced across the sockets. | |||||
*/ | |||||
static void | |||||
lb_simple_accept_loop(int domain, const struct sockaddr *addr, int sds[], | |||||
size_t nsds, int nconns) | |||||
{ | |||||
size_t i; | |||||
int *acceptcnt; | |||||
int csd, error, excnt, sd; | |||||
/* | |||||
* We expect each listening socket to accept roughly nconns/nsds | |||||
* connections, but allow for some error. | |||||
*/ | |||||
excnt = nconns / nsds / 8; | |||||
acceptcnt = calloc(nsds, sizeof(*acceptcnt)); | |||||
ATF_REQUIRE_MSG(acceptcnt != NULL, "calloc() failed: %s", | |||||
strerror(errno)); | |||||
while (nconns-- > 0) { | |||||
sd = socket(domain, SOCK_STREAM, 0); | |||||
ATF_REQUIRE_MSG(sd >= 0, "socket() failed: %s", | |||||
strerror(errno)); | |||||
error = connect(sd, addr, addr->sa_len); | |||||
ATF_REQUIRE_MSG(error == 0, "connect() failed: %s", | |||||
strerror(errno)); | |||||
/* | |||||
* Poll the listening sockets. | |||||
*/ | |||||
do { | |||||
for (i = 0; i < nsds; i++) { | |||||
csd = accept(sds[i], NULL, NULL); | |||||
if (csd < 0) { | |||||
ATF_REQUIRE_MSG(errno == EWOULDBLOCK || | |||||
errno == EAGAIN, | |||||
"accept() failed: %s", | |||||
strerror(errno)); | |||||
continue; | |||||
} | |||||
error = close(csd); | |||||
ATF_REQUIRE_MSG(error == 0, | |||||
"close() failed: %s", strerror(errno)); | |||||
asomers: I think you should assert here that i < nsds. Otherwise, it's possible that some connections… | |||||
acceptcnt[i]++; | |||||
break; | |||||
} | |||||
} while (i == nsds); | |||||
error = close(sd); | |||||
ATF_REQUIRE_MSG(error == 0, "close() failed: %s", | |||||
strerror(errno)); | |||||
} | |||||
for (i = 0; i < nsds; i++) | |||||
ATF_REQUIRE_MSG(acceptcnt[i] > excnt, "uneven balancing"); | |||||
} | |||||
static int | |||||
Done Inline ActionsWhat's the meaning of 8? asomers: What's the meaning of `8`? | |||||
Not Done Inline ActionsBecause connections are balanced among sockets in the group, we expect each listening socket to accept nconns / sdcnt connections. 8 is just an arbitrary factor to allow for some error. I will add a comment. markj: Because connections are balanced among sockets in the group, we expect each listening socket to… | |||||
lb_listen_socket(int domain, int flags) | |||||
{ | |||||
Not Done Inline Actionswaitpid doesn't need to be in the loop. asomers: `waitpid` doesn't need to be in the loop. | |||||
Not Done Inline ActionsWhat if the child exits prematurely after creating a few connections? markj: What if the child exits prematurely after creating a few connections? | |||||
size_t one; | |||||
int error, sd; | |||||
sd = socket(domain, SOCK_STREAM | flags, 0); | |||||
ATF_REQUIRE_MSG(sd >= 0, "socket() failed: %s", strerror(errno)); | |||||
one = 1; | |||||
error = setsockopt(sd, SOL_SOCKET, SO_REUSEPORT_LB, &one, sizeof(one)); | |||||
ATF_REQUIRE_MSG(error == 0, "setsockopt(SO_REUSEPORT_LB) failed: %s", | |||||
strerror(errno)); | |||||
return (sd); | |||||
} | |||||
Done Inline ActionsAre the write and read really necessary? asomers: Are the write and read really necessary? | |||||
Not Done Inline ActionsI guess not. The child is using a blocking socket, so connect() will block until the parent accept()s. Will remove. markj: I guess not. The child is using a blocking socket, so connect() will block until the parent… | |||||
ATF_TC_WITHOUT_HEAD(basic_ipv4); | |||||
ATF_TC_BODY(basic_ipv4, tc) | |||||
{ | |||||
struct sockaddr_in addr; | |||||
socklen_t slen; | |||||
size_t i; | |||||
const int nconns = 16384; | |||||
int error, sds[16]; | |||||
uint16_t port; | |||||
sds[0] = lb_listen_socket(PF_INET, SOCK_NONBLOCK); | |||||
memset(&addr, 0, sizeof(addr)); | |||||
addr.sin_len = sizeof(addr); | |||||
addr.sin_family = AF_INET; | |||||
addr.sin_port = htons(0); | |||||
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); | |||||
error = bind(sds[0], (const struct sockaddr *)&addr, sizeof(addr)); | |||||
ATF_REQUIRE_MSG(error == 0, "bind() failed: %s", strerror(errno)); | |||||
error = listen(sds[0], 1); | |||||
ATF_REQUIRE_MSG(error == 0, "listen() failed: %s", strerror(errno)); | |||||
slen = sizeof(addr); | |||||
error = getsockname(sds[0], (struct sockaddr *)&addr, &slen); | |||||
ATF_REQUIRE_MSG(error == 0, "getsockname() failed: %s", | |||||
strerror(errno)); | |||||
ATF_REQUIRE_MSG(slen == sizeof(addr), "sockaddr size changed"); | |||||
port = addr.sin_port; | |||||
memset(&addr, 0, sizeof(addr)); | |||||
addr.sin_len = sizeof(addr); | |||||
addr.sin_family = AF_INET; | |||||
addr.sin_port = port; | |||||
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); | |||||
for (i = 1; i < nitems(sds); i++) { | |||||
sds[i] = lb_listen_socket(PF_INET, SOCK_NONBLOCK); | |||||
error = bind(sds[i], (const struct sockaddr *)&addr, | |||||
sizeof(addr)); | |||||
ATF_REQUIRE_MSG(error == 0, "bind() failed: %s", | |||||
strerror(errno)); | |||||
error = listen(sds[i], 1); | |||||
ATF_REQUIRE_MSG(error == 0, "listen() failed: %s", | |||||
strerror(errno)); | |||||
} | |||||
lb_simple_accept_loop(PF_INET, (struct sockaddr *)&addr, sds, | |||||
nitems(sds), nconns); | |||||
for (i = 0; i < nitems(sds); i++) { | |||||
error = close(sds[i]); | |||||
ATF_REQUIRE_MSG(error == 0, "close() failed: %s", | |||||
strerror(errno)); | |||||
Not Done Inline ActionsDoes the use of port 0 force the test to run as root? asomers: Does the use of port 0 force the test to run as root? | |||||
Not Done Inline ActionsNo, it's the wildcard port. (Is there a constant for this? I can't find one.) markj: No, it's the wildcard port. (Is there a constant for this? I can't find one.) | |||||
} | |||||
} | |||||
ATF_TC_WITHOUT_HEAD(basic_ipv6); | |||||
ATF_TC_BODY(basic_ipv6, tc) | |||||
{ | |||||
const struct in6_addr loopback6 = IN6ADDR_LOOPBACK_INIT; | |||||
struct sockaddr_in6 addr; | |||||
socklen_t slen; | |||||
Not Done Inline ActionsAny reason not to use ATF_REQUIRE_EQ_MSG? asomers: Any reason not to use `ATF_REQUIRE_EQ_MSG`? | |||||
Not Done Inline ActionsNo, but I find it less clear. markj: No, but I find it less clear. | |||||
size_t i; | |||||
const int nconns = 16384; | |||||
int error, sds[16]; | |||||
uint16_t port; | |||||
sds[0] = lb_listen_socket(PF_INET6, SOCK_NONBLOCK); | |||||
memset(&addr, 0, sizeof(addr)); | |||||
addr.sin6_len = sizeof(addr); | |||||
addr.sin6_family = AF_INET6; | |||||
addr.sin6_port = htons(0); | |||||
addr.sin6_addr = loopback6; | |||||
error = bind(sds[0], (const struct sockaddr *)&addr, sizeof(addr)); | |||||
ATF_REQUIRE_MSG(error == 0, "bind() failed: %s", strerror(errno)); | |||||
error = listen(sds[0], 1); | |||||
ATF_REQUIRE_MSG(error == 0, "listen() failed: %s", strerror(errno)); | |||||
slen = sizeof(addr); | |||||
error = getsockname(sds[0], (struct sockaddr *)&addr, &slen); | |||||
ATF_REQUIRE_MSG(error == 0, "getsockname() failed: %s", | |||||
strerror(errno)); | |||||
ATF_REQUIRE_MSG(slen == sizeof(addr), "sockaddr size changed"); | |||||
port = addr.sin6_port; | |||||
memset(&addr, 0, sizeof(addr)); | |||||
addr.sin6_len = sizeof(addr); | |||||
addr.sin6_family = AF_INET6; | |||||
addr.sin6_port = port; | |||||
addr.sin6_addr = loopback6; | |||||
for (i = 1; i < nitems(sds); i++) { | |||||
sds[i] = lb_listen_socket(PF_INET6, SOCK_NONBLOCK); | |||||
error = bind(sds[i], (const struct sockaddr *)&addr, | |||||
sizeof(addr)); | |||||
ATF_REQUIRE_MSG(error == 0, "bind() failed: %s", | |||||
strerror(errno)); | |||||
error = listen(sds[i], 1); | |||||
ATF_REQUIRE_MSG(error == 0, "listen() failed: %s", | |||||
strerror(errno)); | |||||
} | |||||
lb_simple_accept_loop(PF_INET6, (struct sockaddr *)&addr, sds, | |||||
nitems(sds), nconns); | |||||
for (i = 0; i < nitems(sds); i++) { | |||||
error = close(sds[i]); | |||||
ATF_REQUIRE_MSG(error == 0, "close() failed: %s", | |||||
strerror(errno)); | |||||
} | |||||
} | |||||
ATF_TP_ADD_TCS(tp) | |||||
{ | |||||
ATF_TP_ADD_TC(tp, basic_ipv4); | |||||
ATF_TP_ADD_TC(tp, basic_ipv6); | |||||
return (atf_no_error()); | |||||
} | |||||
Done Inline Actionsmagic numbers should be #defined. asomers: magic numbers should be #defined. |
I think you should assert here that i < nsds. Otherwise, it's possible that some connections don't get accepted on any sockets and you wouldn't notice.