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;
222 diag_printf("PING listener...\n" );
224 // Wait for a response
225 fromlen = sizeof(from);
226 len = recvfrom(sock, pkt, sizeof(pkt), 0,
227 (struct sockaddr *)&from, &fromlen);
231 show_icmp(pkt, len, &from);
235 // ------------------------------------------------------------------------
237 pingsend( int seq, struct sockaddr_in *host,
238 struct icmp *icmp, int icmp_len, int which )
240 cyg_tick_count_t *tp;
243 // Build ICMP packet for interface
244 icmp->icmp_type = ICMP_ECHO;
246 icmp->icmp_cksum = 0;
247 icmp->icmp_seq = seq;
248 icmp->icmp_id = uniqueid[which];
250 tp = (cyg_tick_count_t *)&icmp->icmp_data;
251 *tp++ = cyg_current_time();
253 for (i = sizeof(*tp); i < icmp_len; i += sizeof(*dp))
257 icmp->icmp_cksum = inet_cksum( (u_short *)icmp, icmp_len+8);
259 if (sendto(sock, icmp, icmp_len+8, MSG_DONTWAIT,
260 (struct sockaddr *)host, sizeof(*host)) < 0) {
266 // ------------------------------------------------------------------------
268 floodsend(cyg_addrword_t param)
270 #ifdef CYGHWR_NET_DRIVER_ETH0
271 unsigned char pkt0[MAX_PACKET];
272 struct icmp *icmp0 = (struct icmp *)pkt0;
274 #ifdef CYGHWR_NET_DRIVER_ETH1
275 unsigned char pkt1[MAX_PACKET];
276 struct icmp *icmp1 = (struct icmp *)pkt1;
282 for (seq = 0; 1 ; seq++) {
283 if ( DO_DUMPSTATS( seq ) )
286 #ifdef CYGHWR_NET_DRIVER_ETH0
288 pingsend( seq, &host0, icmp0, icmp_len, 0 );
290 #ifdef CYGHWR_NET_DRIVER_ETH1
292 pingsend( seq, &host1, icmp1, icmp_len, 1 );
298 // ------------------------------------------------------------------------
300 net_test(cyg_addrword_t param)
304 diag_printf("Start Flood PING test\n");
305 init_all_network_interfaces();
306 diag_printf("Interfaces up:\n");
307 cyg_kmem_print_stats();
311 if ((p = getprotobyname("icmp")) == (struct protoent *)0) {
312 perror("getprotobyname");
315 sock = socket(AF_INET, SOCK_RAW, p->p_proto);
321 #ifdef CYGHWR_NET_DRIVER_ETH0
323 host0.sin_family = AF_INET;
324 host0.sin_len = sizeof(host0);
325 host0.sin_addr = eth0_bootp_data.bp_siaddr;
327 diag_printf("PING server %16s\n", inet_ntoa(host0.sin_addr));
330 #ifdef CYGHWR_NET_DRIVER_ETH1
332 host1.sin_family = AF_INET;
333 host1.sin_len = sizeof(host1);
334 host1.sin_addr = eth1_bootp_data.bp_siaddr;
336 diag_printf("PING server %16s\n", inet_ntoa(host1.sin_addr));
340 cyg_thread_resume(thread_handle[1]);
341 cyg_thread_resume(thread_handle[2]);
343 cyg_thread_delay( 100 ); // let the other threads start and print
345 TNR_ON(); // then enable the test
347 cyg_thread_delay( 12000 ); // run for a couple of minutes
351 diag_printf("After running:\n");
353 TNR_PRINT_ACTIVITY();
357 // ------------------------------------------------------------------------
361 // Create a main thread, so we can run the scheduler and have time 'pass'
362 cyg_thread_create(10, // Priority - just a number
364 0, // entry parameter
365 "Network test", // Name
366 &thread_stack[0][0], // Stack
368 &thread_handle[0], // Handle
369 &thread_data[0] // Thread data structure
371 cyg_thread_resume(thread_handle[0]); // Start it
373 // Create the secondary threads
374 cyg_thread_create(11, // Priority - just a number
376 0, // entry parameter
377 "Flood Ping Recv", // Name
378 &thread_stack[1][0], // Stack
380 &thread_handle[1], // Handle
381 &thread_data[1] // Thread data structure
383 cyg_thread_create(12, // Priority - just a number
385 0, // entry parameter
386 "Flood Ping Send", // Name
387 &thread_stack[2][0], // Stack
389 &thread_handle[2], // Handle
390 &thread_data[2] // Thread data structure
394 cyg_scheduler_start();