1 //==========================================================================
5 // Simple TCP throughput test - echo component FOR LINUX HOST
6 // * CAUTION: host, i.e. non eCos, only *
8 //==========================================================================
9 //####BSDCOPYRIGHTBEGIN####
11 // -------------------------------------------
13 // Portions of this software may have been derived from OpenBSD or other sources,
14 // and are covered by the appropriate copyright disclaimers included herein.
16 // -------------------------------------------
18 //####BSDCOPYRIGHTEND####
19 //==========================================================================
20 //#####DESCRIPTIONBEGIN####
23 // Contributors: gthomas
26 // Description: This is the middle part of a three part test. The idea is
27 // to test the throughput of box in a configuration like this:
29 // +------+ port +----+ port +----+
30 // |SOURCE|=========>|ECHO|============>|SINK|
31 // +------+ 9990 +----+ 9991 +----+
34 //####DESCRIPTIONEND####
36 //==========================================================================
43 #include <sys/param.h>
44 #include <sys/socket.h>
45 #include <sys/ioctl.h>
46 #include <sys/errno.h>
50 #include <netinet/in.h>
51 #include <netinet/ip.h>
52 #include <netinet/ip_icmp.h>
53 #include <net/route.h>
63 #define diag_printf printf
73 #define SOURCE_PORT 9990
74 #define SINK_PORT 9991
77 static unsigned char data_buf[MAX_BUF];
89 int max( int a, int b ) { return (a>b)?a:b; }
90 int min( int a, int b ) { return (a<b)?a:b; }
94 do_read(int s, void *_buf, int len)
96 int total, slen, rlen;
97 unsigned char *buf = (unsigned char *)_buf;
100 while (total < len) {
101 slen = read(s, buf, rlen);
104 diag_printf("Error after reading %d bytes\n", total);
116 do_write(int s, void *_buf, int len)
118 int total, slen, rlen;
119 unsigned char *buf = (unsigned char *)_buf;
122 while (total < len) {
123 slen = write(s, buf, rlen);
126 diag_printf("Error after writing %d bytes\n", total);
140 int s_source, s_sink, e_source, e_sink;
141 struct sockaddr_in e_source_addr, e_sink_addr, local;
145 struct test_params params,nparams;
146 struct test_status status,nstatus;
148 s_source = socket(AF_INET, SOCK_STREAM, 0);
150 pexit("stream socket");
152 if (setsockopt(s_source, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one))) {
153 pexit("setsockopt /source/ SO_REUSEADDR");
155 memset(&local, 0, sizeof(local));
156 local.sin_family = AF_INET;
157 // local.sin_len = sizeof(local);
158 local.sin_port = ntohs(SOURCE_PORT);
159 local.sin_addr.s_addr = INADDR_ANY;
160 if(bind(s_source, (struct sockaddr *) &local, sizeof(local)) < 0) {
161 pexit("bind /source/ error");
163 listen(s_source, SOMAXCONN);
165 s_sink = socket(AF_INET, SOCK_STREAM, 0);
167 pexit("stream socket");
169 memset(&local, 0, sizeof(local));
170 local.sin_family = AF_INET;
171 // local.sin_len = sizeof(local);
172 local.sin_port = ntohs(SINK_PORT);
173 local.sin_addr.s_addr = INADDR_ANY;
174 if(bind(s_sink, (struct sockaddr *) &local, sizeof(local)) < 0) {
175 pexit("bind /sink/ error");
177 if (setsockopt(s_sink, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one))) {
178 pexit("setsockopt /sink/ SO_REUSEADDR");
180 listen(s_sink, SOMAXCONN);
182 e_source = 0; e_sink = 0;
184 // Wait for a connection on either of the ports
186 FD_SET(s_source, &in_fds);
187 FD_SET(s_sink, &in_fds);
188 num = select(max(s_sink,s_source)+1, &in_fds, 0, 0, 0);
189 if (FD_ISSET(s_source, &in_fds)) {
190 len = sizeof(e_source_addr);
191 if ((e_source = accept(s_source, (struct sockaddr *)&e_source_addr, &len)) < 0) {
192 pexit("accept /source/");
194 diag_printf("SOURCE connection from %s:%d\n",
195 inet_ntoa(e_source_addr.sin_addr), ntohs(e_source_addr.sin_port));
197 if (FD_ISSET(s_sink, &in_fds)) {
198 len = sizeof(e_sink_addr);
199 if ((e_sink = accept(s_sink, (struct sockaddr *)&e_sink_addr, &len)) < 0) {
200 pexit("accept /sink/");
202 diag_printf("SINK connection from %s:%d\n",
203 inet_ntoa(e_sink_addr.sin_addr), ntohs(e_sink_addr.sin_port));
205 // Continue with test once a connection is established in both directions
206 if ((e_source != 0) && (e_sink != 0)) {
211 // Wait for "source" to tell us the testing paramters
212 if (do_read(e_source, &nparams, sizeof(nparams)) != sizeof(nparams)) {
213 pexit("Can't read initialization parameters");
216 params.nbufs = ntohl(nparams.nbufs);
217 params.bufsize = ntohl(nparams.bufsize);
218 params.load = ntohl(nparams.load);
220 diag_printf("Using %d buffers of %d bytes each, %d%% background load\n",
221 params.nbufs, params.bufsize, params.load);
223 // Tell the sink what the parameters are
224 if (do_write(e_sink, &nparams, sizeof(nparams)) != sizeof(nparams)) {
225 pexit("Can't write initialization parameters");
229 nstatus.ok = htonl(status.ok);
231 // Tell the "source" to start - we're all connected and ready to go!
232 if (do_write(e_source, &nstatus, sizeof(nstatus)) != sizeof(nstatus)) {
233 pexit("Can't send ACK to 'source' host");
238 // Echo the data from the source to the sink hosts
239 for (i = 0; i < params.nbufs; i++) {
240 if ((len = do_read(e_source, data_buf, params.bufsize)) != params.bufsize) {
242 diag_printf("Can't read buf #%d: ", i+1);
246 diag_printf("short read - only %d bytes\n", len);
250 if ((len = do_write(e_sink, data_buf, params.bufsize)) != params.bufsize) {
252 diag_printf("Can't write buf #%d: ", i+1);
256 diag_printf("short write - only %d bytes\n", len);
264 // Wait for the data to drain and the "sink" to tell us all is OK.
265 if (do_read(e_sink, &status, sizeof(status)) != sizeof(status)) {
266 pexit("Can't receive ACK from 'sink' host");
273 main(int argc, char *argv[])