Bring in FreeBSD's stress2 stress testing suite.
[dragonfly.git] / test / stress / stress2 / testcases / tcp / tcp.c
1 /*-
2  * Copyright (c) 2008 Peter Holm <pho@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  *
26  */
27
28 #include <sys/param.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <netdb.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <unistd.h>
35 #include <signal.h>
36 #include <errno.h>
37 #include <string.h>
38 #include <err.h>
39
40 #include "stress.h"
41
42 #define NB (400 * 1024 * 1024)
43
44 int port;
45 int bufsize;
46
47 static void
48 reader(void) {
49         int tcpsock, msgsock;
50         int on;
51         socklen_t len;
52         struct sockaddr_in inetaddr, inetpeer;
53         int n, t, *buf;
54
55         on = 1;
56         if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
57                 err(1, "socket(), %s:%d", __FILE__, __LINE__);
58
59         if (setsockopt(tcpsock,
60             SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
61                 err(1, "setsockopt(), %s:%d", __FILE__, __LINE__);
62
63         inetaddr.sin_family = AF_INET;
64         inetaddr.sin_addr.s_addr = INADDR_ANY;
65         inetaddr.sin_port = htons(port);
66         inetaddr.sin_len = sizeof(inetaddr);
67
68         if (bind(tcpsock,
69             (struct sockaddr *)&inetaddr, sizeof (inetaddr)) < 0)
70                 err(1, "bind(), %s:%d", __FILE__, __LINE__);
71
72         if (listen(tcpsock, 5) < 0)
73                 err(1, "listen(), %s:%d", __FILE__, __LINE__);
74
75         if ((random_int(1,100) > 60) || (op->hog == 1)) {
76                 usleep(random_int(1000000,1000000) * 60);
77         }
78
79         len = sizeof(inetpeer);
80         if ((msgsock = accept(tcpsock,
81             (struct sockaddr *)&inetpeer, &len)) < 0)
82                 err(1, "accept(), %s:%d", __FILE__, __LINE__);
83
84         t = 0;
85         if ((buf = malloc(bufsize)) == NULL)
86                         err(1, "malloc(%d), %s:%d", bufsize, __FILE__, __LINE__);
87         while (done_testing == 0) {
88                 if ((n = read(msgsock, buf, bufsize)) < 0)
89                         err(1, "read(), %s:%d", __FILE__, __LINE__);
90                 t += n;
91                 if (n == 0) break;
92         }
93         close(msgsock);
94         return;
95 }
96
97 static void
98 writer(void) {
99         int tcpsock, on;
100         struct sockaddr_in inetaddr;
101         struct hostent *hostent;
102         int i, *buf, r;
103
104         on = 1;
105         for (i = 1; i < 5; i++) {
106                 if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
107                         err(1, "socket(), %s:%d", __FILE__, __LINE__);
108
109                 if (setsockopt(tcpsock,
110                     SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
111                         err(1, "setsockopt(), %s:%d", __FILE__, __LINE__);
112
113                 hostent = gethostbyname ("localhost");
114                 memcpy (&inetaddr.sin_addr.s_addr, hostent->h_addr,
115                         sizeof (struct in_addr));
116
117                 inetaddr.sin_family = AF_INET;
118                 inetaddr.sin_addr.s_addr = INADDR_ANY;
119                 inetaddr.sin_port = htons(port);
120                 inetaddr.sin_len = sizeof(inetaddr);
121
122                 r = connect(tcpsock, (struct sockaddr *) &inetaddr,
123                         sizeof(inetaddr));
124                 if (r == 0)
125                         break;
126                 sleep(1);
127                 close(tcpsock);
128         }
129         if (r < 0)
130                 err(1, "connect(), %s:%d", __FILE__, __LINE__);
131
132         if ((buf = malloc(bufsize)) == NULL)
133                         err(1, "malloc(%d), %s:%d", bufsize, __FILE__, __LINE__);
134         for (i = 0; i < bufsize / sizeof(int); i++)
135                 buf[i] = i;
136
137         for (;;) {
138                 for (i = 0; i < NB; i+= bufsize) {
139                         if (write(tcpsock, buf, bufsize) < 0) {
140                                 if (errno == EPIPE)
141                                         return;
142                                 err(1, "write(%d), %s:%d", tcpsock,
143                                                 __FILE__, __LINE__);
144                         }
145                 }
146         }
147         return;
148 }
149
150 int
151 setup(int nb)
152 {
153         port = 12340 + nb;
154         bufsize = 2 << random_int(1, 12);
155         return (0);
156 }
157
158 void
159 cleanup(void)
160 {
161 }
162
163 int
164 test(void)
165 {
166         pid_t pid;
167
168         if ((pid = fork()) == 0) {
169                 writer();
170                 exit(EXIT_SUCCESS);
171
172         } else if (pid > 0) {
173                 reader();
174                 kill(pid, SIGINT);
175         } else
176                 err(1, "fork(), %s:%d",  __FILE__, __LINE__);
177
178         return (0);
179 }