18a272653SPeter Holm#!/bin/sh 28a272653SPeter Holm 38a272653SPeter Holm# 48a272653SPeter Holm# Copyright (c) 2016 EMC Corp. 58a272653SPeter Holm# All rights reserved. 68a272653SPeter Holm# 78a272653SPeter Holm# Redistribution and use in source and binary forms, with or without 88a272653SPeter Holm# modification, are permitted provided that the following conditions 98a272653SPeter Holm# are met: 108a272653SPeter Holm# 1. Redistributions of source code must retain the above copyright 118a272653SPeter Holm# notice, this list of conditions and the following disclaimer. 128a272653SPeter Holm# 2. Redistributions in binary form must reproduce the above copyright 138a272653SPeter Holm# notice, this list of conditions and the following disclaimer in the 148a272653SPeter Holm# documentation and/or other materials provided with the distribution. 158a272653SPeter Holm# 168a272653SPeter Holm# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 178a272653SPeter Holm# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 188a272653SPeter Holm# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 198a272653SPeter Holm# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 208a272653SPeter Holm# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 218a272653SPeter Holm# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 228a272653SPeter Holm# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 238a272653SPeter Holm# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 248a272653SPeter Holm# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 258a272653SPeter Holm# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 268a272653SPeter Holm# SUCH DAMAGE. 278a272653SPeter Holm# 288a272653SPeter Holm 298a272653SPeter Holm# No problems seen 308a272653SPeter Holm# Stand alone version of testcases/tcp 318a272653SPeter Holm 328a272653SPeter Holm. ../default.cfg 338a272653SPeter Holm 348a272653SPeter Holmdir=/tmp 358a272653SPeter Holmodir=`pwd` 368a272653SPeter Holmcd $dir 378a272653SPeter Holmsed '1,/^EOF/d' < $odir/$0 > $dir/tcp3.c 388a272653SPeter Holmmycc -o tcp3 -Wall -Wextra -O0 -g tcp3.c || exit 1 398a272653SPeter Holmrm -f tcp3.c 408a272653SPeter Holm 418a272653SPeter Holmdaemon sh -c "(cd $odir/../testcases/swap; ./swap -t 5m)" > /dev/null 2>&1 428a272653SPeter Holmtimeout 2m /tmp/tcp3 438a272653SPeter Holms=$? 448a272653SPeter Holmwhile pgrep -q swap; do 458a272653SPeter Holm pkill -9 swap 468a272653SPeter Holmdone 478a272653SPeter Holm 488a272653SPeter Holmif pgrep -q tcp3; then 498a272653SPeter Holm echo FAIL 508a272653SPeter Holm pgrep tcp3 | while read pid; do 518a272653SPeter Holm ps -lp$pid 528a272653SPeter Holm procstat -kk $pid 538a272653SPeter Holm done 548a272653SPeter Holm exit 1 558a272653SPeter Holmfi 568a272653SPeter Holmrm -rf /tmp/tcp3 /tmp/tcp3.c 578a272653SPeter Holmexit $s 588a272653SPeter Holm 598a272653SPeter HolmEOF 608a272653SPeter Holm#include <sys/param.h> 618a272653SPeter Holm#include <sys/mman.h> 628a272653SPeter Holm#include <sys/socket.h> 638a272653SPeter Holm#include <sys/stat.h> 648a272653SPeter Holm#include <sys/wait.h> 658a272653SPeter Holm 668a272653SPeter Holm#include <netinet/in.h> 678a272653SPeter Holm 688a272653SPeter Holm#include <machine/atomic.h> 698a272653SPeter Holm 708a272653SPeter Holm#include <err.h> 718a272653SPeter Holm#include <errno.h> 728a272653SPeter Holm#include <fcntl.h> 738a272653SPeter Holm#include <netdb.h> 748a272653SPeter Holm#include <signal.h> 758a272653SPeter Holm#include <stdio.h> 768a272653SPeter Holm#include <stdlib.h> 778a272653SPeter Holm#include <string.h> 788a272653SPeter Holm#include <time.h> 798a272653SPeter Holm#include <unistd.h> 808a272653SPeter Holm 818a272653SPeter Holm#define NB (400 * 1024 * 1024) 828a272653SPeter Holm#define SYNC 0 838a272653SPeter Holm#define PARALLEL 8 848a272653SPeter Holm#define RUNTIME (1 * 60) 858a272653SPeter Holm 868a272653SPeter Holmstatic sig_atomic_t done_testing; 878a272653SPeter Holmstatic volatile u_int *share; 888a272653SPeter Holmstatic int bufsize; 898a272653SPeter Holmstatic int port; 908a272653SPeter Holm 918a272653SPeter Holmstatic void 928a272653SPeter Holmalarm_handler(int i __unused) 938a272653SPeter Holm{ 948a272653SPeter Holm 958a272653SPeter Holm done_testing++; 968a272653SPeter Holm} 978a272653SPeter Holm 988a272653SPeter Holmstatic void 998a272653SPeter Holmexit_handler(int i __unused) 1008a272653SPeter Holm{ 1018a272653SPeter Holm 1028a272653SPeter Holm _exit(1); 1038a272653SPeter Holm} 1048a272653SPeter Holm 1058a272653SPeter Holmstatic void 1068a272653SPeter Holmcallcleanup(void) 1078a272653SPeter Holm{ 1088a272653SPeter Holm} 1098a272653SPeter Holm 1108a272653SPeter Holmstatic int 1118a272653SPeter Holmrandom_int(int mi, int ma) 1128a272653SPeter Holm{ 1138a272653SPeter Holm return (arc4random() % (ma - mi + 1) + mi); 1148a272653SPeter Holm} 1158a272653SPeter Holm 1168a272653SPeter Holmstatic void 1178a272653SPeter Holmreader(void) { 1188a272653SPeter Holm struct sockaddr_in inetaddr, inetpeer; 1198a272653SPeter Holm socklen_t len; 120*8d72c409SPeter Holm int n, *buf; 1218a272653SPeter Holm int on; 1228a272653SPeter Holm int tcpsock, msgsock; 1238a272653SPeter Holm 1248a272653SPeter Holm on = 1; 1258a272653SPeter Holm if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0) 1268a272653SPeter Holm err(1, "socket(), %s:%d", __FILE__, __LINE__); 1278a272653SPeter Holm 1288a272653SPeter Holm if (setsockopt(tcpsock, 1298a272653SPeter Holm SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0) 1308a272653SPeter Holm err(1, "setsockopt(), %s:%d", __FILE__, __LINE__); 1318a272653SPeter Holm 1328a272653SPeter Holm inetaddr.sin_family = AF_INET; 1338a272653SPeter Holm inetaddr.sin_addr.s_addr = INADDR_ANY; 1348a272653SPeter Holm inetaddr.sin_port = htons(port); 1358a272653SPeter Holm inetaddr.sin_len = sizeof(inetaddr); 1368a272653SPeter Holm 1378a272653SPeter Holm if (bind(tcpsock, 1388a272653SPeter Holm (struct sockaddr *)&inetaddr, sizeof (inetaddr)) < 0) 1398a272653SPeter Holm err(1, "bind(), %s:%d", __FILE__, __LINE__); 1408a272653SPeter Holm 1418a272653SPeter Holm if (listen(tcpsock, 5) < 0) 1428a272653SPeter Holm err(1, "listen(), %s:%d", __FILE__, __LINE__); 1438a272653SPeter Holm 1448a272653SPeter Holm if (random_int(1,100) > 60) 1458a272653SPeter Holm sleep(6); 1468a272653SPeter Holm 1478a272653SPeter Holm len = sizeof(inetpeer); 1488a272653SPeter Holm if ((msgsock = accept(tcpsock, 1498a272653SPeter Holm (struct sockaddr *)&inetpeer, &len)) < 0) 1508a272653SPeter Holm err(1, "accept(), %s:%d", __FILE__, __LINE__); 1518a272653SPeter Holm 1528a272653SPeter Holm if ((buf = malloc(bufsize)) == NULL) 1538a272653SPeter Holm err(1, "malloc(%d), %s:%d", bufsize, __FILE__, 1548a272653SPeter Holm __LINE__); 1558a272653SPeter Holm while (done_testing == 0) { 1568a272653SPeter Holm if ((n = read(msgsock, buf, bufsize)) < 0) 1578a272653SPeter Holm err(1, "read(), %s:%d", __FILE__, __LINE__); 1588a272653SPeter Holm if (n == 0) break; 1598a272653SPeter Holm } 1608a272653SPeter Holm close(msgsock); 1618a272653SPeter Holm return; 1628a272653SPeter Holm} 1638a272653SPeter Holm 1648a272653SPeter Holmstatic void 1658a272653SPeter Holmwriter(void) { 1668a272653SPeter Holm struct hostent *hostent; 1678a272653SPeter Holm struct sockaddr_in inetaddr; 1688a272653SPeter Holm int i, *buf, r; 1698a272653SPeter Holm int tcpsock, on; 1708a272653SPeter Holm 1718a272653SPeter Holm on = 1; 1728a272653SPeter Holm for (i = 1; i < 5; i++) { 1738a272653SPeter Holm if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0) 1748a272653SPeter Holm err(1, "socket(), %s:%d", __FILE__, __LINE__); 1758a272653SPeter Holm 1768a272653SPeter Holm if (setsockopt(tcpsock, 1778a272653SPeter Holm SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0) 1788a272653SPeter Holm err(1, "setsockopt(), %s:%d", __FILE__, __LINE__); 1798a272653SPeter Holm 1808a272653SPeter Holm hostent = gethostbyname ("localhost"); 1818a272653SPeter Holm bzero((char *) &inetaddr, sizeof(inetaddr)); 1828a272653SPeter Holm memcpy (&inetaddr.sin_addr.s_addr, hostent->h_addr, 1838a272653SPeter Holm sizeof (struct in_addr)); 1848a272653SPeter Holm 1858a272653SPeter Holm inetaddr.sin_family = AF_INET; 1868a272653SPeter Holm inetaddr.sin_port = htons(port); 1878a272653SPeter Holm inetaddr.sin_len = sizeof(inetaddr); 1888a272653SPeter Holm 1898a272653SPeter Holm r = connect(tcpsock, (struct sockaddr *) &inetaddr, 1908a272653SPeter Holm sizeof(inetaddr)); 1918a272653SPeter Holm if (r == 0) 1928a272653SPeter Holm break; 1938a272653SPeter Holm sleep(1); 1948a272653SPeter Holm close(tcpsock); 1958a272653SPeter Holm } 1968a272653SPeter Holm if (r < 0) 1978a272653SPeter Holm err(1, "connect(), %s:%d", __FILE__, __LINE__); 1988a272653SPeter Holm 1998a272653SPeter Holm if ((buf = malloc(bufsize)) == NULL) 2008a272653SPeter Holm err(1, "malloc(%d), %s:%d", bufsize, __FILE__, 2018a272653SPeter Holm __LINE__); 2028a272653SPeter Holm for (i = 0; i < bufsize / (int)sizeof(int); i++) 2038a272653SPeter Holm buf[i] = i; 2048a272653SPeter Holm 2058a272653SPeter Holm for (;;) { 2068a272653SPeter Holm for (i = 0; i < NB; i+= bufsize) { 2078a272653SPeter Holm if (write(tcpsock, buf, bufsize) < 0) { 2088a272653SPeter Holm if (errno == EPIPE) 2098a272653SPeter Holm return; 2108a272653SPeter Holm if (errno != ECONNRESET) 2118a272653SPeter Holm err(1, "write(%d), %s:%d", tcpsock, 2128a272653SPeter Holm __FILE__, __LINE__); 2138a272653SPeter Holm _exit(EXIT_SUCCESS); 2148a272653SPeter Holm } 2158a272653SPeter Holm } 2168a272653SPeter Holm } 2178a272653SPeter Holm return; 2188a272653SPeter Holm} 2198a272653SPeter Holm 2208a272653SPeter Holmstatic int 2218a272653SPeter Holmtest2(void) 2228a272653SPeter Holm{ 2238a272653SPeter Holm pid_t pid; 2248a272653SPeter Holm 2258a272653SPeter Holm if ((pid = fork()) == 0) { 2268a272653SPeter Holm writer(); 2278a272653SPeter Holm _exit(EXIT_SUCCESS); 2288a272653SPeter Holm 2298a272653SPeter Holm } else if (pid > 0) { 2308a272653SPeter Holm reader(); 2318a272653SPeter Holm kill(pid, SIGINT); 2328a272653SPeter Holm waitpid(pid, NULL, 0); 2338a272653SPeter Holm } else 2348a272653SPeter Holm err(1, "fork(), %s:%d", __FILE__, __LINE__); 2358a272653SPeter Holm 2368a272653SPeter Holm return (0); 2378a272653SPeter Holm} 2388a272653SPeter Holm 2398a272653SPeter Holmstatic void 2408a272653SPeter Holmtest(int nb) 2418a272653SPeter Holm{ 2428a272653SPeter Holm atomic_add_int(&share[SYNC], 1); 2438a272653SPeter Holm while (share[SYNC] != PARALLEL) 2448a272653SPeter Holm ; 2458a272653SPeter Holm 2468a272653SPeter Holm port = 12340 + nb; 2478a272653SPeter Holm bufsize = 2 << random_int(1, 12); 2488a272653SPeter Holm signal(SIGALRM, alarm_handler); 2498a272653SPeter Holm signal(SIGINT, exit_handler); 2508a272653SPeter Holm atexit(callcleanup); 2518a272653SPeter Holm 2528a272653SPeter Holm alarm(2); 2538a272653SPeter Holm test2(); 2548a272653SPeter Holm 2558a272653SPeter Holm _exit(0); 2568a272653SPeter Holm} 2578a272653SPeter Holm 2588a272653SPeter Holmint 2598a272653SPeter Holmmain(void) 2608a272653SPeter Holm{ 2618a272653SPeter Holm size_t len; 2628a272653SPeter Holm time_t start; 2638a272653SPeter Holm int e, i, pids[PARALLEL], status; 2648a272653SPeter Holm 2658a272653SPeter Holm e = 0; 2668a272653SPeter Holm len = PAGE_SIZE; 2678a272653SPeter Holm if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE, 2688a272653SPeter Holm MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED) 2698a272653SPeter Holm err(1, "mmap"); 2708a272653SPeter Holm 2718a272653SPeter Holm start = time(NULL); 2728a272653SPeter Holm while ((time(NULL) - start) < RUNTIME && e == 0) { 2738a272653SPeter Holm share[SYNC] = 0; 2748a272653SPeter Holm for (i = 0; i < PARALLEL; i++) { 2758a272653SPeter Holm if ((pids[i] = fork()) == 0) 2768a272653SPeter Holm test(i); 2778a272653SPeter Holm } 2788a272653SPeter Holm for (i = 0; i < PARALLEL; i++) { 2798a272653SPeter Holm if (waitpid(pids[i], &status, 0) == -1) 2808a272653SPeter Holm err(1, "waitpid(%d)", pids[i]); 2818a272653SPeter Holm e += status == 0 ? 0 : 1; 2828a272653SPeter Holm } 2838a272653SPeter Holm } 2848a272653SPeter Holm 2858a272653SPeter Holm return (e); 2868a272653SPeter Holm} 287