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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [net/] [common/] [current/] [tests/] [tcp_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/tcp_echo.c
4
//
5
//      Simple TCP throughput test - echo component
6
//
7
//==========================================================================
8
// ####BSDALTCOPYRIGHTBEGIN####                                             
9
// -------------------------------------------                              
10
// Portions of this software may have been derived from FreeBSD, OpenBSD,   
11
// or other sources, and if so are covered by the appropriate copyright     
12
// and license included herein.                                             
13
// -------------------------------------------                              
14
// ####BSDALTCOPYRIGHTEND####                                               
15
//==========================================================================
16
//#####DESCRIPTIONBEGIN####
17
//
18
// Author(s):    gthomas
19
// Contributors: gthomas
20
// Date:         2000-01-10
21
// Purpose:      
22
// Description:  This is the middle part of a three part test.  The idea is
23
//   to test the throughput of box in a configuration like this:
24
//
25
//      +------+   port   +----+     port    +----+
26
//      |SOURCE|=========>|ECHO|============>|SINK|
27
//      +------+   9990   +----+     9991    +----+
28
// 
29
//
30
//####DESCRIPTIONEND####
31
//
32
//==========================================================================
33
 
34
#include <pkgconf/system.h>
35
#include <pkgconf/net.h>
36
 
37
#ifdef CYGBLD_DEVS_ETH_DEVICE_H    // Get the device config if it exists
38
#include CYGBLD_DEVS_ETH_DEVICE_H  // May provide CYGTST_DEVS_ETH_TEST_NET_REALTIME
39
#endif
40
 
41
#ifdef CYGPKG_NET_TESTS_USE_RT_TEST_HARNESS // do we use the rt test?
42
# ifdef CYGTST_DEVS_ETH_TEST_NET_REALTIME // Get the test ancilla if it exists
43
#  include CYGTST_DEVS_ETH_TEST_NET_REALTIME
44
# endif
45
#endif
46
 
47
 
48
// Fill in the blanks if necessary
49
#ifndef TNR_OFF
50
# define TNR_OFF()
51
#endif
52
#ifndef TNR_ON
53
# define TNR_ON()
54
#endif
55
#ifndef TNR_INIT
56
# define TNR_INIT()
57
#endif
58
#ifndef TNR_PRINT_ACTIVITY
59
# define TNR_PRINT_ACTIVITY()
60
#endif
61
 
62
 
63
// Network throughput test code
64
 
65
#include <network.h>
66
 
67
static __inline__ unsigned int
68
max(unsigned int m, unsigned int n)
69
{
70
    return m > n ? m : n;
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
#ifndef CYGPKG_LIBC_STDIO
90
#define perror(s) diag_printf(#s ": %s\n", strerror(errno))
91
#endif
92
 
93
#define STACK_SIZE (CYGNUM_HAL_STACK_SIZE_TYPICAL + 0x1000)
94
static char stack[STACK_SIZE];
95
static cyg_thread thread_data;
96
static cyg_handle_t thread_handle;
97
 
98
// Background load stuff
99
#define NUM_LOAD_THREADS         20 // Get 5% granularity
100
#define IDLE_THREAD_PRIORITY     CYGPKG_NET_THREAD_PRIORITY+3
101
#define LOAD_THREAD_PRIORITY     CYGPKG_NET_THREAD_PRIORITY-3
102
#define MAIN_THREAD_PRIORITY     CYGPKG_NET_THREAD_PRIORITY-4
103
#define DESIRED_BACKGROUND_LOAD  50 // should be accurate enough over range
104
 
105
// starting points for load calculation
106
#define MAX_LOAD_THREAD_LEVEL    100
107
#define MIN_LOAD_THREAD_LEVEL    0
108
 
109
static char         idle_thread_stack[STACK_SIZE];
110
static cyg_thread   idle_thread_data;
111
static cyg_handle_t idle_thread_handle;
112
static cyg_sem_t    idle_thread_sem;
113
volatile static long long    idle_thread_count;
114
static char         load_thread_stack[NUM_LOAD_THREADS][STACK_SIZE];
115
static cyg_thread   load_thread_data[NUM_LOAD_THREADS];
116
static cyg_handle_t load_thread_handle[NUM_LOAD_THREADS];
117
static cyg_sem_t    load_thread_sem[NUM_LOAD_THREADS];
118
static long         load_thread_level;
119
static void calibrate_load(int load);
120
static void start_load(int load);
121
static void do_some_random_computation(int p,int id);
122
#define abs(n) ((n) < 0 ? -(n) : (n))
123
 
124
static long long no_load_idle_count_1_second;
125
 
126
extern void
127
cyg_test_exit(void);
128
 
129
void
130
pexit(char *s)
131
{
132
    TNR_OFF();
133
    perror(s);
134
    cyg_test_exit();
135
}
136
 
137
int
138
do_read(int s, void *_buf, int len)
139
{
140
    int total, slen, rlen;
141
    unsigned char *buf = (unsigned char *)_buf;
142
    total = 0;
143
    rlen = len;
144
    while (total < len) {
145
        slen = read(s, buf, rlen);
146
        if (slen != rlen) {
147
            if (slen < 0) {
148
                diag_printf("Error after reading %d bytes\n", total);
149
                return -1;
150
            }
151
            rlen -= slen;
152
            buf += slen;
153
        }
154
        total += slen;
155
    }
156
    return total;
157
}
158
 
159
int
160
do_write(int s, void *_buf, int len)
161
{
162
    int total, slen, rlen;
163
    unsigned char *buf = (unsigned char *)_buf;
164
    total = 0;
165
    rlen = len;
166
    while (total < len) {
167
        slen = write(s, buf, rlen);
168
        if (slen != rlen) {
169
            if (slen < 0) {
170
                diag_printf("Error after writing %d bytes\n", total);
171
                return -1;
172
            }
173
            rlen -= slen;
174
            buf += slen;
175
        }
176
        total += slen;
177
    }
178
    return total;
179
}
180
 
181
//
182
// This function is called to calibrate the "background load" which can be
183
// applied during testing.  It will be called before any commands from the
184
// host are managed.
185
//
186
static void
187
calibrate_load(int desired_load)
188
{
189
    long long no_load_idle, load_idle;
190
    int percent_load;
191
    int high, low;
192
 
193
    // Set limits
194
    high = MAX_LOAD_THREAD_LEVEL;
195
    low = MIN_LOAD_THREAD_LEVEL;
196
 
197
    // Compute the "no load" idle value
198
    idle_thread_count = 0;
199
    cyg_semaphore_post(&idle_thread_sem);  // Start idle thread
200
    cyg_thread_delay(1*100);               // Pause for one second
201
    cyg_semaphore_wait(&idle_thread_sem);  // Stop idle thread
202
    no_load_idle = idle_thread_count;
203
    diag_printf("No load = %d\n", (int)idle_thread_count);
204
 
205
    // First ensure that the HIGH level is indeed higher
206
    while (true) {
207
        load_thread_level = high;
208
        start_load(desired_load);              // Start up a given load
209
        idle_thread_count = 0;
210
        cyg_semaphore_post(&idle_thread_sem);  // Start idle thread
211
        cyg_thread_delay(1*100);               // Pause for one second
212
        cyg_semaphore_wait(&idle_thread_sem);  // Stop idle thread
213
        load_idle = idle_thread_count;
214
        start_load(0);                         // Shut down background load
215
        percent_load = 100 - ((load_idle * 100) / no_load_idle);
216
        diag_printf("High Load[%ld] = %d => %d%%\n", load_thread_level,
217
                    (int)idle_thread_count, percent_load);
218
        if ( percent_load > desired_load )
219
            break; // HIGH level is indeed higher
220
        low = load_thread_level; // known to be lower
221
        high *= 2; // else double it and try again
222
    }
223
 
224
    // Now chop down to the level required
225
    while (true) {
226
        load_thread_level = (high + low) / 2;
227
        start_load(desired_load);              // Start up a given load
228
        idle_thread_count = 0;
229
        cyg_semaphore_post(&idle_thread_sem);  // Start idle thread
230
        cyg_thread_delay(1*100);               // Pause for one second
231
        cyg_semaphore_wait(&idle_thread_sem);  // Stop idle thread
232
        load_idle = idle_thread_count;
233
        start_load(0);                         // Shut down background load
234
        percent_load = 100 - ((load_idle * 100) / no_load_idle);
235
        diag_printf("Load[%ld] = %d => %d%%\n", load_thread_level,
236
                    (int)idle_thread_count, percent_load);
237
        if (((high-low) <= 1) || (abs(desired_load-percent_load) <= 2)) break;
238
        if (percent_load < desired_load) {
239
            low = load_thread_level;
240
        } else {
241
            high = load_thread_level;
242
        }
243
    }
244
 
245
    // Now we are within a few percent of the target; scale the load
246
    // factor to get a better fit, and test it, print the answer.
247
    load_thread_level *= desired_load;
248
    load_thread_level /= percent_load;
249
    start_load(desired_load);              // Start up a given load
250
    idle_thread_count = 0;
251
    cyg_semaphore_post(&idle_thread_sem);  // Start idle thread
252
    cyg_thread_delay(1*100);               // Pause for one second
253
    cyg_semaphore_wait(&idle_thread_sem);  // Stop idle thread
254
    load_idle = idle_thread_count;
255
    start_load(0);                         // Shut down background load
256
    percent_load = 100 - ((load_idle * 100) / no_load_idle);
257
    diag_printf("Final load[%ld] = %d => %d%%\n", load_thread_level,
258
                (int)idle_thread_count, percent_load);
259
    no_load_idle_count_1_second = no_load_idle;
260
}
261
 
262
//
263
// This function is called to set up a load level of 'load' percent (given
264
// as a whole number, e.g. start_load(20) would mean initiate a background
265
// load of 20%, leaving the cpu 80% idle).
266
//
267
static void
268
start_load(int load)
269
{
270
    static int prev_load = 0;
271
    int i;
272
    if (load == 0) {
273
        diag_printf("Set no background load\n");
274
        if (prev_load == 0) return;  // Nothing out there to stop
275
        for (i = 0;  i < prev_load * NUM_LOAD_THREADS/100;  i++) {
276
            cyg_semaphore_wait(&load_thread_sem[i]);
277
        }
278
        prev_load = 0;
279
    } else {
280
        diag_printf("Set background load = %d%% starting %d threads\n",
281
                    load, load * NUM_LOAD_THREADS/100 );
282
        for (i = 0;  i < load * NUM_LOAD_THREADS/100;  i++) {
283
            cyg_semaphore_post(&load_thread_sem[i]);
284
        }
285
        prev_load = load;
286
    }
287
}
288
 
289
//
290
// These thread(s) do some amount of "background" computing.  This is used
291
// to simulate a given load level.  They need to be run at a higher priority 
292
// than the network code itself.
293
//
294
// Like the "idle" thread, they run as long as their "switch" (aka semaphore)
295
// is enabled.
296
//
297
void
298
net_load(cyg_addrword_t who)
299
{
300
    int i;
301
    while (true) {
302
        cyg_semaphore_wait(&load_thread_sem[who]);
303
        for (i = 0;  i < load_thread_level;  i++) {
304
            do_some_random_computation(i,who);
305
        }
306
        cyg_thread_delay(1);  // Wait until the next 'tick'
307
        cyg_semaphore_post(&load_thread_sem[who]);
308
    }
309
}
310
 
311
//
312
// Some arbitrary computation, designed to use up the CPU and cause associated
313
// cache "thrash" behaviour - part of background load modelling.
314
//
315
static void
316
do_some_random_computation(int p,int id)
317
{
318
    // Just something that might be "hard"
319
#if 0
320
    {
321
        volatile double x;
322
        x = ((p * 10) * 3.14159) / 180.0;  // radians
323
    }
324
#endif
325
#if 1
326
    {
327
        static int footle[0x10001];
328
        static int counter = 0;
329
        register int i;
330
 
331
        i = (p << 8) + id + counter++;
332
        i &= 0xffff;
333
        footle[ i+1 ] += footle[ i ] + 1;
334
    }
335
#endif
336
}
337
 
338
//
339
// This thread does nothing but count.  It will be allowed to count
340
// as long as the semaphore is "free".  
341
//
342
void
343
net_idle(cyg_addrword_t param)
344
{
345
    while (true) {
346
        cyg_semaphore_wait(&idle_thread_sem);
347
        idle_thread_count++;
348
        cyg_semaphore_post(&idle_thread_sem);
349
    }
350
}
351
 
352
static void
353
echo_test(cyg_addrword_t p)
354
{
355
    int s_source, s_sink, e_source, e_sink;
356
    struct sockaddr_in e_source_addr, e_sink_addr, local;
357
    int one = 1;
358
    fd_set in_fds;
359
    int i, num;
360
    socklen_t len;
361
    struct test_params params,nparams;
362
    struct test_status status,nstatus;
363
 
364
    cyg_tick_count_t starttime, stoptime;
365
 
366
    s_source = socket(AF_INET, SOCK_STREAM, 0);
367
    if (s_source < 0) {
368
        pexit("stream socket");
369
    }
370
    if (setsockopt(s_source, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one))) {
371
        pexit("setsockopt /source/ SO_REUSEADDR");
372
    }
373
    if (setsockopt(s_source, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one))) {
374
        pexit("setsockopt /source/ SO_REUSEPORT");
375
    }
376
    memset(&local, 0, sizeof(local));
377
    local.sin_family = AF_INET;
378
    local.sin_len = sizeof(local);
379
    local.sin_port = ntohs(SOURCE_PORT);
380
    local.sin_addr.s_addr = INADDR_ANY;
381
    if(bind(s_source, (struct sockaddr *) &local, sizeof(local)) < 0) {
382
        pexit("bind /source/ error");
383
    }
384
    listen(s_source, SOMAXCONN);
385
 
386
    s_sink = socket(AF_INET, SOCK_STREAM, 0);
387
    if (s_sink < 0) {
388
        pexit("stream socket");
389
    }
390
    memset(&local, 0, sizeof(local));
391
    local.sin_family = AF_INET;
392
    local.sin_len = sizeof(local);
393
    local.sin_port = ntohs(SINK_PORT);
394
    local.sin_addr.s_addr = INADDR_ANY;
395
    if(bind(s_sink, (struct sockaddr *) &local, sizeof(local)) < 0) {
396
        pexit("bind /sink/ error");
397
    }
398
    if (setsockopt(s_sink, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one))) {
399
        pexit("setsockopt /sink/ SO_REUSEADDR");
400
    }
401
    if (setsockopt(s_sink, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one))) {
402
        pexit("setsockopt /sink/ SO_REUSEPORT");
403
    }
404
    listen(s_sink, SOMAXCONN);
405
 
406
    e_source = 0;  e_sink = 0;
407
    while (true) {
408
        // Wait for a connection on either of the ports
409
        FD_ZERO(&in_fds);
410
        FD_SET(s_source, &in_fds);
411
        FD_SET(s_sink, &in_fds);
412
        num = select(max(s_sink,s_source)+1, &in_fds, 0, 0, 0);
413
        if (FD_ISSET(s_source, &in_fds)) {
414
            len = sizeof(e_source_addr);
415
            if ((e_source = accept(s_source, (struct sockaddr *)&e_source_addr, &len)) < 0) {
416
                pexit("accept /source/");
417
            }
418
            diag_printf("SOURCE connection from %s:%d\n",
419
                        inet_ntoa(e_source_addr.sin_addr), ntohs(e_source_addr.sin_port));
420
        }
421
        if (FD_ISSET(s_sink, &in_fds)) {
422
            len = sizeof(e_sink_addr);
423
            if ((e_sink = accept(s_sink, (struct sockaddr *)&e_sink_addr, &len)) < 0) {
424
                pexit("accept /sink/");
425
            }
426
            diag_printf("SINK connection from %s:%d\n",
427
                        inet_ntoa(e_sink_addr.sin_addr), ntohs(e_sink_addr.sin_port));
428
        }
429
        // Continue with test once a connection is established in both directions
430
        if ((e_source != 0) && (e_sink != 0)) {
431
            break;
432
        }
433
    }
434
 
435
    // Wait for "source" to tell us the testing paramters
436
    if (do_read(e_source, &nparams, sizeof(nparams)) != sizeof(nparams)) {
437
        pexit("Can't read initialization parameters");
438
    }
439
 
440
    params.nbufs = ntohl(nparams.nbufs);
441
    params.bufsize = ntohl(nparams.bufsize);
442
    params.load = ntohl(nparams.load);
443
 
444
    diag_printf("Using %ld buffers of %ld bytes each, %ld%% background load\n",
445
                params.nbufs, params.bufsize, params.load);
446
 
447
    // Tell the sink what the parameters are
448
    if (do_write(e_sink, &nparams, sizeof(nparams)) != sizeof(nparams)) {
449
        pexit("Can't write initialization parameters");
450
    }
451
 
452
    status.ok = 1;
453
    nstatus.ok = htonl(status.ok);
454
 
455
    // Tell the "source" to start - we're all connected and ready to go!
456
    if (do_write(e_source, &nstatus, sizeof(nstatus)) != sizeof(nstatus)) {
457
        pexit("Can't send ACK to 'source' host");
458
    }
459
 
460
    idle_thread_count = 0;
461
    cyg_semaphore_post(&idle_thread_sem);  // Start idle thread
462
    starttime = cyg_current_time();
463
    start_load(params.load);
464
 
465
    TNR_ON();
466
 
467
    // Echo the data from the source to the sink hosts
468
    for (i = 0;  i < params.nbufs;  i++) {
469
        if ((len = do_read(e_source, data_buf, params.bufsize)) != params.bufsize) {
470
            TNR_OFF();
471
            diag_printf("Can't read buf #%d: ", i+1);
472
            if (len < 0) {
473
                perror("I/O error");
474
            } else {
475
                diag_printf("short read - only %d bytes\n", len);
476
            }
477
            TNR_ON();
478
        }
479
        if ((len = do_write(e_sink, data_buf, params.bufsize)) != params.bufsize) {
480
            TNR_OFF();
481
            diag_printf("Can't write buf #%d: ", i+1);
482
            if (len < 0) {
483
                perror("I/O error");
484
            } else {
485
                diag_printf("short write - only %d bytes\n", len);
486
            }
487
            TNR_ON();
488
        }
489
    }
490
 
491
    TNR_OFF();
492
 
493
    // Wait for the data to drain and the "sink" to tell us all is OK.
494
    if (do_read(e_sink, &status, sizeof(status)) != sizeof(status)) {
495
        pexit("Can't receive ACK from 'sink' host");
496
    }
497
 
498
    start_load(0);
499
    cyg_semaphore_wait(&idle_thread_sem);  // Stop idle thread
500
    stoptime = cyg_current_time();
501
    stoptime -= starttime; // time taken in cS
502
    // expected idle loops in that time period for an idle system:
503
    starttime = no_load_idle_count_1_second * stoptime / 100;
504
    diag_printf( "%d ticks elapsed, %d kloops predicted for an idle system\n",
505
                 (int)stoptime, (int)(starttime/1000) );
506
    diag_printf( "actual kloops %d, CPU was %d%% idle during transfer\n",
507
                 (int)(idle_thread_count/1000),
508
                 (int)(idle_thread_count * 100 / starttime) );
509
 
510
    // Now examine how close that loading actually was:
511
    start_load(params.load);              // Start up a given load
512
    idle_thread_count = 0;
513
    cyg_semaphore_post(&idle_thread_sem);  // Start idle thread
514
    cyg_thread_delay(1*100);               // Pause for one second
515
    cyg_semaphore_wait(&idle_thread_sem);  // Stop idle thread
516
    start_load(0);                         // Shut down background load
517
    i = 100 - ((idle_thread_count * 100) / no_load_idle_count_1_second );
518
    diag_printf("Final load[%ld] = %d => %d%%\n", load_thread_level,
519
                (int)idle_thread_count, i);
520
 
521
//#ifdef CYGDBG_USE_ASSERTS
522
#ifdef CYGDBG_NET_TIMING_STATS 
523
    {
524
        extern void show_net_times(void);
525
        show_net_times();
526
    }
527
#endif
528
//#endif
529
}
530
 
531
void
532
net_test(cyg_addrword_t param)
533
{
534
    diag_printf("Start TCP test - ECHO mode\n");
535
    init_all_network_interfaces();
536
    calibrate_load(DESIRED_BACKGROUND_LOAD);
537
    TNR_INIT();
538
#ifdef CYGPKG_SNMPAGENT
539
    {
540
        extern void cyg_net_snmp_init(void);
541
        cyg_net_snmp_init();
542
    }
543
#endif
544
    echo_test(param);
545
    TNR_PRINT_ACTIVITY();
546
    cyg_test_exit();
547
}
548
 
549
void
550
cyg_start(void)
551
{
552
    int i;
553
    // Create a main thread which actually runs the test
554
    cyg_thread_create(MAIN_THREAD_PRIORITY, // Priority
555
                      net_test,             // entry
556
                      0,                    // entry parameter
557
                      "Network test",       // Name
558
                      &stack[0],            // Stack
559
                      STACK_SIZE,           // Size
560
                      &thread_handle,       // Handle
561
                      &thread_data          // Thread data structure
562
            );
563
    cyg_thread_resume(thread_handle);  // Start it
564
    // Create the idle thread environment
565
    cyg_semaphore_init(&idle_thread_sem, 0);
566
    cyg_thread_create(IDLE_THREAD_PRIORITY,     // Priority
567
                      net_idle,                 // entry
568
                      0,                        // entry parameter
569
                      "Network idle",           // Name
570
                      &idle_thread_stack[0],    // Stack
571
                      STACK_SIZE,               // Size
572
                      &idle_thread_handle,      // Handle
573
                      &idle_thread_data         // Thread data structure
574
            );
575
    cyg_thread_resume(idle_thread_handle);      // Start it
576
    // Create the load threads and their environment(s)
577
    for (i = 0;  i < NUM_LOAD_THREADS;  i++) {
578
        cyg_semaphore_init(&load_thread_sem[i], 0);
579
        cyg_thread_create(LOAD_THREAD_PRIORITY,     // Priority
580
                          net_load,                 // entry
581
                          i,                        // entry parameter
582
                          "Background load",        // Name
583
                          &load_thread_stack[i][0], // Stack
584
                          STACK_SIZE,               // Size
585
                          &load_thread_handle[i],   // Handle
586
                          &load_thread_data[i]      // Thread data structure
587
            );
588
        cyg_thread_resume(load_thread_handle[i]);   // Start it
589
    }
590
    cyg_scheduler_start();
591
}
592
 
593
// EOF tcp_echo.c

powered by: WebSVN 2.1.0

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