freebsd-src/tests/sys/netinet/so_reuseport_lb_test.c
Alfredo Dal'Ava Junior 9b2877353b so_reuseport_lb_test: correct setsockopt parameter size
Change type of variable used in setsocketopt so correct size of
option is passed.

Test failure was identified when running the test on PowerPC64,
and the following error message was seen:

	"bind () failed: Address already in use"

Submitted by:	Fernando Valle <fernando.valle@eldorado.org.br>
Reviewed by:	melifaro, adalava
Approved by:	jhibbits (mentor)
Sponsored by:	Eldorado Research Institute (eldorado.org.br)
Differential Revision:	https://reviews.freebsd.org/D24164
2020-03-25 20:20:08 +00:00

243 lines
6.9 KiB
C

/*-
* 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));
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
lb_listen_socket(int domain, int flags)
{
int 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);
}
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));
}
}
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;
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());
}