xref: /freebsd/tools/test/stress2/misc/tcp4.sh (revision 4d846d260e2b9a3d4d0a701462568268cbfe7a5b)
18a272653SPeter Holm#!/bin/sh
28a272653SPeter Holm
38a272653SPeter Holm#
4*4d846d26SWarner Losh# SPDX-License-Identifier: BSD-2-Clause
58a272653SPeter Holm#
68a272653SPeter Holm# Copyright (c) 2018 Dell EMC Isilon
78a272653SPeter Holm#
88a272653SPeter Holm# Redistribution and use in source and binary forms, with or without
98a272653SPeter Holm# modification, are permitted provided that the following conditions
108a272653SPeter Holm# are met:
118a272653SPeter Holm# 1. Redistributions of source code must retain the above copyright
128a272653SPeter Holm#    notice, this list of conditions and the following disclaimer.
138a272653SPeter Holm# 2. Redistributions in binary form must reproduce the above copyright
148a272653SPeter Holm#    notice, this list of conditions and the following disclaimer in the
158a272653SPeter Holm#    documentation and/or other materials provided with the distribution.
168a272653SPeter Holm#
178a272653SPeter Holm# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
188a272653SPeter Holm# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
198a272653SPeter Holm# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
208a272653SPeter Holm# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
218a272653SPeter Holm# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
228a272653SPeter Holm# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
238a272653SPeter Holm# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
248a272653SPeter Holm# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
258a272653SPeter Holm# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
268a272653SPeter Holm# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
278a272653SPeter Holm# SUCH DAMAGE.
288a272653SPeter Holm#
298a272653SPeter Holm
308a272653SPeter Holm# "panic: in_pcbrele_wlocked: refcount 0" seen in WiP kernel code.
318a272653SPeter Holm
328a272653SPeter Holm. ../default.cfg
338a272653SPeter Holm[ `id -u` -ne 0 ] && echo "Must be root!" && exit 1
348a272653SPeter Holm
358a272653SPeter Holmcd /tmp
368a272653SPeter Holm
378a272653SPeter Holmcat > /tmp/tcp4.c <<EOF
388a272653SPeter Holm#include <sys/param.h>
398a272653SPeter Holm#include <sys/socket.h>
408a272653SPeter Holm#include <sys/wait.h>
418a272653SPeter Holm
428a272653SPeter Holm#include <netinet/in.h>
438a272653SPeter Holm
448a272653SPeter Holm#include <err.h>
458a272653SPeter Holm#include <errno.h>
468a272653SPeter Holm#include <netdb.h>
478a272653SPeter Holm#include <signal.h>
488a272653SPeter Holm#include <stdio.h>
498a272653SPeter Holm#include <stdlib.h>
508a272653SPeter Holm#include <string.h>
518a272653SPeter Holm#include <time.h>
528a272653SPeter Holm#include <unistd.h>
538a272653SPeter Holm
548a272653SPeter Holm#define NB (400 * 1024 * 1024)
558a272653SPeter Holm#define RUNTIME (16 * 60)
568a272653SPeter Holm
578a272653SPeter Holmstatic int port;
588a272653SPeter Holmstatic int bufsize;
598a272653SPeter Holm
608a272653SPeter Holmstatic int
618a272653SPeter Holmrandom_int(int mi, int ma)
628a272653SPeter Holm{
638a272653SPeter Holm        return (arc4random()  % (ma - mi + 1) + mi);
648a272653SPeter Holm}
658a272653SPeter Holm
668a272653SPeter Holmstatic void
678a272653SPeter Holmreader(void) {
688a272653SPeter Holm	socklen_t len;
698a272653SPeter Holm	struct sockaddr_in inetaddr, inetpeer;
708d72c409SPeter Holm	int tcpsock, msgsock ,on, n, *buf;
718a272653SPeter Holm
728a272653SPeter Holm	on = 1;
738a272653SPeter Holm	setproctitle("%s", __func__);
748a272653SPeter Holm	if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
758a272653SPeter Holm		err(1, "socket(), %s:%d", __FILE__, __LINE__);
768a272653SPeter Holm
778a272653SPeter Holm	if (setsockopt(tcpsock,
788a272653SPeter Holm	    SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
798a272653SPeter Holm		err(1, "setsockopt(), %s:%d", __FILE__, __LINE__);
808a272653SPeter Holm
818a272653SPeter Holm	inetaddr.sin_family = AF_INET;
828a272653SPeter Holm	inetaddr.sin_addr.s_addr = INADDR_ANY;
838a272653SPeter Holm	inetaddr.sin_port = htons(port);
848a272653SPeter Holm	inetaddr.sin_len = sizeof(inetaddr);
858a272653SPeter Holm
868a272653SPeter Holm	if (bind(tcpsock,
878a272653SPeter Holm	    (struct sockaddr *)&inetaddr, sizeof (inetaddr)) < 0)
888a272653SPeter Holm		err(1, "bind(), %s:%d", __FILE__, __LINE__);
898a272653SPeter Holm
908a272653SPeter Holm	if (listen(tcpsock, 5) < 0)
918a272653SPeter Holm		err(1, "listen(), %s:%d", __FILE__, __LINE__);
928a272653SPeter Holm
938a272653SPeter Holm	if ((random_int(1,100) > 60)) {
948a272653SPeter Holm		usleep(random_int(1000000,1000000) * 60);
958a272653SPeter Holm	}
968a272653SPeter Holm
978a272653SPeter Holm	len = sizeof(inetpeer);
988a272653SPeter Holm	if ((msgsock = accept(tcpsock,
998a272653SPeter Holm	    (struct sockaddr *)&inetpeer, &len)) < 0)
1008a272653SPeter Holm		err(1, "accept(), %s:%d", __FILE__, __LINE__);
1018a272653SPeter Holm
1028a272653SPeter Holm	if ((buf = malloc(bufsize)) == NULL)
1038a272653SPeter Holm			err(1, "malloc(%d), %s:%d", bufsize, __FILE__, __LINE__);
1048a272653SPeter Holm	for (;;) {
1058a272653SPeter Holm		if ((n = read(msgsock, buf, bufsize)) < 0)
1068a272653SPeter Holm			err(1, "read(), %s:%d", __FILE__, __LINE__);
1078a272653SPeter Holm		if (n == 0) break;
1088a272653SPeter Holm	}
1098a272653SPeter Holm	close(msgsock);
1108a272653SPeter Holm	return;
1118a272653SPeter Holm}
1128a272653SPeter Holm
1138a272653SPeter Holmstatic void
1148a272653SPeter Holmwriter(void) {
1158a272653SPeter Holm	struct sockaddr_in inetaddr;
1168a272653SPeter Holm	struct hostent *hostent;
1178a272653SPeter Holm	time_t start;
1188a272653SPeter Holm	int i, *buf, r;
1198a272653SPeter Holm	int tcpsock, on;
1208a272653SPeter Holm
1218a272653SPeter Holm	on = 1;
1228a272653SPeter Holm	setproctitle("%s", __func__);
1238a272653SPeter Holm	for (i = 1; i < 5; i++) {
1248a272653SPeter Holm		if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
1258a272653SPeter Holm			err(1, "socket(), %s:%d", __FILE__, __LINE__);
1268a272653SPeter Holm
1278a272653SPeter Holm		if (setsockopt(tcpsock,
1288a272653SPeter Holm		    SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
1298a272653SPeter Holm			err(1, "setsockopt(), %s:%d", __FILE__, __LINE__);
1308a272653SPeter Holm
1318a272653SPeter Holm		hostent = gethostbyname ("localhost");
1328a272653SPeter Holm		bzero((char *) &inetaddr, sizeof(inetaddr));
1338a272653SPeter Holm		memcpy (&inetaddr.sin_addr.s_addr, hostent->h_addr,
1348a272653SPeter Holm			sizeof (struct in_addr));
1358a272653SPeter Holm
1368a272653SPeter Holm		inetaddr.sin_family = AF_INET;
1378a272653SPeter Holm		inetaddr.sin_port = htons(port);
1388a272653SPeter Holm		inetaddr.sin_len = sizeof(inetaddr);
1398a272653SPeter Holm
1408a272653SPeter Holm		r = connect(tcpsock, (struct sockaddr *) &inetaddr,
1418a272653SPeter Holm			sizeof(inetaddr));
1428a272653SPeter Holm		if (r == 0)
1438a272653SPeter Holm			break;
1448a272653SPeter Holm		sleep(1);
1458a272653SPeter Holm		close(tcpsock);
1468a272653SPeter Holm	}
1478a272653SPeter Holm	if (r < 0)
1488a272653SPeter Holm		err(1, "connect(), %s:%d", __FILE__, __LINE__);
1498a272653SPeter Holm
1508a272653SPeter Holm	if ((buf = malloc(bufsize)) == NULL)
1518a272653SPeter Holm			err(1, "malloc(%d), %s:%d", bufsize, __FILE__, __LINE__);
1528a272653SPeter Holm	for (i = 0; i < bufsize / (int)sizeof(int); i++)
1538a272653SPeter Holm		buf[i] = i;
1548a272653SPeter Holm
1558a272653SPeter Holm	start = time(NULL);
1568a272653SPeter Holm	while (time(NULL) - start < 60) {
1578a272653SPeter Holm		for (i = 0; i < NB; i+= bufsize) {
1588a272653SPeter Holm			if (write(tcpsock, buf, bufsize) < 0) {
1598a272653SPeter Holm				if (errno == EPIPE)
1608a272653SPeter Holm					return;
1618a272653SPeter Holm				if (errno != ECONNRESET)
1628a272653SPeter Holm					err(1, "write(%d), %s:%d", tcpsock,
1638a272653SPeter Holm						__FILE__, __LINE__);
1648a272653SPeter Holm				_exit(EXIT_SUCCESS);
1658a272653SPeter Holm			}
1668a272653SPeter Holm		}
1678a272653SPeter Holm	}
1688a272653SPeter Holm	return;
1698a272653SPeter Holm}
1708a272653SPeter Holm
1718a272653SPeter Holmint
1728a272653SPeter Holmsetup(int nb)
1738a272653SPeter Holm{
1748a272653SPeter Holm	port = 12340 + nb;
1758a272653SPeter Holm	bufsize = 2 << random_int(1, 12);
1768a272653SPeter Holm	return (0);
1778a272653SPeter Holm}
1788a272653SPeter Holm
1798a272653SPeter Holmint
1808a272653SPeter Holmtest(void)
1818a272653SPeter Holm{
1828a272653SPeter Holm	pid_t pid;
1838a272653SPeter Holm
1848a272653SPeter Holm	sleep(1);
1858a272653SPeter Holm	if ((pid = fork()) == 0) {
1868a272653SPeter Holm		alarm(180);
1878a272653SPeter Holm		writer();
1888a272653SPeter Holm		_exit(EXIT_SUCCESS);
1898a272653SPeter Holm
1908a272653SPeter Holm	} else if (pid > 0) {
1918a272653SPeter Holm		alarm(180);
1928a272653SPeter Holm		reader();
1938a272653SPeter Holm		kill(pid, SIGINT);
1948a272653SPeter Holm		if (waitpid(pid, NULL, 0) != pid)
1958a272653SPeter Holm			err(1, "waitpid(%d)", pid);
1968a272653SPeter Holm	} else
1978a272653SPeter Holm		err(1, "fork(), %s:%d",  __FILE__, __LINE__);
1988a272653SPeter Holm
1998a272653SPeter Holm	return (0);
2008a272653SPeter Holm}
2018a272653SPeter Holm
2028a272653SPeter Holmint
2038a272653SPeter Holmmain(int argc, char *argv[])
2048a272653SPeter Holm{
2058a272653SPeter Holm	pid_t *pid;
2068a272653SPeter Holm	time_t start;
2078a272653SPeter Holm	int i, n;
2088a272653SPeter Holm
2098a272653SPeter Holm	if (argc != 2) {
2108a272653SPeter Holm		fprintf(stderr, "Usage: %s <threads>\n", argv[0]);
2118a272653SPeter Holm		exit(1);
2128a272653SPeter Holm	}
2138a272653SPeter Holm	n = atoi(argv[1]);
2148a272653SPeter Holm	pid = calloc(sizeof(pid_t *), n);
2158a272653SPeter Holm
2168a272653SPeter Holm	start = time(NULL);
2178a272653SPeter Holm	while (time(NULL) - start < RUNTIME) {
2188a272653SPeter Holm		for (i = 0; i < n; i++) {
2198a272653SPeter Holm			if ((pid[i] = fork()) == 0) {
2208a272653SPeter Holm				setup(i);
2218a272653SPeter Holm				test();
2228a272653SPeter Holm				_exit(0);
2238a272653SPeter Holm			}
2248a272653SPeter Holm		}
2258a272653SPeter Holm		for (i = 0; i < n; i++)
2268a272653SPeter Holm			if (waitpid(pid[i], NULL, 0) != pid[i])
2278a272653SPeter Holm				err(1, "waitpid(%d) l%d", pid[i], __LINE__);
2288a272653SPeter Holm		fprintf(stderr, "*** Done loop\n");
2298a272653SPeter Holm	}
2308a272653SPeter Holm
2318a272653SPeter Holm	return (0);
2328a272653SPeter Holm}
2338a272653SPeter HolmEOF
2348a272653SPeter Holm
2358a272653SPeter Holmmycc -o tcp4 -Wall -Wextra -O2 tcp4.c || exit 1
2368a272653SPeter Holmrm tcp4.c
2378a272653SPeter Holm
2388a272653SPeter Holmn=`sysctl -n kern.maxprocperuid`
2398a272653SPeter Holmn=$((n / 2 / 10 * 8))
2408a272653SPeter Holm./tcp4 $n > /dev/null 2>&1 &
2418a272653SPeter Holmsleep 30
2428a272653SPeter Holmwhile pgrep -q tcp4; do
2438a272653SPeter Holm	pkill tcp4
2448a272653SPeter Holmdone
2458a272653SPeter Holmwait
2468a272653SPeter Holmrm -f tcp4
2478a272653SPeter Holmexit 0
248