1 //==========================================================================
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####
21 // Author(s): gthomas, hmt
22 // Contributors: gthomas
28 //####DESCRIPTIONEND####
30 //==========================================================================
32 #include <pkgconf/system.h>
33 #include <pkgconf/net.h>
35 #ifdef CYGBLD_DEVS_ETH_DEVICE_H // Get the device config if it exists
36 #include CYGBLD_DEVS_ETH_DEVICE_H // May provide CYGTST_DEVS_ETH_TEST_NET_REALTIME
39 #ifdef CYGPKG_NET_TESTS_USE_RT_TEST_HARNESS // do we use the rt test?
40 # ifdef CYGTST_DEVS_ETH_TEST_NET_REALTIME // Get the test ancilla if it exists
41 # include CYGTST_DEVS_ETH_TEST_NET_REALTIME
46 // Fill in the blanks if necessary
56 #ifndef TNR_PRINT_ACTIVITY
57 # define TNR_PRINT_ACTIVITY()
61 // FLOOD PING test code
65 #define MAX_PACKET 4096
68 #define STACK_SIZE (CYGNUM_HAL_STACK_SIZE_TYPICAL + MAX_PACKET + MAX_PACKET + 0x1000)
69 static char thread_stack[NUMTHREADS][STACK_SIZE];
70 static cyg_thread thread_data[NUMTHREADS];
71 static cyg_handle_t thread_handle[NUMTHREADS];
73 #define DO_DUMPSTATS( seq ) (0 == (0xffff & seq))
75 #ifdef CYGHWR_NET_DRIVER_ETH0
76 struct sockaddr_in host0;
78 #ifdef CYGHWR_NET_DRIVER_ETH1
79 struct sockaddr_in host1;
83 static int uniqueid[3] = { 0x1234, 0x4321, 0xdead };
85 static int ok_recv[3] = { 0,0,0 };
86 static int bogus_recv[3] = { 0,0,0 };
87 static int pings_sent[3] = { 0,0,0 };
89 extern void cyg_kmem_print_stats( void );
101 // ------------------------------------------------------------------------
102 static void dumpstats(void)
105 diag_printf( "------------------------\n" );
106 #ifdef CYGHWR_NET_DRIVER_ETH0
108 diag_printf("%16s: Sent %d packets, received %d OK, %d bad\n",
109 inet_ntoa(host0.sin_addr), pings_sent[0],
110 ok_recv[0], bogus_recv[0]);
113 #ifdef CYGHWR_NET_DRIVER_ETH1
115 diag_printf("%16s: Sent %d packets, received %d OK, %d bad\n",
116 inet_ntoa(host1.sin_addr), pings_sent[1],
117 ok_recv[1], bogus_recv[1]);
121 diag_printf("Wierd! %d unknown sends!\n", pings_sent[2] );
123 diag_printf("Wierd! %d unknown good recvs!\n", ok_recv[2] );
125 diag_printf("Wierd! %d unknown bogus recvs!\n", bogus_recv[2] );
126 cyg_kmem_print_stats();
127 diag_printf( "------------------------\n" );
132 // ------------------------------------------------------------------------
133 // Compute INET checksum
135 inet_cksum(u_short *addr, int len)
137 register int nleft = len;
138 register u_short *w = addr;
139 register u_short answer;
140 register u_int sum = 0;
141 u_short odd_byte = 0;
144 * Our algorithm is simple, using a 32 bit accumulator (sum),
145 * we add sequential 16 bit words to it, and at the end, fold
146 * back all the carry bits from the top 16 bits into the lower
154 /* mop up an odd byte, if necessary */
156 *(u_char *)(&odd_byte) = *(u_char *)w;
161 * add back carry outs from top 16 bits to low 16 bits
163 sum = (sum >> 16) + (sum & 0x0000ffff); /* add hi 16 to low 16 */
164 sum += (sum >> 16); /* add carry */
165 answer = ~sum; /* truncate to 16 bits */
169 // ------------------------------------------------------------------------
171 show_icmp(unsigned char *pkt, int len, struct sockaddr_in *from)
173 cyg_tick_count_t *tp, tv;
177 tv = cyg_current_time();
178 #ifdef CYGHWR_NET_DRIVER_ETH0
179 if (eth0_up && (from->sin_addr.s_addr == host0.sin_addr.s_addr) )
182 #ifdef CYGHWR_NET_DRIVER_ETH1
183 if (eth1_up && (from->sin_addr.s_addr == host1.sin_addr.s_addr) )
187 ip = (struct ip *)pkt;
188 if ((len < sizeof(*ip)) || ip->ip_v != IPVERSION) {
189 diag_printf("%s: Short packet or not IP! - Len: %d, Version: %d\n",
190 inet_ntoa(from->sin_addr), len, ip->ip_v);
194 icmp = (struct icmp *)(pkt + sizeof(*ip));
195 len -= (sizeof(*ip) + 8);
196 tp = (cyg_tick_count_t *)&icmp->icmp_data;
197 if (icmp->icmp_type != ICMP_ECHOREPLY) {
198 diag_printf("%s: Invalid ICMP - type: %d\n",
199 inet_ntoa(from->sin_addr), icmp->icmp_type);
204 if (icmp->icmp_id != uniqueid[which]) {
205 diag_printf("%s: ICMP received for wrong id - sent: %x, recvd: %x\n",
206 inet_ntoa(from->sin_addr), uniqueid[which], icmp->icmp_id);
208 // diag_printf("%d bytes from %s: ", len, inet_ntoa(from->sin_addr));
209 // diag_printf("icmp_seq=%d", icmp->icmp_seq);
210 // diag_printf(", time=%dms\n", (int)(tv - *tp)*10);
213 // ------------------------------------------------------------------------
215 floodrecv(cyg_addrword_t p)
217 unsigned char pkt[MAX_PACKET];
218 struct sockaddr_in from;
221 diag_printf("PING listener...\n" );
223 // Wait for a response
224 fromlen = sizeof(from);
225 len = recvfrom(sock, pkt, sizeof(pkt), 0,
226 (struct sockaddr *)&from, &fromlen);
230 show_icmp(pkt, len, &from);
234 // ------------------------------------------------------------------------
236 pingsend( int seq, struct sockaddr_in *host,
237 struct icmp *icmp, int icmp_len, int which )
239 cyg_tick_count_t *tp;
242 // Build ICMP packet for interface
243 icmp->icmp_type = ICMP_ECHO;
245 icmp->icmp_cksum = 0;
246 icmp->icmp_seq = seq;
247 icmp->icmp_id = uniqueid[which];
249 tp = (cyg_tick_count_t *)&icmp->icmp_data;
250 *tp++ = cyg_current_time();
252 for (i = sizeof(*tp); i < icmp_len; i += sizeof(*dp))
256 icmp->icmp_cksum = inet_cksum( (u_short *)icmp, icmp_len+8);
258 if (sendto(sock, icmp, icmp_len+8, MSG_DONTWAIT,
259 (struct sockaddr *)host, sizeof(*host)) < 0) {
265 // ------------------------------------------------------------------------
267 floodsend(cyg_addrword_t param)
269 #ifdef CYGHWR_NET_DRIVER_ETH0
270 unsigned char pkt0[MAX_PACKET];
271 struct icmp *icmp0 = (struct icmp *)pkt0;
273 #ifdef CYGHWR_NET_DRIVER_ETH1
274 unsigned char pkt1[MAX_PACKET];
275 struct icmp *icmp1 = (struct icmp *)pkt1;
281 for (seq = 0; 1 ; seq++) {
282 if ( DO_DUMPSTATS( seq ) )
285 #ifdef CYGHWR_NET_DRIVER_ETH0
287 pingsend( seq, &host0, icmp0, icmp_len, 0 );
289 #ifdef CYGHWR_NET_DRIVER_ETH1
291 pingsend( seq, &host1, icmp1, icmp_len, 1 );
297 // ------------------------------------------------------------------------
299 net_test(cyg_addrword_t param)
303 diag_printf("Start Flood PING test\n");
304 init_all_network_interfaces();
305 diag_printf("Interfaces up:\n");
306 cyg_kmem_print_stats();
310 if ((p = getprotobyname("icmp")) == (struct protoent *)0) {
311 perror("getprotobyname");
314 sock = socket(AF_INET, SOCK_RAW, p->p_proto);
320 #ifdef CYGHWR_NET_DRIVER_ETH0
322 host0.sin_family = AF_INET;
323 host0.sin_len = sizeof(host0);
324 host0.sin_addr = eth0_bootp_data.bp_siaddr;
326 diag_printf("PING server %16s\n", inet_ntoa(host0.sin_addr));
329 #ifdef CYGHWR_NET_DRIVER_ETH1
331 host1.sin_family = AF_INET;
332 host1.sin_len = sizeof(host1);
333 host1.sin_addr = eth1_bootp_data.bp_siaddr;
335 diag_printf("PING server %16s\n", inet_ntoa(host1.sin_addr));
339 cyg_thread_resume(thread_handle[1]);
340 cyg_thread_resume(thread_handle[2]);
342 cyg_thread_delay( 100 ); // let the other threads start and print
344 TNR_ON(); // then enable the test
346 cyg_thread_delay( 12000 ); // run for a couple of minutes
350 diag_printf("After running:\n");
352 TNR_PRINT_ACTIVITY();
356 // ------------------------------------------------------------------------
360 // Create a main thread, so we can run the scheduler and have time 'pass'
361 cyg_thread_create(10, // Priority - just a number
363 0, // entry parameter
364 "Network test", // Name
365 &thread_stack[0][0], // Stack
367 &thread_handle[0], // Handle
368 &thread_data[0] // Thread data structure
370 cyg_thread_resume(thread_handle[0]); // Start it
372 // Create the secondary threads
373 cyg_thread_create(11, // Priority - just a number
375 0, // entry parameter
376 "Flood Ping Recv", // Name
377 &thread_stack[1][0], // Stack
379 &thread_handle[1], // Handle
380 &thread_data[1] // Thread data structure
382 cyg_thread_create(12, // Priority - just a number
384 0, // entry parameter
385 "Flood Ping Send", // Name
386 &thread_stack[2][0], // Stack
388 &thread_handle[2], // Handle
389 &thread_data[2] // Thread data structure
393 cyg_scheduler_start();