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

Subversion Repositories openrisc

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

powered by: WebSVN 2.1.0

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