]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/net/common/v2_0/tests/tcp_lo_test.c
unified MX27, MX25, MX37 trees
[karo-tx-redboot.git] / packages / net / common / v2_0 / tests / tcp_lo_test.c
1 //==========================================================================
2 //
3 //      tests/tcp_lo_test.c
4 // 
5 //      Simple TCP throughput test 
6 //
7 //==========================================================================
8 //####BSDCOPYRIGHTBEGIN####
9 //
10 // -------------------------------------------
11 //
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.
14 //
15 // -------------------------------------------
16 //
17 //####BSDCOPYRIGHTEND####
18 //==========================================================================
19 //#####DESCRIPTIONBEGIN####
20 //
21 // Author(s):    sorin@netappi.com 
22 // Contributors: gthomas,sorin@netappi.com, hmt
23 // Date:         2000-05-24
24
25
26 // Network throughput test code
27
28 #include <network.h>
29
30 #include <cyg/infra/testcase.h>
31
32 #define SOURCE_PORT 9990
33 #define SINK_PORT   9991
34
35 #define NUM_BUF 1024
36 #define MAX_BUF 8192
37 static unsigned char data_buf[MAX_BUF];
38 static unsigned char data_buf_write[MAX_BUF]="Client is alive. You may continue ....";
39
40 #define STACK_SIZE (CYGNUM_HAL_STACK_SIZE_TYPICAL + 0x10000)
41 static char stack_server[STACK_SIZE];
42 static cyg_thread server_thread_data;
43 static cyg_handle_t server_thread_handle;
44
45 static char stack_client[STACK_SIZE];
46 static cyg_thread client_thread_data; 
47 static cyg_handle_t client_thread_handle;  
48
49
50 #define MAIN_THREAD_PRIORITY     CYGPKG_NET_THREAD_PRIORITY-4
51
52 void
53 pexit(char *s)
54 {
55     CYG_TEST_FAIL_FINISH( s );
56 }
57
58
59 void server(void)
60 {
61     int s_source, e_source;
62     struct sockaddr_in e_source_addr, local;
63     int one = 1;
64     fd_set in_fds;
65     socklen_t len;
66     
67     char *hello_string=" Hello eCos network \n";
68     diag_printf("TCP SERVER:");
69     diag_printf(hello_string);
70
71     s_source = socket(AF_INET, SOCK_STREAM, 0);
72     if (s_source < 0) {
73         pexit("stream socket");
74     }   
75     if (setsockopt(s_source, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one))) {
76         pexit("setsockopt /source/ SO_REUSEADDR");
77     }
78     if (setsockopt(s_source, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one))) {
79         pexit("setsockopt /source/ SO_REUSEPORT");
80     }
81     memset(&local, 0, sizeof(local));
82     local.sin_family = AF_INET;
83     local.sin_len = sizeof(local);
84     local.sin_port = ntohs(SOURCE_PORT);
85     local.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
86     if(bind(s_source, (struct sockaddr *) &local, sizeof(local)) < 0) {
87         pexit("bind /source/ error");
88     }
89     listen(s_source, SOMAXCONN);
90    
91     e_source = 0; 
92     while (true) {
93         FD_ZERO(&in_fds);
94         FD_SET(s_source, &in_fds);
95         len = sizeof(e_source_addr);
96         if ((e_source = accept(s_source,(struct sockaddr *)&e_source_addr,&len))<0) {
97             pexit("accept /source/");
98         }
99         diag_printf("TCP SERVER connection from %s: %d\n",
100                inet_ntoa(e_source_addr.sin_addr),ntohs(e_source_addr.sin_port));
101
102         if (e_source != 0) {
103             break;
104         }
105     }   /* while (true) */ 
106     
107     if ((len = read(e_source, data_buf, MAX_BUF)) < 0  ) {
108         CYG_TEST_FAIL_FINISH( "I/O error" );
109     }
110     diag_printf("SERVER : %s\n",data_buf);
111     
112 }
113
114 void client(void)
115 {
116     int s_source;
117     struct sockaddr_in local;
118     int len;
119
120     diag_printf("client:started\n");
121     
122     s_source = socket(AF_INET, SOCK_STREAM, 0);
123     if (s_source < 0) {
124         pexit("stream socket");
125     }
126     memset(&local, 0, sizeof(local));
127     local.sin_family = AF_INET;
128     local.sin_len = sizeof(local);
129     local.sin_port = htons(SOURCE_PORT);
130     local.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
131     
132     if (connect(s_source, (struct sockaddr *)&local, sizeof(local)) < 0) {
133         pexit("Can't connect to target");
134     }
135     
136     if ((len = write(s_source,data_buf_write,40)) < 0){
137         CYG_TEST_FAIL_FINISH( "Error writing buffer");
138     } 
139 }
140
141 void
142 tcp_server(cyg_addrword_t param)
143 {
144     init_all_network_interfaces();
145     diag_printf("Start TCP server - test\n");
146     cyg_thread_resume(client_thread_handle);    // Start it
147 #if NLOOP > 0
148     server();
149     CYG_TEST_PASS_FINISH( "server returned OK" );
150 #endif
151     CYG_TEST_NA( "No loopback devs" );
152 }
153
154 void
155 tcp_client(cyg_addrword_t param)
156 {
157     diag_printf("Start TCP client - test\n");
158 #if NLOOP > 0
159     client(); 
160 #endif
161 }
162
163
164
165 void
166 cyg_start(void)
167 {
168     CYG_TEST_INIT();
169
170     cyg_thread_create(MAIN_THREAD_PRIORITY,     // Priority
171                       tcp_server,               // entry
172                       0,                        // entry parameter
173                       "TCP loopback server",    // Name
174                       &stack_server[0],         // Stack
175                       STACK_SIZE,               // Size
176                       &server_thread_handle,    // Handle
177                       &server_thread_data       // Thread data structure
178             );
179     cyg_thread_resume(server_thread_handle);    // Start it
180
181     cyg_thread_create(MAIN_THREAD_PRIORITY,     // Priority
182                       tcp_client,               // entry
183                       0,                        // entry parameter
184                       "TCP loopback client",    // Name
185                       &stack_client[0],         // Stack
186                       STACK_SIZE,               // Size
187                       &client_thread_handle,    // Handle
188                       &client_thread_data       // Thread data structure
189             );
190     cyg_scheduler_start();
191 }
192
193
194