OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [net/] [common/] [current/] [tests/] [host_echo.c] - Blame information for rev 856

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
//==========================================================================
2
//
3
//      tests/host_echo.c
4
//
5
//      Simple TCP throughput test - echo component FOR *HOST*
6
//      * CAUTION: host, i.e. non eCos, only *
7
//
8
//==========================================================================
9
// ####BSDALTCOPYRIGHTBEGIN####                                             
10
// -------------------------------------------                              
11
// Portions of this software may have been derived from FreeBSD, OpenBSD,   
12
// or other sources, and if so are covered by the appropriate copyright     
13
// and license included herein.                                             
14
// -------------------------------------------                              
15
// ####BSDALTCOPYRIGHTEND####                                               
16
//==========================================================================
17
//#####DESCRIPTIONBEGIN####
18
//
19
// Author(s):    gthomas
20
// Contributors: gthomas
21
// Date:         2000-01-10
22
// Purpose:      
23
// Description:  This is the middle part of a three part test.  The idea is
24
//   to test the throughput of box in a configuration like this:
25
//
26
//      +------+   port   +----+     port    +----+
27
//      |SOURCE|=========>|ECHO|============>|SINK|
28
//      +------+   9990   +----+     9991    +----+
29
// 
30
//
31
//####DESCRIPTIONEND####
32
//
33
//==========================================================================
34
 
35
#undef _KERNEL
36
#include <stdlib.h>
37
#include <unistd.h>
38
#include <stdio.h>
39
 
40
#include <sys/param.h>
41
#include <sys/socket.h>
42
#include <sys/ioctl.h>
43
#include <sys/errno.h>
44
#include <sys/time.h>
45
 
46
#include <net/if.h>
47
#include <netinet/in_systm.h>
48
#include <netinet/in.h>
49
#include <netinet/ip.h>
50
#include <netinet/ip_icmp.h>
51
 
52
#include <netdb.h>
53
 
54
#define true 1
55
#define false 1
56
 
57
#define TNR_ON()
58
#define TNR_OFF()
59
 
60
#define diag_printf printf
61
 
62
void
63
pexit(char *s)
64
{
65
    perror(s);
66
    exit(1);
67
}
68
 
69
 
70
#define SOURCE_PORT 9990
71
#define SINK_PORT   9991
72
 
73
#define MAX_BUF 8192
74
static unsigned char data_buf[MAX_BUF];
75
 
76
struct test_params {
77
    long nbufs;
78
    long bufsize;
79
    long load;
80
};
81
 
82
struct test_status {
83
    long ok;
84
};
85
 
86
int max( int a, int b ) { return (a>b)?a:b; }
87
int min( int a, int b ) { return (a<b)?a:b; }
88
 
89
 
90
int
91
do_read(int s, void *_buf, int len)
92
{
93
    int total, slen, rlen;
94
    unsigned char *buf = (unsigned char *)_buf;
95
    total = 0;
96
    rlen = len;
97
    while (total < len) {
98
        slen = read(s, buf, rlen);
99
        if (slen != rlen) {
100
            if (slen < 0) {
101
                diag_printf("Error after reading %d bytes\n", total);
102
                return -1;
103
            }
104
            rlen -= slen;
105
            buf += slen;
106
        }
107
        total += slen;
108
    }
109
    return total;
110
}
111
 
112
int
113
do_write(int s, void *_buf, int len)
114
{
115
    int total, slen, rlen;
116
    unsigned char *buf = (unsigned char *)_buf;
117
    total = 0;
118
    rlen = len;
119
    while (total < len) {
120
        slen = write(s, buf, rlen);
121
        if (slen != rlen) {
122
            if (slen < 0) {
123
                diag_printf("Error after writing %d bytes\n", total);
124
                return -1;
125
            }
126
            rlen -= slen;
127
            buf += slen;
128
        }
129
        total += slen;
130
    }
131
    return total;
132
}
133
 
134
static void
135
echo_test(void * p)
136
{
137
    int s_source, s_sink, e_source, e_sink;
138
    struct sockaddr_in e_source_addr, e_sink_addr, local;
139
    int one = 1;
140
    fd_set in_fds;
141
    int i, num, len;
142
    struct test_params params,nparams;
143
    struct test_status status,nstatus;
144
 
145
    s_source = socket(AF_INET, SOCK_STREAM, 0);
146
    if (s_source < 0) {
147
        pexit("stream socket");
148
    }
149
    if (setsockopt(s_source, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one))) {
150
        pexit("setsockopt /source/ SO_REUSEADDR");
151
    }
152
    memset(&local, 0, sizeof(local));
153
    local.sin_family = AF_INET;
154
//    local.sin_len = sizeof(local);
155
    local.sin_port = ntohs(SOURCE_PORT);
156
    local.sin_addr.s_addr = INADDR_ANY;
157
    if(bind(s_source, (struct sockaddr *) &local, sizeof(local)) < 0) {
158
        pexit("bind /source/ error");
159
    }
160
    listen(s_source, SOMAXCONN);
161
 
162
    s_sink = socket(AF_INET, SOCK_STREAM, 0);
163
    if (s_sink < 0) {
164
        pexit("stream socket");
165
    }
166
    memset(&local, 0, sizeof(local));
167
    local.sin_family = AF_INET;
168
//    local.sin_len = sizeof(local);
169
    local.sin_port = ntohs(SINK_PORT);
170
    local.sin_addr.s_addr = INADDR_ANY;
171
    if(bind(s_sink, (struct sockaddr *) &local, sizeof(local)) < 0) {
172
        pexit("bind /sink/ error");
173
    }
174
    if (setsockopt(s_sink, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one))) {
175
        pexit("setsockopt /sink/ SO_REUSEADDR");
176
    }
177
    listen(s_sink, SOMAXCONN);
178
 
179
    e_source = 0;  e_sink = 0;
180
    while (true) {
181
        // Wait for a connection on either of the ports
182
        FD_ZERO(&in_fds);
183
        FD_SET(s_source, &in_fds);
184
        FD_SET(s_sink, &in_fds);
185
        num = select(max(s_sink,s_source)+1, &in_fds, 0, 0, 0);
186
        if (FD_ISSET(s_source, &in_fds)) {
187
            len = sizeof(e_source_addr);
188
            if ((e_source = accept(s_source, (struct sockaddr *)&e_source_addr, &len)) < 0) {
189
                pexit("accept /source/");
190
            }
191
            diag_printf("SOURCE connection from %s:%d\n",
192
                        inet_ntoa(e_source_addr.sin_addr), ntohs(e_source_addr.sin_port));
193
        }
194
        if (FD_ISSET(s_sink, &in_fds)) {
195
            len = sizeof(e_sink_addr);
196
            if ((e_sink = accept(s_sink, (struct sockaddr *)&e_sink_addr, &len)) < 0) {
197
                pexit("accept /sink/");
198
            }
199
            diag_printf("SINK connection from %s:%d\n",
200
                        inet_ntoa(e_sink_addr.sin_addr), ntohs(e_sink_addr.sin_port));
201
        }
202
        // Continue with test once a connection is established in both directions
203
        if ((e_source != 0) && (e_sink != 0)) {
204
            break;
205
        }
206
    }
207
 
208
    // Wait for "source" to tell us the testing paramters
209
    if (do_read(e_source, &nparams, sizeof(nparams)) != sizeof(nparams)) {
210
        pexit("Can't read initialization parameters");
211
    }
212
 
213
    params.nbufs = ntohl(nparams.nbufs);
214
    params.bufsize = ntohl(nparams.bufsize);
215
    params.load = ntohl(nparams.load);
216
 
217
    diag_printf("Using %d buffers of %d bytes each, %d%% background load\n",
218
                params.nbufs, params.bufsize, params.load);
219
 
220
    // Tell the sink what the parameters are
221
    if (do_write(e_sink, &nparams, sizeof(nparams)) != sizeof(nparams)) {
222
        pexit("Can't write initialization parameters");
223
    }
224
 
225
    status.ok = 1;
226
    nstatus.ok = htonl(status.ok);
227
 
228
    // Tell the "source" to start - we're all connected and ready to go!
229
    if (do_write(e_source, &nstatus, sizeof(nstatus)) != sizeof(nstatus)) {
230
        pexit("Can't send ACK to 'source' host");
231
    }
232
 
233
    TNR_ON();
234
 
235
    // Echo the data from the source to the sink hosts
236
    for (i = 0;  i < params.nbufs;  i++) {
237
        if ((len = do_read(e_source, data_buf, params.bufsize)) != params.bufsize) {
238
            TNR_OFF();
239
            diag_printf("Can't read buf #%d: ", i+1);
240
            if (len < 0) {
241
                perror("I/O error");
242
            } else {
243
                diag_printf("short read - only %d bytes\n", len);
244
            }
245
            TNR_ON();
246
        }
247
        if ((len = do_write(e_sink, data_buf, params.bufsize)) != params.bufsize) {
248
            TNR_OFF();
249
            diag_printf("Can't write buf #%d: ", i+1);
250
            if (len < 0) {
251
                perror("I/O error");
252
            } else {
253
                diag_printf("short write - only %d bytes\n", len);
254
            }
255
            TNR_ON();
256
        }
257
    }
258
 
259
    TNR_OFF();
260
 
261
    // Wait for the data to drain and the "sink" to tell us all is OK.
262
    if (do_read(e_sink, &status, sizeof(status)) != sizeof(status)) {
263
        pexit("Can't receive ACK from 'sink' host");
264
    }
265
 
266
}
267
 
268
 
269
int
270
main(int argc, char *argv[])
271
{
272
    echo_test(argv[1]);
273
    return 0;
274
}
275
 
276
 
277
// EOF
278
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.