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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [net/] [common/] [current/] [tests/] [tcp_source.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_source.c
4
//
5
//      Simple TCP throughput test - source 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 1024
70
#define MAX_BUF 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("SOURCE 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
source_test(char *echo_node, int load)
144
{
145
    int s_source;
146
    struct sockaddr_in slave, local;
147
    int one = 1;
148
    int len;
149
    struct hostent *host;
150
    struct test_params params;
151
    struct test_params nparams;
152
    struct test_status status;
153
    struct test_status nstatus;
154
    struct timeval start_time, end_time;
155
    int i;
156
 
157
    printf("Start TCP test - SOURCE mode to %s\n", echo_node);
158
 
159
    host = gethostbyname(echo_node);
160
    if (host == (struct hostent *)NULL) {
161
        pexit("gethostbyname");
162
    }
163
 
164
    memset(&slave, 0, sizeof(slave));
165
    slave.sin_family = AF_INET;
166
#ifdef __ECOS
167
    slave.sin_len = sizeof(slave);
168
#endif
169
    slave.sin_port = htons(SOURCE_PORT);
170
    memcpy(&slave.sin_addr.s_addr, host->h_addr, host->h_length);
171
 
172
    s_source = socket(AF_INET, SOCK_STREAM, 0);
173
    if (s_source < 0) {
174
        pexit("stream socket");
175
    }
176
    if (setsockopt(s_source, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one))) {
177
        pexit("setsockopt /source/ SO_REUSEADDR");
178
    }
179
    memset(&local, 0, sizeof(local));
180
    local.sin_family = AF_INET;
181
#ifdef __ECOS
182
    local.sin_len = sizeof(local);
183
#endif
184
    local.sin_port = INADDR_ANY;
185
    local.sin_addr.s_addr = INADDR_ANY;
186
    if(bind(s_source, (struct sockaddr *) &local, sizeof(local)) < 0) {
187
        pexit("bind /source/ error");
188
    }
189
 
190
    if (connect(s_source, (struct sockaddr *)&slave, sizeof(slave)) < 0) {
191
        pexit("Can't connect to target");
192
    }
193
 
194
    params.nbufs = NUM_BUF;
195
    params.bufsize = MAX_BUF;
196
    params.load = load;
197
 
198
    nparams.nbufs = htonl(params.nbufs);
199
    nparams.bufsize = htonl(params.bufsize);
200
    nparams.load = htonl(params.load);
201
 
202
    if ( do_write(s_source, (unsigned char *)&nparams, sizeof(nparams))
203
                  != sizeof(params)) {
204
        pexit("Can't send initialization parameters");
205
    }
206
    if (do_read(s_source, (unsigned char *)&nstatus, sizeof(nstatus))
207
                  != sizeof(status)) {
208
        pexit("Can't get status from 'echo' client");
209
    }
210
 
211
    status.ok = ntohl(nstatus.ok);
212
 
213
    // Actual test
214
    gettimeofday(&start_time, 0);
215
    for (i = 0;  i < params.nbufs;  i++) {
216
        if ((len = do_write(s_source, data_buf, params.bufsize)) != params.bufsize) {
217
            printf("Error writing buffer #%d:", i+1);
218
            if (len < 0) {
219
                perror("can't write data");
220
            } else {
221
                printf(" short data, only wrote %d bytes\n", len);
222
            }
223
        }
224
    }
225
    gettimeofday(&end_time, 0);
226
    show_results(&start_time, &end_time, params.nbufs, params.bufsize);
227
}
228
 
229
int
230
main(int argc, char *argv[])
231
{
232
    int load = 0;
233
    if (argc > 2) {
234
        load = atoi(argv[2]);
235
    }
236
    source_test(argv[1], load);
237
   return 0;
238
}
239
 

powered by: WebSVN 2.1.0

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