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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [net/] [common/] [current/] [tests/] [nc6_test_master.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/nc6_test_master.c
4
//
5
//      Network characterizations test (master portion) IPv4+IPv6 aware
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:  
23
//              
24
//
25
//####DESCRIPTIONEND####
26
//
27
//==========================================================================
28
 
29
// Network characterization test code - master portion
30
 
31
#include "nc_test_framework.h"
32
 
33
#ifdef __ECOS
34
#ifndef CYGPKG_LIBC_STDIO
35
#define perror(s) diag_printf(#s ": %s\n", strerror(errno))
36
#endif
37
#define STACK_SIZE (CYGNUM_HAL_STACK_SIZE_TYPICAL + 0x1000)
38
static char stack[STACK_SIZE];
39
static cyg_thread thread_data;
40
static cyg_handle_t thread_handle;
41
#endif
42
 
43
struct test_params {
44
    int  argc;
45
    char **argv;
46
};
47
 
48
#define MAX_BUF 32*1024
49
static unsigned char in_buf[MAX_BUF], out_buf[MAX_BUF];
50
 
51
static int test_seq = 1;
52
static long long idle_count;
53
static long      idle_ticks;
54
#define IDLE_TEST_TIME   10
55
 
56
struct pause {
57
    int pause_ticks;
58
    int pause_threshold;
59
};
60
#define LENGTH(x) (sizeof(x)/sizeof(x[0]))
61
 
62
#ifdef __ECOS
63
extern void
64
cyg_test_exit(void);
65
#else
66
void
67
cyg_test_exit(void)
68
{
69
    test_printf("... Done\n");
70
    exit(1);
71
}
72
#endif
73
 
74
#ifdef __ECOS
75
static void
76
test_delay(int ticks)
77
{
78
    cyg_thread_delay(ticks);
79
}
80
 
81
#else
82
 
83
static void
84
test_delay(int ticks)
85
{
86
    usleep(ticks * 10000);
87
}
88
#endif
89
 
90
void
91
pexit(char *s)
92
{
93
    perror(s);
94
    cyg_test_exit();
95
}
96
 
97
#ifdef __ECOS
98
# ifdef CYGPKG_POSIX
99
# #include <pkgconf/posix.h>
100
# endif
101
# ifndef CYGPKG_POSIX_TIMERS
102
int
103
gettimeofday(struct timeval *tv, struct timezone *tz)
104
{
105
    cyg_tick_count_t cur_time;
106
    cur_time = cyg_current_time();
107
    tv->tv_sec = cur_time / 100;
108
    tv->tv_usec = (cur_time % 100) * 10000;
109
}
110
# endif
111
#endif
112
 
113
void
114
show_results(const char *msg, struct timeval *start,
115
             struct timeval *end, int nbufs, int buflen,
116
             int lost, int seq_errors)
117
{
118
    struct timeval tot_time;
119
#ifndef __ECOS
120
    double real_time, thru;
121
    long tot_bytes = nbufs * buflen;
122
#endif
123
    timersub(end, start, &tot_time);
124
    test_printf("%s - %d bufs of %d bytes in %d.%02d seconds",
125
                msg, nbufs, buflen,
126
                tot_time.tv_sec, tot_time.tv_usec / 10000);
127
#ifndef __ECOS
128
    real_time = tot_time.tv_sec + ((tot_time.tv_usec / 10000) * .01);
129
    // Compute bytes / second (rounded up)
130
    thru = tot_bytes / real_time;
131
    // Convert to Mb / second
132
    test_printf(" - %.2f KB/S", thru / 1024.0);
133
    test_printf(" - %.4f Mbit/S (M = 10^6)", thru * 8.0 / 1000000.0);
134
#endif
135
    if (lost) {
136
        test_printf(", %d lost", lost);
137
    }
138
    if (seq_errors) {
139
        test_printf(", %d out of sequence", seq_errors);
140
    }
141
    test_printf("\n");
142
}
143
 
144
void
145
new_test(void)
146
{
147
    test_seq++;
148
}
149
 
150
static int
151
sa_len(struct sockaddr *sa)
152
{
153
    switch (sa->sa_family) {
154
    case AF_INET:
155
        return sizeof(struct sockaddr_in);
156
    case AF_INET6:
157
        return sizeof(struct sockaddr_in6);
158
    default:
159
        printf("Unknown socket type: %d\n", sa->sa_family);
160
        return sizeof(struct sockaddr_storage);
161
    }
162
}
163
 
164
int
165
nc_message(int s, struct nc_request *req,
166
           struct nc_reply *reply, struct sockaddr *slave)
167
{
168
    fd_set fds;
169
    struct timeval timeout;
170
 
171
    req->seq = htonl(test_seq);
172
    if (sendto(s, req, sizeof(*req), 0, slave, sa_len(slave)) < 0) {
173
        perror("sendto");
174
        return false;
175
    }
176
    FD_ZERO(&fds);
177
    FD_SET(s, &fds);
178
    timeout.tv_sec = NC_REPLY_TIMEOUT;
179
    timeout.tv_usec = 0;
180
    if (select(s+1, &fds, 0, 0, &timeout) <= 0) {
181
        test_printf("No response to command\n");
182
        return false;
183
    }
184
    if (recvfrom(s, reply, sizeof(*reply), 0, 0, 0) < 0) {
185
        perror("recvfrom");
186
        return false;
187
    }
188
    if (reply->seq != req->seq) {
189
        test_printf("Response out of order - sent: %d, recvd: %d\n",
190
                    ntohl(req->seq), ntohl(reply->seq));
191
        return false;
192
    }
193
    return true;
194
}
195
 
196
void
197
show_test_results(struct nc_test_results *results)
198
{
199
    if ((ntohl(results->key1) == NC_TEST_RESULT_KEY1) &&
200
        (ntohl(results->key2) == NC_TEST_RESULT_KEY2) &&
201
        (ntohl(results->seq) == test_seq)) {
202
        test_printf("   slave sent %d, recvd %d\n",
203
                    ntohl(results->nsent), ntohl(results->nrecvd));
204
    } else {
205
        test_printf("   ... invalid results - keys: %x/%x, seq: %d/%d\n",
206
                    ntohl(results->key1), ntohl(results->key2),
207
                    ntohl(results->seq), test_seq);
208
    }
209
}
210
 
211
void
212
do_udp_test(int s1, int type, struct sockaddr *slave,
213
            int nbufs, int buflen, int pause_time, int pause_threshold)
214
{
215
    int i, s, td_len, seq, seq_errors, total_packets;
216
    struct sockaddr_storage test_chan_master, test_chan_slave;
217
    struct timeval start_time, end_time;
218
    struct nc_request req;
219
    struct nc_reply reply;
220
    struct nc_test_results results;
221
    struct nc_test_data *tdp;
222
    fd_set fds;
223
    struct timeval timeout;
224
    int lost_packets = 0;
225
    int need_send, need_recv;
226
    const char *type_name;
227
    int pkt_ctr = 0;
228
 
229
    need_recv = true;  need_send = true;  type_name = "UDP echo";
230
    switch (type) {
231
    case NC_REQUEST_UDP_RECV:
232
        need_recv = false;
233
        need_send = true;
234
        type_name = "UDP recv";
235
        break;
236
    case NC_REQUEST_UDP_SEND:
237
        need_recv = true;
238
        need_send = false;
239
        type_name = "UDP send";
240
        break;
241
    case NC_REQUEST_UDP_ECHO:
242
        break;
243
    }
244
 
245
    new_test();
246
    req.type = htonl(type);
247
    req.nbufs = htonl(nbufs);
248
    req.buflen = htonl(buflen);
249
    req.slave_port = htonl(NC_TESTING_SLAVE_PORT);
250
    req.master_port = htonl(NC_TESTING_MASTER_PORT);
251
    nc_message(s1, &req, &reply, slave);
252
    if (reply.response != ntohl(NC_REPLY_ACK)) {
253
        test_printf("Slave denied %s [%d,%d] test\n", type_name, nbufs, buflen);
254
        return;
255
    }
256
 
257
    s = socket(slave->sa_family, SOCK_DGRAM, 0);
258
    if (s < 0) {
259
        pexit("datagram socket");
260
    }
261
 
262
    memset(&test_chan_master, 0, sizeof(test_chan_master));
263
    ((struct sockaddr *)&test_chan_master)->sa_family = slave->sa_family;
264
    memcpy(&test_chan_slave, slave, sa_len(slave));
265
#ifndef __linux
266
    ((struct sockaddr *)&test_chan_master)->sa_len = slave->sa_len;
267
#endif
268
    switch (slave->sa_family) {
269
    case AF_INET:
270
        ((struct sockaddr_in *)&test_chan_master)->sin_addr.s_addr = htonl(INADDR_ANY);
271
        ((struct sockaddr_in *)&test_chan_master)->sin_port = htons(ntohl(req.master_port));
272
        ((struct sockaddr_in *)&test_chan_slave)->sin_port = htons(ntohl(req.slave_port));
273
        break;
274
    case AF_INET6:
275
        ((struct sockaddr_in6 *)&test_chan_master)->sin6_addr = in6addr_any;
276
        ((struct sockaddr_in6 *)&test_chan_master)->sin6_port = htons(ntohl(req.master_port));
277
        ((struct sockaddr_in6 *)&test_chan_slave)->sin6_port = htons(ntohl(req.slave_port));
278
        break;
279
    default:
280
        pexit("strange TCP sockaddr");
281
    }
282
 
283
    if (bind(s, (struct sockaddr *)&test_chan_master,
284
             sa_len((struct sockaddr *)&test_chan_master)) < 0) {
285
        perror("UDP bind <do_udp_test>");
286
        close(s);
287
        return;
288
    }
289
    test_printf("Start %s [%d,%d]", type_name, nbufs, buflen);
290
    if (pause_time) {
291
        test_printf(" - %dms delay after %d packet%s\n", pause_time*10,
292
                    pause_threshold, pause_threshold > 1 ? "s" : "");
293
    } else {
294
        test_printf(" - no delays\n");
295
    }
296
 
297
    gettimeofday(&start_time, 0);
298
    seq = 0;  seq_errors = 0;  total_packets = 0;
299
    for (i = 0;  i < nbufs;  i++) {
300
        td_len = buflen + sizeof(struct nc_test_data);
301
        if (need_send) {
302
            tdp = (struct nc_test_data *)out_buf;
303
            tdp->key1 = htonl(NC_TEST_DATA_KEY1);
304
            tdp->key2 = htonl(NC_TEST_DATA_KEY2);
305
            tdp->seq = htonl(i);
306
            tdp->len = htonl(td_len);
307
            if (sendto(s, tdp, td_len, 0,
308
                       (struct sockaddr *)&test_chan_slave,
309
                       sa_len((struct sockaddr *)&test_chan_slave)) < 0) {
310
                perror("sendto");
311
                close(s);
312
                return;
313
            }
314
            total_packets++;
315
        }
316
        if (need_recv) {
317
            FD_ZERO(&fds);
318
            FD_SET(s, &fds);
319
            timeout.tv_sec = NC_TEST_TIMEOUT;
320
            timeout.tv_usec = 0;
321
            if (select(s+1, &fds, 0, 0, &timeout) <= 0) {
322
                test_printf("Slave timed out after %d buffers\n", i);
323
                lost_packets++;
324
            } else {
325
                tdp = (struct nc_test_data *)in_buf;
326
                if (recvfrom(s, tdp, td_len, 0, 0, 0) < 0) {
327
                    perror("recvfrom");
328
                    close(s);
329
                    return;
330
                }
331
                if ((ntohl(tdp->key1) == NC_TEST_DATA_KEY1) &&
332
                    (ntohl(tdp->key2) == NC_TEST_DATA_KEY2)) {
333
                    if (ntohl(tdp->seq) != seq) {
334
                        test_printf("Packets out of sequence - recvd: %d, expected: %d\n",
335
                                    ntohl(tdp->seq), seq);
336
                        seq_errors++;
337
                        if (!need_send) {
338
                            // Reset sequence to what the slave wants
339
                            seq = ntohl(tdp->seq);
340
                        }
341
                    }
342
                } else {
343
                    test_printf("Bad data packet - key: %x/%x, seq: %d\n",
344
                                ntohl(tdp->key1), ntohl(tdp->key2),
345
                                ntohl(tdp->seq));
346
                }
347
                total_packets++;
348
            }
349
            seq++;
350
            if (seq == nbufs) {
351
                break;
352
            }
353
            if (pause_time && (++pkt_ctr == pause_threshold)) {
354
                pkt_ctr = 0;
355
                test_delay(pause_time);
356
            }
357
        }
358
    }
359
    gettimeofday(&end_time, 0);
360
    show_results(type_name, &start_time, &end_time, total_packets, buflen,
361
                 lost_packets, seq_errors);
362
    // Fetch results record
363
    FD_ZERO(&fds);
364
    FD_SET(s, &fds);
365
    timeout.tv_sec = NC_RESULTS_TIMEOUT;
366
    timeout.tv_usec = 0;
367
    if (select(s+1, &fds, 0, 0, &timeout) <= 0) {
368
        test_printf("No results record sent\n");
369
    } else {
370
        if (recvfrom(s, &results, sizeof(results), 0, 0, 0) < 0) {
371
            perror("recvfrom");
372
        }
373
        show_test_results(&results);
374
    }
375
    close(s);
376
}
377
 
378
//
379
// Read data from a stream, accounting for the fact that packet 'boundaries'
380
// are not preserved.  This can also timeout (which would probably wreck the
381
// data boundaries).
382
//
383
 
384
int
385
do_read(int fd, void *buf, int buflen)
386
{
387
    char *p = (char *)buf;
388
    int len = buflen;
389
    int res;
390
    while (len) {
391
        res = read(fd, p, len);
392
        if (res < 0) {
393
            perror("read");
394
        } else {
395
            len -= res;
396
            p += res;
397
            if (res == 0) {
398
                break;
399
            }
400
        }
401
    }
402
    return (buflen - len);
403
}
404
 
405
void
406
do_tcp_test(int s1, int type, struct sockaddr *slave,
407
            int nbufs, int buflen, int pause_time, int pause_threshold)
408
{
409
    int i, s, td_len, tot_len, wlen, len, seq, seq_errors, total_packets, res;
410
    struct sockaddr_storage test_chan_slave;
411
    struct timeval start_time, end_time;
412
    struct nc_request req;
413
    struct nc_reply reply;
414
    struct nc_test_results results;
415
    struct nc_test_data *tdp;
416
    int lost_packets = 0;
417
    int conn_failures = 0;
418
    int need_send, need_recv;
419
    const char *type_name;
420
    int pkt_ctr = 0;
421
    unsigned char *dp;
422
 
423
    need_recv = true;  need_send = true;  type_name = "TCP echo";
424
    switch (type) {
425
    case NC_REQUEST_TCP_RECV:
426
        need_recv = false;
427
        need_send = true;
428
        type_name = "TCP recv";
429
        break;
430
    case NC_REQUEST_TCP_SEND:
431
        need_recv = true;
432
        need_send = false;
433
        type_name = "TCP send";
434
        break;
435
    case NC_REQUEST_TCP_ECHO:
436
        break;
437
    }
438
 
439
    new_test();
440
    req.type = htonl(type);
441
    req.nbufs = htonl(nbufs);
442
    req.buflen = htonl(buflen);
443
    req.slave_port = htonl(NC_TESTING_SLAVE_PORT);
444
    req.master_port = htonl(NC_TESTING_MASTER_PORT);
445
    nc_message(s1, &req, &reply, slave);
446
    if (reply.response != ntohl(NC_REPLY_ACK)) {
447
        test_printf("Slave denied %s [%d,%d] test\n", type_name, nbufs, buflen);
448
        return;
449
    }
450
 
451
    s = socket(slave->sa_family, SOCK_STREAM, 0);
452
    if (s < 0) {
453
        pexit("datagram socket");
454
    }
455
 
456
    test_printf("Start %s [%d,%d]", type_name, nbufs, buflen);
457
    if (pause_time) {
458
        test_printf(" - %dms delay after %d packet%s\n", pause_time*10,
459
                    pause_threshold, pause_threshold > 1 ? "s" : "");
460
    } else {
461
        test_printf(" - no delays\n");
462
    }
463
 
464
    test_delay(3*100);
465
    memcpy(&test_chan_slave, slave, sa_len(slave));
466
    switch (slave->sa_family) {
467
    case AF_INET:
468
        ((struct sockaddr_in *)&test_chan_slave)->sin_port = htons(ntohl(req.slave_port));
469
        break;
470
    case AF_INET6:
471
        ((struct sockaddr_in6 *)&test_chan_slave)->sin6_port = htons(ntohl(req.slave_port));
472
        break;
473
    default:
474
        pexit("strange TCP sockaddr");
475
    }
476
    while (connect(s, (struct sockaddr *)&test_chan_slave, sa_len(slave)) < 0) {
477
        perror("Can't connect to slave");
478
        if (++conn_failures > MAX_ERRORS) {
479
            test_printf("Too many connection failures - giving up\n");
480
            return;
481
        }
482
        if (errno == ECONNREFUSED) {
483
            // Give the slave a little time
484
            test_delay(100);  // 1 second
485
        } else {
486
            return;
487
        }
488
    }
489
 
490
    gettimeofday(&start_time, 0);
491
    seq = 0;  seq_errors = 0;  total_packets = 0;
492
    for (i = 0;  i < nbufs;  i++) {
493
        td_len = buflen + sizeof(struct nc_test_data);
494
        if (need_send) {
495
            tdp = (struct nc_test_data *)out_buf;
496
            tdp->key1 = htonl(NC_TEST_DATA_KEY1);
497
            tdp->key2 = htonl(NC_TEST_DATA_KEY2);
498
            tdp->seq = htonl(i);
499
            tdp->len = htonl(td_len);
500
            tot_len = 0;
501
            dp = (unsigned char *)tdp;
502
            while (tot_len < td_len) {
503
                len = td_len - tot_len;
504
                if ((wlen = write(s, dp, len)) != len) {
505
                    if (wlen < 0) {
506
                        test_printf("Slave connection broken\n");
507
                        perror("write");
508
                        close(s);
509
                        return;
510
                    } else {
511
                        test_printf("block: %d, short write - only %d of %d\n",
512
                                    total_packets, wlen, len);
513
                    }
514
                }
515
                tot_len += wlen;
516
                dp += wlen;
517
            }
518
            total_packets++;
519
        }
520
        if (need_recv) {
521
            tdp = (struct nc_test_data *)in_buf;
522
            res = do_read(s, tdp, td_len);
523
            if (res != td_len) {
524
                lost_packets++;
525
                if (res < 0) {
526
                    test_printf("Slave connection broken\n");
527
                    perror("read");
528
                    break;
529
                } else {
530
                    test_printf("Slave timed out after %d buffers [read %d bytes]\n", i, res);
531
                }
532
            } else {
533
                if ((ntohl(tdp->key1) == NC_TEST_DATA_KEY1) &&
534
                    (ntohl(tdp->key2) == NC_TEST_DATA_KEY2)) {
535
                    if (ntohl(tdp->seq) != seq) {
536
                        test_printf("Packets out of sequence - recvd: %d, expected: %d\n",
537
                                    ntohl(tdp->seq), seq);
538
                        seq_errors++;
539
                        if (!need_send) {
540
                            // Reset sequence to what the slave wants
541
                            seq = ntohl(tdp->seq);
542
                        }
543
                    }
544
                } else {
545
                    test_printf("Bad data packet - key: %x/%x, seq: %d\n",
546
                                ntohl(tdp->key1), ntohl(tdp->key2),
547
                                ntohl(tdp->seq));
548
                }
549
                total_packets++;
550
            }
551
            seq++;
552
            if (seq == nbufs) {
553
                break;
554
            }
555
            if (pause_time && (++pkt_ctr == pause_threshold)) {
556
                pkt_ctr = 0;
557
                test_delay(pause_time);
558
            }
559
        }
560
    }
561
    gettimeofday(&end_time, 0);
562
    show_results(type_name, &start_time, &end_time, total_packets, buflen,
563
                 lost_packets, seq_errors);
564
    // Fetch results record
565
    if (do_read(s, &results, sizeof(results)) != sizeof(results)) {
566
        test_printf("No results record sent\n");
567
    } else {
568
        show_test_results(&results);
569
    }
570
    close(s);
571
}
572
 
573
int
574
do_set_load(int s, struct sockaddr *slave, int load_level)
575
{
576
    struct nc_request req;
577
    struct nc_reply reply;
578
    req.type = htonl(NC_REQUEST_SET_LOAD);
579
    req.nbufs = htonl(load_level);
580
    nc_message(s, &req, &reply, slave);
581
    return (reply.response == ntohl(NC_REPLY_ACK));
582
}
583
 
584
int
585
do_start_idle(int s, struct sockaddr *slave)
586
{
587
    struct nc_request req;
588
    struct nc_reply reply;
589
    req.type = htonl(NC_REQUEST_START_IDLE);
590
    nc_message(s, &req, &reply, slave);
591
    return (reply.response == ntohl(NC_REPLY_ACK));
592
}
593
 
594
void
595
do_stop_idle(int s, struct sockaddr *slave, int calibrate)
596
{
597
    struct nc_request req;
598
    struct nc_reply reply;
599
    long long res_idle_count;
600
    long long adj_count;
601
    int idle, res_idle_ticks;
602
    req.type = htonl(NC_REQUEST_STOP_IDLE);
603
    nc_message(s, &req, &reply, slave);
604
    if (reply.response == ntohl(NC_REPLY_ACK)) {
605
        res_idle_ticks = ntohl(reply.misc.idle_results.elapsed_time);
606
        res_idle_count = ((long long)ntohl(reply.misc.idle_results.count[0]) << 32) |
607
            ntohl(reply.misc.idle_results.count[1]);
608
        test_printf("IDLE - ticks: %d, count: %ld",
609
                    res_idle_ticks, res_idle_count);
610
        if (calibrate) {
611
            idle_count = res_idle_count;
612
            idle_ticks = res_idle_ticks;
613
        } else {
614
            adj_count = res_idle_count / res_idle_ticks;
615
            adj_count *= idle_ticks;
616
            idle = (int) ((adj_count * 100) / idle_count);
617
            test_printf(", %d%% idle", idle);
618
        }
619
        test_printf("\n");
620
    } else {
621
        test_printf("Slave failed on IDLE\n");
622
    }
623
}
624
 
625
void
626
do_disconnect(int s, struct sockaddr *slave)
627
{
628
    struct nc_request req;
629
    struct nc_reply reply;
630
    req.type = htonl(NC_REQUEST_DISCONNECT);
631
    nc_message(s, &req, &reply, slave);
632
}
633
 
634
static void
635
nc_master_test(struct sockaddr *slave, int test_tcp, int test_udp,
636
               int test_slave_loads, int test_master_loads)
637
{
638
    int s, i;
639
    struct sockaddr_storage my_addr;
640
    struct pause pause_times[] = {
641
        {0,0}, {1,10}, {5,10}, {10,10}, {1,1} };
642
 
643
 
644
    s = socket(slave->sa_family, SOCK_DGRAM, 0);
645
    if (s < 0) {
646
        pexit("datagram socket");
647
    }
648
 
649
    memset(&my_addr, 0, sizeof(my_addr));
650
    ((struct sockaddr *)&my_addr)->sa_family = slave->sa_family;
651
#ifndef __linux
652
    ((struct sockaddr *)&my_addr)->sa_len = slave->sa_len;
653
#endif
654
    switch (slave->sa_family) {
655
    case AF_INET:
656
        ((struct sockaddr_in *)&my_addr)->sin_addr.s_addr = htonl(INADDR_ANY);
657
        ((struct sockaddr_in *)&my_addr)->sin_port = htons(NC_MASTER_PORT);
658
        break;
659
    case AF_INET6:
660
        ((struct sockaddr_in6 *)&my_addr)->sin6_addr = in6addr_any;
661
        ((struct sockaddr_in6 *)&my_addr)->sin6_port = htons(NC_MASTER_PORT);
662
        break;
663
    default:
664
        pexit("strange sockaddr family");
665
    }
666
    if (bind(s, (struct sockaddr *) &my_addr, sa_len((struct sockaddr *)&my_addr)) < 0) {
667
        pexit("UDP bind <main>");
668
    }
669
 
670
    test_printf("================== No load, master at 100%% ========================\n");
671
    if (test_udp) {
672
        do_udp_test(s, NC_REQUEST_UDP_ECHO, slave, 640, 1024, 0, 0);
673
        do_udp_test(s, NC_REQUEST_UDP_SEND, slave, 640, 1024, 0, 0);
674
        do_udp_test(s, NC_REQUEST_UDP_RECV, slave, 640, 1024, 0, 0);
675
    }
676
    if (test_tcp) {
677
        do_tcp_test(s, NC_REQUEST_TCP_ECHO, slave, 640, 1024, 0, 0);
678
        do_tcp_test(s, NC_REQUEST_TCP_SEND, slave, 640, 1024, 0, 0);
679
        do_tcp_test(s, NC_REQUEST_TCP_RECV, slave, 640, 1024, 0, 0);
680
        do_tcp_test(s, NC_REQUEST_TCP_ECHO, slave, 64, 10240, 0, 0);
681
    }
682
 
683
    if (test_slave_loads) {
684
        if (do_set_load(s, slave, 0)) {
685
            test_printf("\n====================== Various slave compute loads ===================\n");
686
            for (i = 0;  i < 60;  i += 10) {
687
                test_printf(">>>>>>>>>>>> slave processing load at %d%%\n", i);
688
                do_set_load(s, slave, i);
689
                if (test_udp) {
690
                    do_udp_test(s, NC_REQUEST_UDP_ECHO, slave, 2048, 1024, 0, 0);
691
                }
692
                if (test_tcp) {
693
                    do_tcp_test(s, NC_REQUEST_TCP_ECHO, slave, 2048, 1024, 0, 0);
694
                }
695
            }
696
        }
697
    }
698
 
699
    if (test_master_loads) {
700
        if (do_start_idle(s, slave)) {
701
            test_printf("\n====================== Various master loads ===================\n");
702
            test_printf("Testing IDLE for %d seconds\n", IDLE_TEST_TIME);
703
            test_delay(IDLE_TEST_TIME*100);
704
            do_stop_idle(s, slave, true);
705
            for (i = 0;  i < LENGTH(pause_times);  i++) {
706
                if (test_udp) {
707
                    do_start_idle(s, slave);
708
                    do_udp_test(s, NC_REQUEST_UDP_ECHO, slave, 2048, 1024,
709
                                pause_times[i].pause_ticks, pause_times[i].pause_threshold);
710
                    do_stop_idle(s, slave, false);
711
                }
712
                if (test_tcp) {
713
                    do_start_idle(s, slave);
714
                    do_tcp_test(s, NC_REQUEST_TCP_ECHO, slave, 2048, 1024,
715
                                pause_times[i].pause_ticks, pause_times[i].pause_threshold);
716
                    do_stop_idle(s, slave, false);
717
                }
718
            }
719
        }
720
    }
721
 
722
//    do_disconnect(s, slave);
723
    close(s);
724
}
725
 
726
static void
727
nc_master(struct test_params *p)
728
{
729
    struct sockaddr_storage slave, my_addr;
730
    struct addrinfo *ai, *addrs, hints;
731
    char *host = (char *)NULL;
732
    int i;
733
    int err = 0;
734
    int test_tcp = true;
735
    int test_udp = true;
736
    int test_slave_loads = true;
737
    int test_master_loads = true;
738
 
739
    for (i = 1;  i < p->argc;  i++) {
740
        if (p->argv[i][0] == '-') {
741
            switch (p->argv[i][1]) {
742
            case 't':
743
                test_tcp = false;
744
                break;
745
            case 'u':
746
                test_udp = false;
747
                break;
748
            case 's':
749
                test_slave_loads = false;
750
                break;
751
            case 'm':
752
                test_master_loads = false;
753
                break;
754
            default:
755
                test_printf("... invalid switch '%s'\n", p->argv[i]);
756
                err++;
757
            }
758
        } else {
759
            if (host != (char *)NULL) {
760
                test_printf("... ignoring argument '%s'\n", p->argv[i]);
761
                err++;
762
            } else {
763
                host = p->argv[i];
764
            }
765
        }
766
    }
767
    if ((err != 0) || (p->argc < 2) || (host == (char *)NULL)) {
768
        test_printf("usage: 'master <host> [-t] [-u] [-s] [-m]'\n");
769
        test_printf("   -t - suppress TCP tests\n");
770
        test_printf("   -u - suppress UDP tests\n");
771
        test_printf("   -s - suppress slave load tests\n");
772
        test_printf("   -m - suppress master load tests\n");
773
        return;
774
    }
775
    bzero(&hints, sizeof(hints));
776
    hints.ai_family = PF_UNSPEC;
777
    hints.ai_socktype = SOCK_DGRAM;
778
    hints.ai_flags = AI_PASSIVE;
779
    if ((err = getaddrinfo(p->argv[1], _string(NC_SLAVE_PORT), &hints, &addrs)) != EAI_NONE) {
780
        test_printf("<ERROR> can't getaddrinfo(): %s\n", gai_strerror(err));
781
        pexit("getaddrinfo");
782
    }
783
    // Prepare a socket for each connection type
784
    ai = addrs;
785
    while (ai) {
786
        nc_master_test(ai->ai_addr, test_tcp, test_udp, test_slave_loads, test_master_loads);
787
        ai = ai->ai_next;
788
    }
789
}
790
 
791
void
792
net_test(test_param_t p)
793
{
794
    test_printf("Start Network Characterization - MASTER\n");
795
#ifdef __ECOS
796
    init_all_network_interfaces();
797
#endif
798
    nc_master((struct test_params *)p);
799
    cyg_test_exit();
800
}
801
 
802
#ifdef __ECOS
803
void
804
cyg_start(void)
805
{
806
    static struct test_params p;
807
    // Create a main thread, so we can run the scheduler and have time 'pass'
808
    cyg_thread_create(10,                // Priority - just a number
809
                      net_test,          // entry
810
                      (cyg_addrword_t)&p,// entry parameter
811
                      "Network test",    // Name
812
                      &stack[0],         // Stack
813
                      STACK_SIZE,        // Size
814
                      &thread_handle,    // Handle
815
                      &thread_data       // Thread data structure
816
            );
817
    cyg_thread_resume(thread_handle);  // Start it
818
    cyg_scheduler_start();
819
}
820
 
821
#else
822
 
823
int
824
main(int argc, char *argv[])
825
{
826
    struct test_params p;
827
    p.argc = argc;
828
    p.argv = argv;
829
    net_test(&p);
830
}
831
#endif

powered by: WebSVN 2.1.0

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