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

Subversion Repositories openrisc

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

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

Line No. Rev Author Line
1 27 unneback
//==========================================================================
2
//
3
//      tests/tcp_sink.c
4
//
5
//      Simple TCP throughput test - sink component
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
// Network throughput test code
39
 
40
#undef _KERNEL
41
#include <stdlib.h>
42
#include <unistd.h>
43
#include <stdio.h>
44
 
45
#include <sys/param.h>
46
#include <sys/socket.h>
47
#include <sys/ioctl.h>
48
#include <sys/errno.h>
49
#include <sys/time.h>
50
 
51
#include <net/if.h>
52
#include <netinet/in.h>
53
#include <netinet/ip.h>
54
#include <netinet/ip_icmp.h>
55
#include <net/route.h>
56
 
57
#include <netdb.h>
58
 
59
#define SOURCE_PORT 9990
60
#define SINK_PORT   9991
61
 
62
struct test_params {
63
    long nbufs;
64
    long bufsize;
65
    long load;
66
};
67
 
68
struct test_status {
69
    long ok;
70
};
71
 
72
#define NUM_BUF 128
73
#define MAX_BUF 32 * 8192
74
static unsigned char data_buf[MAX_BUF];
75
 
76
void
77
pexit(char *s)
78
{
79
    perror(s);
80
    exit(1);
81
}
82
 
83
void
84
show_results(struct timeval *start, struct timeval *end,
85
             int nbufs, int buflen)
86
{
87
    struct timeval tot_time;
88
    long tot_bytes = nbufs * buflen;
89
    double real_time, thru;
90
    timersub(end, start, &tot_time);
91
    printf("SINK complete - %d bufs of %d bytes in %ld.%02ld seconds",
92
           nbufs, buflen,
93
           tot_time.tv_sec, tot_time.tv_usec / 10000);
94
    real_time = tot_time.tv_sec + ((tot_time.tv_usec / 10000) * .01);
95
    // Compute bytes / second (rounded up)
96
    thru = tot_bytes / real_time;
97
    // Convert to Mb / second
98
    printf(" - %.2f KB/S", thru / 1024.0);
99
    printf(" - %.4f Mbit/S (M = 10^6)", thru * 8.0 / 1000000.0);
100
    printf("\n");
101
}
102
 
103
int
104
do_read(int s, unsigned char *buf, int len)
105
{
106
    int total, slen, rlen;
107
    total = 0;
108
    rlen = len;
109
    while (total < len) {
110
        slen = read(s, buf, rlen);
111
        if (slen != rlen) {
112
            if (slen < 0) {
113
                printf("Error after reading %d bytes\n", total);
114
                return -1;
115
            }
116
            rlen -= slen;
117
            buf += slen;
118
        }
119
        total += slen;
120
    }
121
    return total;
122
}
123
 
124
int
125
do_write(int s, unsigned char *buf, int len)
126
{
127
    int total, slen, rlen;
128
    total = 0;
129
    rlen = len;
130
    while (total < len) {
131
        slen = write(s, buf, rlen);
132
        if (slen != rlen) {
133
            if (slen < 0) {
134
                printf("Error after writing %d bytes\n", total);
135
                return -1;
136
            }
137
            rlen -= slen;
138
            buf += slen;
139
        }
140
        total += slen;
141
    }
142
    return total;
143
}
144
 
145
static void
146
sink_test(char *echo_node)
147
{
148
    int s_sink;
149
    struct sockaddr_in slave, local;
150
    int one = 1;
151
    int len;
152
    struct hostent *host;
153
    struct test_params params, nparams;
154
    struct test_status status, nstatus;
155
    struct timeval start_time, end_time;
156
    int i;
157
 
158
    printf("Start TCP test - SINK mode to %s\n", echo_node);
159
 
160
    host = gethostbyname(echo_node);
161
    if (host == (struct hostent *)NULL) {
162
        pexit("gethostbyname");
163
    }
164
 
165
    memset(&slave, 0, sizeof(slave));
166
    slave.sin_family = AF_INET;
167
#ifdef __ECOS
168
    slave.sin_len = sizeof(slave);
169
#endif
170
    slave.sin_port = htons(SINK_PORT);
171
    memcpy(&slave.sin_addr.s_addr, host->h_addr, host->h_length);
172
 
173
    s_sink = socket(AF_INET, SOCK_STREAM, 0);
174
    if (s_sink < 0) {
175
        pexit("stream socket");
176
    }
177
    if (setsockopt(s_sink, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one))) {
178
        pexit("setsockopt /sink/ SO_REUSEADDR");
179
    }
180
    memset(&local, 0, sizeof(local));
181
    local.sin_family = AF_INET;
182
#ifdef __ECOS
183
    local.sin_len = sizeof(local);
184
#endif
185
    local.sin_port = INADDR_ANY;
186
    local.sin_addr.s_addr = INADDR_ANY;
187
    if(bind(s_sink, (struct sockaddr *) &local, sizeof(local)) < 0) {
188
        pexit("bind /sink/ error");
189
    }
190
 
191
    if (connect(s_sink, (struct sockaddr *)&slave, sizeof(slave)) < 0) {
192
        pexit("Can't connect to target");
193
    }
194
 
195
    // Get testing paramters from middleman
196
    if (do_read(s_sink, (unsigned char *)&nparams, sizeof(nparams))
197
                  != sizeof(nparams)) {
198
        pexit("Can't read initialization parameters");
199
    }
200
 
201
    params.nbufs = ntohl(nparams.nbufs);
202
    params.bufsize = ntohl(nparams.bufsize);
203
    params.load = ntohl(nparams.load);
204
 
205
    printf("Using %ld buffers of %ld bytes each\n", params.nbufs, params.bufsize);
206
 
207
    // Actual test
208
    gettimeofday(&start_time, 0);
209
    for (i = 0;  i < params.nbufs;  i++) {
210
        if ((len = do_read(s_sink, data_buf, params.bufsize)) != params.bufsize) {
211
            printf("Error reading buffer #%d:", i+1);
212
            if (len < 0) {
213
                perror("can't read data");
214
            } else {
215
                printf(" short data, only read %d bytes\n", len);
216
            }
217
        }
218
    }
219
    gettimeofday(&end_time, 0);
220
    show_results(&start_time, &end_time, params.nbufs, params.bufsize);
221
 
222
    // Tell the middleman
223
 
224
    nstatus.ok = htonl(status.ok);
225
 
226
    if (do_write(s_sink, (unsigned char *)&nstatus, sizeof(nstatus))
227
                        != sizeof(nstatus)) {
228
        pexit("Can't send ACK to 'echo' host");
229
    }
230
}
231
 
232
int
233
main(int argc, char *argv[])
234
{
235
    sink_test(argv[1]);
236
    return 0;
237
}
238
 

powered by: WebSVN 2.1.0

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