1 //==========================================================================
3 // tests/nc_test_master.c
5 // Network characterizations test (master portion)
7 //==========================================================================
8 //####BSDCOPYRIGHTBEGIN####
10 // -------------------------------------------
12 // Portions of this software may have been derived from OpenBSD or other sources,
13 // and are covered by the appropriate copyright disclaimers included herein.
15 // -------------------------------------------
17 //####BSDCOPYRIGHTEND####
18 //==========================================================================
19 //#####DESCRIPTIONBEGIN####
22 // Contributors: gthomas
28 //####DESCRIPTIONEND####
30 //==========================================================================
32 // Network characterization test code - master portion
34 #include "nc_test_framework.h"
37 #ifndef CYGPKG_LIBC_STDIO
38 #define perror(s) diag_printf(#s ": %s\n", strerror(errno))
40 #define STACK_SIZE (CYGNUM_HAL_STACK_SIZE_TYPICAL + 0x1000)
41 static char stack[STACK_SIZE];
42 static cyg_thread thread_data;
43 static cyg_handle_t thread_handle;
51 #define MAX_BUF 32*1024
52 static unsigned char in_buf[MAX_BUF], out_buf[MAX_BUF];
54 static int test_seq = 1;
55 static long long idle_count;
56 static long idle_ticks;
57 #define IDLE_TEST_TIME 10
63 #define LENGTH(x) (sizeof(x)/sizeof(x[0]))
73 test_printf("... Done\n");
82 cyg_thread_delay(ticks);
90 usleep(ticks * 10000);
103 # #include <pkgconf/posix.h>
105 # ifndef CYGPKG_POSIX_TIMERS
107 gettimeofday(struct timeval *tv, struct timezone *tz)
109 cyg_tick_count_t cur_time;
110 cur_time = cyg_current_time();
111 tv->tv_sec = cur_time / 100;
112 tv->tv_usec = (cur_time % 100) * 10000;
118 show_results(const char *msg, struct timeval *start,
119 struct timeval *end, int nbufs, int buflen,
120 int lost, int seq_errors)
122 struct timeval tot_time;
124 double real_time, thru;
125 long tot_bytes = nbufs * buflen;
127 timersub(end, start, &tot_time);
128 test_printf("%s - %d bufs of %d bytes in %d.%02d seconds",
130 tot_time.tv_sec, tot_time.tv_usec / 10000);
132 real_time = tot_time.tv_sec + ((tot_time.tv_usec / 10000) * .01);
133 // Compute bytes / second (rounded up)
134 thru = tot_bytes / real_time;
135 // Convert to Mb / second
136 test_printf(" - %.2f KB/S", thru / 1024.0);
137 test_printf(" - %.4f Mbit/S (M = 10^6)", thru * 8.0 / 1000000.0);
140 test_printf(", %d lost", lost);
143 test_printf(", %d out of sequence", seq_errors);
155 nc_message(int s, struct nc_request *req,
156 struct nc_reply *reply, struct sockaddr_in *slave)
160 struct timeval timeout;
161 len = sizeof(*slave);
162 req->seq = htonl(test_seq);
163 if (sendto(s, req, sizeof(*req), 0, (struct sockaddr *)slave, len) < 0) {
169 timeout.tv_sec = NC_REPLY_TIMEOUT;
171 if (select(s+1, &fds, 0, 0, &timeout) <= 0) {
172 test_printf("No response to command\n");
175 if (recvfrom(s, reply, sizeof(*reply), 0, 0, 0) < 0) {
179 if (reply->seq != req->seq) {
180 test_printf("Response out of order - sent: %d, recvd: %d\n",
181 ntohl(req->seq), ntohl(reply->seq));
188 show_test_results(struct nc_test_results *results)
190 if ((ntohl(results->key1) == NC_TEST_RESULT_KEY1) &&
191 (ntohl(results->key2) == NC_TEST_RESULT_KEY2) &&
192 (ntohl(results->seq) == test_seq)) {
193 test_printf(" slave sent %d, recvd %d\n",
194 ntohl(results->nsent), ntohl(results->nrecvd));
196 test_printf(" ... invalid results - keys: %x/%x, seq: %d/%d\n",
197 ntohl(results->key1), ntohl(results->key2),
198 ntohl(results->seq), test_seq);
203 do_udp_test(int s1, int type, struct sockaddr_in *slave,
204 int nbufs, int buflen, int pause_time, int pause_threshold)
206 int i, s, td_len, seq, seq_errors, total_packets;
207 struct sockaddr_in test_chan_master, test_chan_slave;
208 struct timeval start_time, end_time;
209 struct nc_request req;
210 struct nc_reply reply;
211 struct nc_test_results results;
212 struct nc_test_data *tdp;
214 struct timeval timeout;
215 int lost_packets = 0;
216 int need_send, need_recv;
217 const char *type_name;
220 need_recv = true; need_send = true; type_name = "UDP echo";
222 case NC_REQUEST_UDP_RECV:
225 type_name = "UDP recv";
227 case NC_REQUEST_UDP_SEND:
230 type_name = "UDP send";
232 case NC_REQUEST_UDP_ECHO:
237 req.type = htonl(type);
238 req.nbufs = htonl(nbufs);
239 req.buflen = htonl(buflen);
240 req.slave_port = htonl(NC_TESTING_SLAVE_PORT);
241 req.master_port = htonl(NC_TESTING_MASTER_PORT);
242 nc_message(s1, &req, &reply, slave);
243 if (reply.response != ntohl(NC_REPLY_ACK)) {
244 test_printf("Slave denied %s [%d,%d] test\n", type_name, nbufs, buflen);
248 s = socket(AF_INET, SOCK_DGRAM, 0);
250 pexit("datagram socket");
253 memset(&test_chan_master, 0, sizeof(test_chan_master));
254 test_chan_master.sin_family = AF_INET;
256 test_chan_master.sin_len = sizeof(test_chan_master);
258 test_chan_master.sin_port = htons(ntohl(req.master_port));
259 test_chan_master.sin_addr.s_addr = INADDR_ANY;
261 if (bind(s, (struct sockaddr *) &test_chan_master, sizeof(test_chan_master)) < 0) {
266 test_printf("Start %s [%d,%d]", type_name, nbufs, buflen);
268 test_printf(" - %dms delay after %d packet%s\n", pause_time*10,
269 pause_threshold, pause_threshold > 1 ? "s" : "");
271 test_printf(" - no delays\n");
274 gettimeofday(&start_time, 0);
275 memcpy(&test_chan_slave, slave, sizeof(*slave));
276 test_chan_slave.sin_port = htons(ntohl(req.slave_port));
277 seq = 0; seq_errors = 0; total_packets = 0;
278 for (i = 0; i < nbufs; i++) {
279 td_len = buflen + sizeof(struct nc_test_data);
281 tdp = (struct nc_test_data *)out_buf;
282 tdp->key1 = htonl(NC_TEST_DATA_KEY1);
283 tdp->key2 = htonl(NC_TEST_DATA_KEY2);
285 tdp->len = htonl(td_len);
286 if (sendto(s, tdp, td_len, 0,
287 (struct sockaddr *)&test_chan_slave, sizeof(test_chan_slave)) < 0) {
297 timeout.tv_sec = NC_TEST_TIMEOUT;
299 if (select(s+1, &fds, 0, 0, &timeout) <= 0) {
300 test_printf("Slave timed out after %d buffers\n", i);
303 tdp = (struct nc_test_data *)in_buf;
304 if (recvfrom(s, tdp, td_len, 0, 0, 0) < 0) {
309 if ((ntohl(tdp->key1) == NC_TEST_DATA_KEY1) &&
310 (ntohl(tdp->key2) == NC_TEST_DATA_KEY2)) {
311 if (ntohl(tdp->seq) != seq) {
312 test_printf("Packets out of sequence - recvd: %d, expected: %d\n",
313 ntohl(tdp->seq), seq);
316 // Reset sequence to what the slave wants
317 seq = ntohl(tdp->seq);
321 test_printf("Bad data packet - key: %x/%x, seq: %d\n",
322 ntohl(tdp->key1), ntohl(tdp->key2),
331 if (pause_time && (++pkt_ctr == pause_threshold)) {
333 test_delay(pause_time);
337 gettimeofday(&end_time, 0);
338 show_results(type_name, &start_time, &end_time, total_packets, buflen,
339 lost_packets, seq_errors);
340 // Fetch results record
343 timeout.tv_sec = NC_RESULTS_TIMEOUT;
345 if (select(s+1, &fds, 0, 0, &timeout) <= 0) {
346 test_printf("No results record sent\n");
348 if (recvfrom(s, &results, sizeof(results), 0, 0, 0) < 0) {
351 show_test_results(&results);
357 // Read data from a stream, accounting for the fact that packet 'boundaries'
358 // are not preserved. This can also timeout (which would probably wreck the
363 do_read(int fd, void *buf, int buflen)
365 char *p = (char *)buf;
369 res = read(fd, p, len);
380 return (buflen - len);
384 do_tcp_test(int s1, int type, struct sockaddr_in *slave,
385 int nbufs, int buflen, int pause_time, int pause_threshold)
387 int i, s, td_len, len, seq, seq_errors, total_packets, res;
388 struct sockaddr_in test_chan_slave;
389 struct timeval start_time, end_time;
390 struct nc_request req;
391 struct nc_reply reply;
392 struct nc_test_results results;
393 struct nc_test_data *tdp;
394 int lost_packets = 0;
395 int conn_failures = 0;
396 int need_send, need_recv;
397 const char *type_name;
400 need_recv = true; need_send = true; type_name = "TCP echo";
402 case NC_REQUEST_TCP_RECV:
405 type_name = "TCP recv";
407 case NC_REQUEST_TCP_SEND:
410 type_name = "TCP send";
412 case NC_REQUEST_TCP_ECHO:
417 req.type = htonl(type);
418 req.nbufs = htonl(nbufs);
419 req.buflen = htonl(buflen);
420 req.slave_port = htonl(NC_TESTING_SLAVE_PORT);
421 req.master_port = htonl(NC_TESTING_MASTER_PORT);
422 nc_message(s1, &req, &reply, slave);
423 if (reply.response != ntohl(NC_REPLY_ACK)) {
424 test_printf("Slave denied %s [%d,%d] test\n", type_name, nbufs, buflen);
428 s = socket(AF_INET, SOCK_STREAM, 0);
430 pexit("datagram socket");
433 test_printf("Start %s [%d,%d]", type_name, nbufs, buflen);
435 test_printf(" - %dms delay after %d packet%s\n", pause_time*10,
436 pause_threshold, pause_threshold > 1 ? "s" : "");
438 test_printf(" - no delays\n");
442 memcpy(&test_chan_slave, slave, sizeof(*slave));
443 test_chan_slave.sin_port = htons(ntohl(req.slave_port));
444 while (connect(s, (struct sockaddr *)&test_chan_slave, sizeof(*slave)) < 0) {
445 perror("Can't connect to slave");
446 if (++conn_failures > MAX_ERRORS) {
447 test_printf("Too many connection failures - giving up\n");
450 if (errno == ECONNREFUSED) {
451 // Give the slave a little time
452 test_delay(100); // 1 second
458 gettimeofday(&start_time, 0);
459 seq = 0; seq_errors = 0; total_packets = 0;
460 for (i = 0; i < nbufs; i++) {
461 td_len = buflen + sizeof(struct nc_test_data);
463 tdp = (struct nc_test_data *)out_buf;
464 tdp->key1 = htonl(NC_TEST_DATA_KEY1);
465 tdp->key2 = htonl(NC_TEST_DATA_KEY2);
467 tdp->len = htonl(td_len);
468 if ((len = write(s, tdp, td_len)) != td_len) {
472 test_printf("short write - only %d or %d bytes written\n", len, td_len);
480 tdp = (struct nc_test_data *)in_buf;
481 res = do_read(s, tdp, td_len);
483 test_printf("Slave timed out after %d buffers\n", i);
486 if ((ntohl(tdp->key1) == NC_TEST_DATA_KEY1) &&
487 (ntohl(tdp->key2) == NC_TEST_DATA_KEY2)) {
488 if (ntohl(tdp->seq) != seq) {
489 test_printf("Packets out of sequence - recvd: %d, expected: %d\n",
490 ntohl(tdp->seq), seq);
493 // Reset sequence to what the slave wants
494 seq = ntohl(tdp->seq);
498 test_printf("Bad data packet - key: %x/%x, seq: %d\n",
499 ntohl(tdp->key1), ntohl(tdp->key2),
508 if (pause_time && (++pkt_ctr == pause_threshold)) {
510 test_delay(pause_time);
514 gettimeofday(&end_time, 0);
515 show_results(type_name, &start_time, &end_time, total_packets, buflen,
516 lost_packets, seq_errors);
517 // Fetch results record
518 if (do_read(s, &results, sizeof(results)) != sizeof(results)) {
519 test_printf("No results record sent\n");
521 show_test_results(&results);
527 do_set_load(int s, struct sockaddr_in *slave, int load_level)
529 struct nc_request req;
530 struct nc_reply reply;
531 req.type = htonl(NC_REQUEST_SET_LOAD);
532 req.nbufs = htonl(load_level);
533 nc_message(s, &req, &reply, slave);
534 return (reply.response == ntohl(NC_REPLY_ACK));
538 do_start_idle(int s, struct sockaddr_in *slave)
540 struct nc_request req;
541 struct nc_reply reply;
542 req.type = htonl(NC_REQUEST_START_IDLE);
543 nc_message(s, &req, &reply, slave);
544 return (reply.response == ntohl(NC_REPLY_ACK));
548 do_stop_idle(int s, struct sockaddr_in *slave, int calibrate)
550 struct nc_request req;
551 struct nc_reply reply;
552 long long res_idle_count;
554 int idle, res_idle_ticks;
555 req.type = htonl(NC_REQUEST_STOP_IDLE);
556 nc_message(s, &req, &reply, slave);
557 if (reply.response == ntohl(NC_REPLY_ACK)) {
558 res_idle_ticks = ntohl(reply.misc.idle_results.elapsed_time);
559 res_idle_count = ((long long)ntohl(reply.misc.idle_results.count[0]) << 32) |
560 ntohl(reply.misc.idle_results.count[1]);
561 test_printf("IDLE - ticks: %d, count: %ld",
562 res_idle_ticks, res_idle_count);
564 idle_count = res_idle_count;
565 idle_ticks = res_idle_ticks;
567 adj_count = res_idle_count / res_idle_ticks;
568 adj_count *= idle_ticks;
569 idle = (int) ((adj_count * 100) / idle_count);
570 test_printf(", %d%% idle", idle);
574 test_printf("Slave failed on IDLE\n");
579 do_disconnect(int s, struct sockaddr_in *slave)
581 struct nc_request req;
582 struct nc_reply reply;
583 req.type = htonl(NC_REQUEST_DISCONNECT);
584 nc_message(s, &req, &reply, slave);
588 nc_master(struct test_params *p)
591 struct sockaddr_in slave, my_addr;
592 struct hostent *host;
593 struct pause pause_times[] = {
594 {0,0}, {1,10}, {5,10}, {10,10}, {1,1} };
597 test_printf("Need exactly 'master <host>'\n");
601 s = socket(AF_INET, SOCK_DGRAM, 0);
603 pexit("datagram socket");
606 memset(&my_addr, 0, sizeof(my_addr));
607 my_addr.sin_family = AF_INET;
609 my_addr.sin_len = sizeof(my_addr);
611 my_addr.sin_port = htons(NC_MASTER_PORT);
612 my_addr.sin_addr.s_addr = INADDR_ANY;
614 if (bind(s, (struct sockaddr *) &my_addr, sizeof(my_addr)) < 0) {
618 host = gethostbyname(p->argv[1]);
619 if (host == (struct hostent *)NULL) {
620 pexit("gethostbyname");
623 memset(&slave, 0, sizeof(slave));
624 slave.sin_family = AF_INET;
626 slave.sin_len = sizeof(slave);
628 slave.sin_port = htons(NC_SLAVE_PORT);
629 memcpy(&slave.sin_addr.s_addr, host->h_addr, host->h_length);
631 test_printf("================== No load, master at 100%% ========================\n");
633 do_udp_test(s, NC_REQUEST_UDP_ECHO, &slave, 640, 1024, 0, 0);
634 do_udp_test(s, NC_REQUEST_UDP_SEND, &slave, 640, 1024, 0, 0);
635 do_udp_test(s, NC_REQUEST_UDP_RECV, &slave, 640, 1024, 0, 0);
636 do_tcp_test(s, NC_REQUEST_TCP_ECHO, &slave, 640, 1024, 0, 0);
637 do_tcp_test(s, NC_REQUEST_TCP_SEND, &slave, 640, 1024, 0, 0);
638 do_tcp_test(s, NC_REQUEST_TCP_RECV, &slave, 640, 1024, 0, 0);
640 do_tcp_test(s, NC_REQUEST_TCP_ECHO, &slave, 64, 10240, 0, 0);
642 if (do_set_load(s, &slave, 0)) {
643 test_printf("\n====================== Various slave compute loads ===================\n");
644 for (i = 0; i < 60; i += 10) {
645 test_printf(">>>>>>>>>>>> slave processing load at %d%%\n", i);
646 do_set_load(s, &slave, i);
647 do_udp_test(s, NC_REQUEST_UDP_ECHO, &slave, 2048, 1024, 0, 0);
648 do_tcp_test(s, NC_REQUEST_TCP_ECHO, &slave, 2048, 1024, 0, 0);
652 if (do_start_idle(s, &slave)) {
653 test_printf("\n====================== Various master loads ===================\n");
654 test_printf("Testing IDLE for %d seconds\n", IDLE_TEST_TIME);
655 test_delay(IDLE_TEST_TIME*100);
656 do_stop_idle(s, &slave, true);
657 for (i = 0; i < LENGTH(pause_times); i++) {
658 do_start_idle(s, &slave);
659 do_udp_test(s, NC_REQUEST_UDP_ECHO, &slave, 2048, 1024,
660 pause_times[i].pause_ticks, pause_times[i].pause_threshold);
661 do_stop_idle(s, &slave, false);
662 do_start_idle(s, &slave);
663 do_tcp_test(s, NC_REQUEST_TCP_ECHO, &slave, 2048, 1024,
664 pause_times[i].pause_ticks, pause_times[i].pause_threshold);
665 do_stop_idle(s, &slave, false);
669 do_disconnect(s, &slave);
674 net_test(test_param_t p)
676 test_printf("Start Network Characterization - MASTER\n");
678 init_all_network_interfaces();
680 nc_master((struct test_params *)p);
688 static struct test_params p;
689 // Create a main thread, so we can run the scheduler and have time 'pass'
690 cyg_thread_create(10, // Priority - just a number
692 (cyg_addrword_t)&p,// entry parameter
693 "Network test", // Name
696 &thread_handle, // Handle
697 &thread_data // Thread data structure
699 cyg_thread_resume(thread_handle); // Start it
700 cyg_scheduler_start();
706 main(int argc, char *argv[])
708 struct test_params p;