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

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [net/] [common/] [v2_0/] [tests/] [linux_echo.c] - Blame information for rev 174

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 27 unneback
//==========================================================================
2
//
3
//      tests/linux_echo.c
4
//
5
//      Simple TCP throughput test - echo component FOR LINUX HOST
6
//      * CAUTION: host, i.e. non eCos, only *
7
//
8
//==========================================================================
9
//####BSDCOPYRIGHTBEGIN####
10
//
11
// -------------------------------------------
12
//
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.
15
//
16
// -------------------------------------------
17
//
18
//####BSDCOPYRIGHTEND####
19
//==========================================================================
20
//#####DESCRIPTIONBEGIN####
21
//
22
// Author(s):    gthomas
23
// Contributors: gthomas
24
// Date:         2000-01-10
25
// Purpose:      
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:
28
//
29
//      +------+   port   +----+     port    +----+
30
//      |SOURCE|=========>|ECHO|============>|SINK|
31
//      +------+   9990   +----+     9991    +----+
32
// 
33
//
34
//####DESCRIPTIONEND####
35
//
36
//==========================================================================
37
 
38
#undef _KERNEL
39
#include <stdlib.h>
40
#include <unistd.h>
41
#include <stdio.h>
42
 
43
#include <sys/param.h>
44
#include <sys/socket.h>
45
#include <sys/ioctl.h>
46
#include <sys/errno.h>
47
#include <sys/time.h>
48
 
49
#include <net/if.h>
50
#include <netinet/in.h>
51
#include <netinet/ip.h>
52
#include <netinet/ip_icmp.h>
53
#include <net/route.h>
54
 
55
#include <netdb.h>
56
 
57
#define true 1
58
#define false 1
59
 
60
#define TNR_ON()
61
#define TNR_OFF()
62
 
63
#define diag_printf printf
64
 
65
void
66
pexit(char *s)
67
{
68
    perror(s);
69
    exit(1);
70
}
71
 
72
 
73
#define SOURCE_PORT 9990
74
#define SINK_PORT   9991
75
 
76
#define MAX_BUF 8192
77
static unsigned char data_buf[MAX_BUF];
78
 
79
struct test_params {
80
    long nbufs;
81
    long bufsize;
82
    long load;
83
};
84
 
85
struct test_status {
86
    long ok;
87
};
88
 
89
int max( int a, int b ) { return (a>b)?a:b; }
90
int min( int a, int b ) { return (a<b)?a:b; }
91
 
92
 
93
int
94
do_read(int s, void *_buf, int len)
95
{
96
    int total, slen, rlen;
97
    unsigned char *buf = (unsigned char *)_buf;
98
    total = 0;
99
    rlen = len;
100
    while (total < len) {
101
        slen = read(s, buf, rlen);
102
        if (slen != rlen) {
103
            if (slen < 0) {
104
                diag_printf("Error after reading %d bytes\n", total);
105
                return -1;
106
            }
107
            rlen -= slen;
108
            buf += slen;
109
        }
110
        total += slen;
111
    }
112
    return total;
113
}
114
 
115
int
116
do_write(int s, void *_buf, int len)
117
{
118
    int total, slen, rlen;
119
    unsigned char *buf = (unsigned char *)_buf;
120
    total = 0;
121
    rlen = len;
122
    while (total < len) {
123
        slen = write(s, buf, rlen);
124
        if (slen != rlen) {
125
            if (slen < 0) {
126
                diag_printf("Error after writing %d bytes\n", total);
127
                return -1;
128
            }
129
            rlen -= slen;
130
            buf += slen;
131
        }
132
        total += slen;
133
    }
134
    return total;
135
}
136
 
137
static void
138
echo_test(void * p)
139
{
140
    int s_source, s_sink, e_source, e_sink;
141
    struct sockaddr_in e_source_addr, e_sink_addr, local;
142
    int one = 1;
143
    fd_set in_fds;
144
    int i, num, len;
145
    struct test_params params,nparams;
146
    struct test_status status,nstatus;
147
 
148
    s_source = socket(AF_INET, SOCK_STREAM, 0);
149
    if (s_source < 0) {
150
        pexit("stream socket");
151
    }
152
    if (setsockopt(s_source, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one))) {
153
        pexit("setsockopt /source/ SO_REUSEADDR");
154
    }
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");
162
    }
163
    listen(s_source, SOMAXCONN);
164
 
165
    s_sink = socket(AF_INET, SOCK_STREAM, 0);
166
    if (s_sink < 0) {
167
        pexit("stream socket");
168
    }
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");
176
    }
177
    if (setsockopt(s_sink, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one))) {
178
        pexit("setsockopt /sink/ SO_REUSEADDR");
179
    }
180
    listen(s_sink, SOMAXCONN);
181
 
182
    e_source = 0;  e_sink = 0;
183
    while (true) {
184
        // Wait for a connection on either of the ports
185
        FD_ZERO(&in_fds);
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/");
193
            }
194
            diag_printf("SOURCE connection from %s:%d\n",
195
                        inet_ntoa(e_source_addr.sin_addr), ntohs(e_source_addr.sin_port));
196
        }
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/");
201
            }
202
            diag_printf("SINK connection from %s:%d\n",
203
                        inet_ntoa(e_sink_addr.sin_addr), ntohs(e_sink_addr.sin_port));
204
        }
205
        // Continue with test once a connection is established in both directions
206
        if ((e_source != 0) && (e_sink != 0)) {
207
            break;
208
        }
209
    }
210
 
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");
214
    }
215
 
216
    params.nbufs = ntohl(nparams.nbufs);
217
    params.bufsize = ntohl(nparams.bufsize);
218
    params.load = ntohl(nparams.load);
219
 
220
    diag_printf("Using %d buffers of %d bytes each, %d%% background load\n",
221
                params.nbufs, params.bufsize, params.load);
222
 
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");
226
    }
227
 
228
    status.ok = 1;
229
    nstatus.ok = htonl(status.ok);
230
 
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");
234
    }
235
 
236
    TNR_ON();
237
 
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) {
241
            TNR_OFF();
242
            diag_printf("Can't read buf #%d: ", i+1);
243
            if (len < 0) {
244
                perror("I/O error");
245
            } else {
246
                diag_printf("short read - only %d bytes\n", len);
247
            }
248
            TNR_ON();
249
        }
250
        if ((len = do_write(e_sink, data_buf, params.bufsize)) != params.bufsize) {
251
            TNR_OFF();
252
            diag_printf("Can't write buf #%d: ", i+1);
253
            if (len < 0) {
254
                perror("I/O error");
255
            } else {
256
                diag_printf("short write - only %d bytes\n", len);
257
            }
258
            TNR_ON();
259
        }
260
    }
261
 
262
    TNR_OFF();
263
 
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");
267
    }
268
 
269
}
270
 
271
 
272
int
273
main(int argc, char *argv[])
274
{
275
    echo_test(argv[1]);
276
    return 0;
277
}
278
 
279
 
280
// EOF
281
 

powered by: WebSVN 2.1.0

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