PageRenderTime 26ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/packages/net/common/current/tests/host_echo.c

https://gitlab.com/metadevfoundation/ecos-ax-som-bf609
C | 278 lines | 198 code | 37 blank | 43 comment | 40 complexity | c8eb1d44194cbc9febae22d2d9fd4665 MD5 | raw file
Possible License(s): GPL-2.0, MIT
  1. //==========================================================================
  2. //
  3. // tests/host_echo.c
  4. //
  5. // Simple TCP throughput test - echo component FOR *HOST*
  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. #undef _KERNEL
  35. #include <stdlib.h>
  36. #include <unistd.h>
  37. #include <stdio.h>
  38. #include <sys/param.h>
  39. #include <sys/socket.h>
  40. #include <sys/ioctl.h>
  41. #include <sys/errno.h>
  42. #include <sys/time.h>
  43. #include <net/if.h>
  44. #include <netinet/in_systm.h>
  45. #include <netinet/in.h>
  46. #include <netinet/ip.h>
  47. #include <netinet/ip_icmp.h>
  48. #include <netdb.h>
  49. #define true 1
  50. #define false 1
  51. #define TNR_ON()
  52. #define TNR_OFF()
  53. #define diag_printf printf
  54. void
  55. pexit(char *s)
  56. {
  57. perror(s);
  58. exit(1);
  59. }
  60. #define SOURCE_PORT 9990
  61. #define SINK_PORT 9991
  62. #define MAX_BUF 8192
  63. static unsigned char data_buf[MAX_BUF];
  64. struct test_params {
  65. long nbufs;
  66. long bufsize;
  67. long load;
  68. };
  69. struct test_status {
  70. long ok;
  71. };
  72. int max( int a, int b ) { return (a>b)?a:b; }
  73. int min( int a, int b ) { return (a<b)?a:b; }
  74. int
  75. do_read(int s, void *_buf, int len)
  76. {
  77. int total, slen, rlen;
  78. unsigned char *buf = (unsigned char *)_buf;
  79. total = 0;
  80. rlen = len;
  81. while (total < len) {
  82. slen = read(s, buf, rlen);
  83. if (slen != rlen) {
  84. if (slen < 0) {
  85. diag_printf("Error after reading %d bytes\n", total);
  86. return -1;
  87. }
  88. rlen -= slen;
  89. buf += slen;
  90. }
  91. total += slen;
  92. }
  93. return total;
  94. }
  95. int
  96. do_write(int s, void *_buf, int len)
  97. {
  98. int total, slen, rlen;
  99. unsigned char *buf = (unsigned char *)_buf;
  100. total = 0;
  101. rlen = len;
  102. while (total < len) {
  103. slen = write(s, buf, rlen);
  104. if (slen != rlen) {
  105. if (slen < 0) {
  106. diag_printf("Error after writing %d bytes\n", total);
  107. return -1;
  108. }
  109. rlen -= slen;
  110. buf += slen;
  111. }
  112. total += slen;
  113. }
  114. return total;
  115. }
  116. static void
  117. echo_test(void * p)
  118. {
  119. int s_source, s_sink, e_source, e_sink;
  120. struct sockaddr_in e_source_addr, e_sink_addr, local;
  121. int one = 1;
  122. fd_set in_fds;
  123. int i, num, len;
  124. struct test_params params,nparams;
  125. struct test_status status,nstatus;
  126. s_source = socket(AF_INET, SOCK_STREAM, 0);
  127. if (s_source < 0) {
  128. pexit("stream socket");
  129. }
  130. if (setsockopt(s_source, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one))) {
  131. pexit("setsockopt /source/ SO_REUSEADDR");
  132. }
  133. memset(&local, 0, sizeof(local));
  134. local.sin_family = AF_INET;
  135. // local.sin_len = sizeof(local);
  136. local.sin_port = ntohs(SOURCE_PORT);
  137. local.sin_addr.s_addr = INADDR_ANY;
  138. if(bind(s_source, (struct sockaddr *) &local, sizeof(local)) < 0) {
  139. pexit("bind /source/ error");
  140. }
  141. listen(s_source, SOMAXCONN);
  142. s_sink = socket(AF_INET, SOCK_STREAM, 0);
  143. if (s_sink < 0) {
  144. pexit("stream socket");
  145. }
  146. memset(&local, 0, sizeof(local));
  147. local.sin_family = AF_INET;
  148. // local.sin_len = sizeof(local);
  149. local.sin_port = ntohs(SINK_PORT);
  150. local.sin_addr.s_addr = INADDR_ANY;
  151. if(bind(s_sink, (struct sockaddr *) &local, sizeof(local)) < 0) {
  152. pexit("bind /sink/ error");
  153. }
  154. if (setsockopt(s_sink, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one))) {
  155. pexit("setsockopt /sink/ SO_REUSEADDR");
  156. }
  157. listen(s_sink, SOMAXCONN);
  158. e_source = 0; e_sink = 0;
  159. while (true) {
  160. // Wait for a connection on either of the ports
  161. FD_ZERO(&in_fds);
  162. FD_SET(s_source, &in_fds);
  163. FD_SET(s_sink, &in_fds);
  164. num = select(max(s_sink,s_source)+1, &in_fds, 0, 0, 0);
  165. if (FD_ISSET(s_source, &in_fds)) {
  166. len = sizeof(e_source_addr);
  167. if ((e_source = accept(s_source, (struct sockaddr *)&e_source_addr, &len)) < 0) {
  168. pexit("accept /source/");
  169. }
  170. diag_printf("SOURCE connection from %s:%d\n",
  171. inet_ntoa(e_source_addr.sin_addr), ntohs(e_source_addr.sin_port));
  172. }
  173. if (FD_ISSET(s_sink, &in_fds)) {
  174. len = sizeof(e_sink_addr);
  175. if ((e_sink = accept(s_sink, (struct sockaddr *)&e_sink_addr, &len)) < 0) {
  176. pexit("accept /sink/");
  177. }
  178. diag_printf("SINK connection from %s:%d\n",
  179. inet_ntoa(e_sink_addr.sin_addr), ntohs(e_sink_addr.sin_port));
  180. }
  181. // Continue with test once a connection is established in both directions
  182. if ((e_source != 0) && (e_sink != 0)) {
  183. break;
  184. }
  185. }
  186. // Wait for "source" to tell us the testing paramters
  187. if (do_read(e_source, &nparams, sizeof(nparams)) != sizeof(nparams)) {
  188. pexit("Can't read initialization parameters");
  189. }
  190. params.nbufs = ntohl(nparams.nbufs);
  191. params.bufsize = ntohl(nparams.bufsize);
  192. params.load = ntohl(nparams.load);
  193. diag_printf("Using %d buffers of %d bytes each, %d%% background load\n",
  194. params.nbufs, params.bufsize, params.load);
  195. // Tell the sink what the parameters are
  196. if (do_write(e_sink, &nparams, sizeof(nparams)) != sizeof(nparams)) {
  197. pexit("Can't write initialization parameters");
  198. }
  199. status.ok = 1;
  200. nstatus.ok = htonl(status.ok);
  201. // Tell the "source" to start - we're all connected and ready to go!
  202. if (do_write(e_source, &nstatus, sizeof(nstatus)) != sizeof(nstatus)) {
  203. pexit("Can't send ACK to 'source' host");
  204. }
  205. TNR_ON();
  206. // Echo the data from the source to the sink hosts
  207. for (i = 0; i < params.nbufs; i++) {
  208. if ((len = do_read(e_source, data_buf, params.bufsize)) != params.bufsize) {
  209. TNR_OFF();
  210. diag_printf("Can't read buf #%d: ", i+1);
  211. if (len < 0) {
  212. perror("I/O error");
  213. } else {
  214. diag_printf("short read - only %d bytes\n", len);
  215. }
  216. TNR_ON();
  217. }
  218. if ((len = do_write(e_sink, data_buf, params.bufsize)) != params.bufsize) {
  219. TNR_OFF();
  220. diag_printf("Can't write buf #%d: ", i+1);
  221. if (len < 0) {
  222. perror("I/O error");
  223. } else {
  224. diag_printf("short write - only %d bytes\n", len);
  225. }
  226. TNR_ON();
  227. }
  228. }
  229. TNR_OFF();
  230. // Wait for the data to drain and the "sink" to tell us all is OK.
  231. if (do_read(e_sink, &status, sizeof(status)) != sizeof(status)) {
  232. pexit("Can't receive ACK from 'sink' host");
  233. }
  234. }
  235. int
  236. main(int argc, char *argv[])
  237. {
  238. echo_test(argv[1]);
  239. return 0;
  240. }
  241. // EOF