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