]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/net/common/v2_0/tests/flood.c
unified MX27, MX25, MX37 trees
[karo-tx-redboot.git] / packages / net / common / v2_0 / tests / flood.c
1 //==========================================================================
2 //
3 //      tests/flood.c
4 //
5 //      Flood PING 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):    gthomas, hmt
22 // Contributors: gthomas
23 // Date:         2000-05-03
24 // Purpose:      
25 // Description:  
26 //              
27 //
28 //####DESCRIPTIONEND####
29 //
30 //==========================================================================
31
32 #include <pkgconf/system.h>
33 #include <pkgconf/net.h>
34
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
37 #endif
38
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
42 # endif
43 #endif
44
45
46 // Fill in the blanks if necessary
47 #ifndef TNR_OFF
48 # define TNR_OFF()
49 #endif
50 #ifndef TNR_ON
51 # define TNR_ON()
52 #endif
53 #ifndef TNR_INIT
54 # define TNR_INIT()
55 #endif
56 #ifndef TNR_PRINT_ACTIVITY
57 # define TNR_PRINT_ACTIVITY()
58 #endif
59
60
61 // FLOOD PING test code
62
63 #include <network.h>
64
65 #define MAX_PACKET 4096
66
67 #define NUMTHREADS 3
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];
72
73 #define DO_DUMPSTATS( seq ) (0 == (0xffff & seq))
74
75 #ifdef CYGHWR_NET_DRIVER_ETH0
76 struct sockaddr_in host0;
77 #endif
78 #ifdef CYGHWR_NET_DRIVER_ETH1
79 struct sockaddr_in host1;
80 #endif
81 static int sock;
82
83 static int uniqueid[3] = { 0x1234, 0x4321, 0xdead };
84
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 };
88
89 extern void cyg_kmem_print_stats( void );
90
91 extern void
92 cyg_test_exit(void);
93
94 void
95 pexit(char *s)
96 {
97     perror(s);
98     cyg_test_exit();
99 }
100
101 // ------------------------------------------------------------------------
102 static void dumpstats(void)
103 {
104     TNR_OFF();
105     diag_printf( "------------------------\n" );
106 #ifdef CYGHWR_NET_DRIVER_ETH0
107     if (eth0_up) {
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]);
111     }
112 #endif
113 #ifdef CYGHWR_NET_DRIVER_ETH1
114     if (eth1_up) {
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]);
118     }
119 #endif
120     if ( pings_sent[2] )
121         diag_printf("Wierd!  %d unknown sends!\n", pings_sent[2] );
122     if ( ok_recv[2] )
123         diag_printf("Wierd!  %d unknown good recvs!\n", ok_recv[2] );
124     if ( bogus_recv[2] )
125         diag_printf("Wierd!  %d unknown bogus recvs!\n", bogus_recv[2] );
126     cyg_kmem_print_stats();
127     diag_printf( "------------------------\n" );
128     TNR_ON();
129 }
130
131
132 // ------------------------------------------------------------------------
133 // Compute INET checksum
134 int
135 inet_cksum(u_short *addr, int len)
136 {
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;
142
143     /*
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
147      *  16 bits.
148      */
149     while( nleft > 1 )  {
150         sum += *w++;
151         nleft -= 2;
152     }
153
154     /* mop up an odd byte, if necessary */
155     if( nleft == 1 ) {
156         *(u_char *)(&odd_byte) = *(u_char *)w;
157         sum += odd_byte;
158     }
159
160     /*
161      * add back carry outs from top 16 bits to low 16 bits
162      */
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 */
166     return (answer);
167 }
168
169 // ------------------------------------------------------------------------
170 static void
171 show_icmp(unsigned char *pkt, int len, struct sockaddr_in *from)
172 {
173     cyg_tick_count_t *tp, tv;
174     struct ip *ip;
175     struct icmp *icmp;
176     int which = 2;
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) )
180         which = 0;
181 #endif
182 #ifdef CYGHWR_NET_DRIVER_ETH1
183     if (eth1_up && (from->sin_addr.s_addr == host1.sin_addr.s_addr) )
184         which = 1;
185 #endif
186
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);
191         bogus_recv[which]++;
192         return;
193     }
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);
200         bogus_recv[which]++;
201         return;
202     }
203     ok_recv[which]++;
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);
207     }
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);
211 }
212
213 // ------------------------------------------------------------------------
214 static void
215 floodrecv(cyg_addrword_t p)
216 {
217     unsigned char pkt[MAX_PACKET];
218     struct sockaddr_in from;
219     int len; 
220     socklen_t fromlen;
221
222     diag_printf("PING listener...\n" );
223     for (;;) {
224         // Wait for a response
225         fromlen = sizeof(from);
226         len = recvfrom(sock, pkt, sizeof(pkt), 0,
227                        (struct sockaddr *)&from, &fromlen);
228         if (len < 0)
229             perror("recvfrom");
230         else
231             show_icmp(pkt, len, &from);
232     }
233 }
234
235 // ------------------------------------------------------------------------
236 static void
237 pingsend( int seq, struct sockaddr_in *host,
238           struct icmp *icmp, int icmp_len, int which )
239 {
240     cyg_tick_count_t *tp;
241     long *dp;
242     int i;
243     // Build ICMP packet for interface
244     icmp->icmp_type = ICMP_ECHO;
245     icmp->icmp_code = 0;
246     icmp->icmp_cksum = 0;
247     icmp->icmp_seq = seq;
248     icmp->icmp_id = uniqueid[which];
249     // Set up ping data
250     tp = (cyg_tick_count_t *)&icmp->icmp_data;
251     *tp++ = cyg_current_time();
252     dp = (long *)tp;
253     for (i = sizeof(*tp);  i < icmp_len;  i += sizeof(*dp))
254         *dp++ = i;
255
256     // Add checksum
257     icmp->icmp_cksum = inet_cksum( (u_short *)icmp, icmp_len+8);
258     // Send it off
259     if (sendto(sock, icmp, icmp_len+8, MSG_DONTWAIT,
260               (struct sockaddr *)host, sizeof(*host)) < 0) {
261         perror("sendto");
262     }
263     pings_sent[which]++;
264 }
265
266 // ------------------------------------------------------------------------
267 static void
268 floodsend(cyg_addrword_t param)
269 {
270 #ifdef CYGHWR_NET_DRIVER_ETH0
271     unsigned char pkt0[MAX_PACKET];
272     struct icmp *icmp0 = (struct icmp *)pkt0;
273 #endif
274 #ifdef CYGHWR_NET_DRIVER_ETH1
275     unsigned char pkt1[MAX_PACKET];
276     struct icmp *icmp1 = (struct icmp *)pkt1;
277 #endif
278
279     int icmp_len = 64;
280     int seq;
281
282     for (seq = 0; 1 ; seq++) {
283         if ( DO_DUMPSTATS( seq ) )
284             dumpstats();
285
286 #ifdef CYGHWR_NET_DRIVER_ETH0
287         if (eth0_up)
288             pingsend( seq, &host0, icmp0, icmp_len, 0 );
289 #endif
290 #ifdef CYGHWR_NET_DRIVER_ETH1
291         if (eth1_up)
292             pingsend( seq, &host1, icmp1, icmp_len, 1 );
293 #endif
294     }
295 }
296
297
298 // ------------------------------------------------------------------------
299 void
300 net_test(cyg_addrword_t param)
301 {
302     struct protoent *p;
303
304     diag_printf("Start Flood PING test\n");
305     init_all_network_interfaces();
306     diag_printf("Interfaces up:\n");
307     cyg_kmem_print_stats();
308
309     TNR_INIT();
310
311     if ((p = getprotobyname("icmp")) == (struct protoent *)0) {
312         perror("getprotobyname");
313         return;
314     }
315     sock = socket(AF_INET, SOCK_RAW, p->p_proto);
316     if (sock < 0) {
317         perror("tx socket");
318         return;
319     }
320
321 #ifdef CYGHWR_NET_DRIVER_ETH0
322     if (eth0_up) {
323         host0.sin_family = AF_INET;
324         host0.sin_len = sizeof(host0);
325         host0.sin_addr = eth0_bootp_data.bp_siaddr;
326         host0.sin_port = 0;
327         diag_printf("PING server %16s\n", inet_ntoa(host0.sin_addr));
328     }
329 #endif
330 #ifdef CYGHWR_NET_DRIVER_ETH1
331     if (eth1_up) {
332         host1.sin_family = AF_INET;
333         host1.sin_len = sizeof(host1);
334         host1.sin_addr = eth1_bootp_data.bp_siaddr;
335         host1.sin_port = 0;
336         diag_printf("PING server %16s\n", inet_ntoa(host1.sin_addr));
337     }
338 #endif
339
340     cyg_thread_resume(thread_handle[1]);
341     cyg_thread_resume(thread_handle[2]);
342
343     cyg_thread_delay( 100 ); // let the other threads start and print
344
345     TNR_ON();                // then enable the test
346
347     cyg_thread_delay( 12000 ); // run for a couple of minutes
348
349     TNR_OFF();
350
351     diag_printf("After running:\n");
352     dumpstats();
353     TNR_PRINT_ACTIVITY();
354     cyg_test_exit();
355 }
356
357 // ------------------------------------------------------------------------
358 void
359 cyg_start(void)
360 {
361     // Create a main thread, so we can run the scheduler and have time 'pass'
362     cyg_thread_create(10,                // Priority - just a number
363                       net_test,          // entry
364                       0,                 // entry parameter
365                       "Network test",    // Name
366                      &thread_stack[0][0], // Stack
367                       STACK_SIZE,        // Size
368                       &thread_handle[0], // Handle
369                       &thread_data[0]    // Thread data structure
370             );
371     cyg_thread_resume(thread_handle[0]);  // Start it
372
373     // Create the secondary threads
374     cyg_thread_create(11,                // Priority - just a number
375                       floodrecv,         // entry
376                       0,                 // entry parameter
377                       "Flood Ping Recv", // Name
378                      &thread_stack[1][0], // Stack
379                       STACK_SIZE,        // Size
380                       &thread_handle[1], // Handle
381                       &thread_data[1]    // Thread data structure
382             );
383     cyg_thread_create(12,                // Priority - just a number
384                       floodsend,         // entry
385                       0,                 // entry parameter
386                       "Flood Ping Send", // Name
387                      &thread_stack[2][0], // Stack
388                       STACK_SIZE,        // Size
389                       &thread_handle[2], // Handle
390                       &thread_data[2]    // Thread data structure
391             );
392
393
394     cyg_scheduler_start();
395 }
396
397 // EOF flood.c
398