]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/net/autotest/v2_0/tests/floodpingmux.c
Initial revision
[karo-tx-redboot.git] / packages / net / autotest / v2_0 / tests / floodpingmux.c
1 //==========================================================================
2 //
3 //      autotest/current/tests/floodpingmux.c
4 //
5 //      Flood pinging test, the server floods me and I flood hir.
6 //
7 //==========================================================================
8 //####ECOSGPLCOPYRIGHTBEGIN####
9 // -------------------------------------------
10 // This file is part of eCos, the Embedded Configurable Operating System.
11 // Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
12 //
13 // eCos is free software; you can redistribute it and/or modify it under
14 // the terms of the GNU General Public License as published by the Free
15 // Software Foundation; either version 2 or (at your option) any later version.
16 //
17 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
18 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
20 // for more details.
21 //
22 // You should have received a copy of the GNU General Public License along
23 // with eCos; if not, write to the Free Software Foundation, Inc.,
24 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
25 //
26 // As a special exception, if other files instantiate templates or use macros
27 // or inline functions from this file, or you compile this file and link it
28 // with other works to produce a work based on this file, this file does not
29 // by itself cause the resulting work to be covered by the GNU General Public
30 // License. However the source code for this file must still be made available
31 // in accordance with section (3) of the GNU General Public License.
32 //
33 // This exception does not invalidate any other reasons why a work based on
34 // this file might be covered by the GNU General Public License.
35 //
36 // -------------------------------------------
37 //####ECOSGPLCOPYRIGHTEND####
38 //####BSDCOPYRIGHTBEGIN####
39 //
40 // -------------------------------------------
41 //
42 // Portions of this software may have been derived from OpenBSD or other sources,
43 // and are covered by the appropriate copyright disclaimers included herein.
44 //
45 // -------------------------------------------
46 //
47 //####BSDCOPYRIGHTEND####
48 //==========================================================================
49 //#####DESCRIPTIONBEGIN####
50 //
51 // Author(s):    hmt,gthomas
52 // Contributors: hmt,gthomas
53 // Date:         2000-10-23
54 // Purpose:      
55 // Description:  
56 //              
57 //
58 //####DESCRIPTIONEND####
59 //
60 //==========================================================================
61
62 #include <pkgconf/system.h>
63
64 #include <cyg/infra/testcase.h>         // testing infrastructure
65
66 #include <pkgconf/net.h>
67
68 #ifdef CYGBLD_DEVS_ETH_DEVICE_H    // Get the device config if it exists
69 #include CYGBLD_DEVS_ETH_DEVICE_H  // May provide CYGTST_DEVS_ETH_TEST_NET_REALTIME
70 #endif
71
72 #ifdef CYGPKG_NET_TESTS_USE_RT_TEST_HARNESS // do we use the rt test?
73 # ifdef CYGTST_DEVS_ETH_TEST_NET_REALTIME // Get the test ancilla if it exists
74 #  include CYGTST_DEVS_ETH_TEST_NET_REALTIME
75 # endif
76 #endif
77
78
79 // Fill in the blanks if necessary
80 #ifndef TNR_OFF
81 # define TNR_OFF()
82 #endif
83 #ifndef TNR_ON
84 # define TNR_ON()
85 #endif
86 #ifndef TNR_INIT
87 # define TNR_INIT()
88 #endif
89 #ifndef TNR_PRINT_ACTIVITY
90 # define TNR_PRINT_ACTIVITY()
91 #endif
92
93 // ------------------------------------------------------------------------
94
95 #include <errno.h>
96 #include <network.h>
97
98 #define MAX_PACKET 4096
99
100 #define NUMTHREADS 3
101 #define STACK_SIZE (CYGNUM_HAL_STACK_SIZE_TYPICAL + MAX_PACKET + MAX_PACKET + 0x1000)
102 static char thread_stack[NUMTHREADS][STACK_SIZE];
103 static cyg_thread thread_data[NUMTHREADS];
104 static cyg_handle_t thread_handle[NUMTHREADS];
105
106 #define DO_DUMPSTATS( seq ) (0 == (0xffff & seq))
107
108 #ifdef CYGHWR_NET_DRIVER_ETH0
109 struct sockaddr_in host0;
110 #endif
111 #ifdef CYGHWR_NET_DRIVER_ETH1
112 struct sockaddr_in host1;
113 #endif
114 static int sock;
115
116 static int uniqueid[3] = { 0x1234, 0x4321, 0xdead };
117
118 static int ok_recv[3] = { 0,0,0 };
119 static int bogus_recv[3] = { 0,0,0 };
120 static int pings_sent[3] = { 0,0,0 };
121
122 // ------------------------------------------------------------------------
123 static void dumpstats(void)
124 {
125     TNR_OFF();
126     diag_printf( "------------------------\n" );
127 #ifdef CYGHWR_NET_DRIVER_ETH0
128     if (eth0_up) {
129         diag_printf("%16s: Sent %d packets, received %d OK, %d bad\n",
130                     inet_ntoa(host0.sin_addr), pings_sent[0],
131                     ok_recv[0], bogus_recv[0]);
132     }
133 #endif
134 #ifdef CYGHWR_NET_DRIVER_ETH1
135     if (eth1_up) {
136         diag_printf("%16s: Sent %d packets, received %d OK, %d bad\n",
137                     inet_ntoa(host1.sin_addr), pings_sent[1],
138                     ok_recv[1], bogus_recv[1]);
139     }
140 #endif
141     if ( pings_sent[2] )
142         diag_printf("Wierd!  %d unknown sends!\n", pings_sent[2] );
143     if ( ok_recv[2] )
144         diag_printf("Wierd!  %d unknown good recvs!\n", ok_recv[2] );
145     if ( bogus_recv[2] )
146         diag_printf("Wierd!  %d unknown bogus recvs!\n", bogus_recv[2] );
147     diag_printf( "------------------------\n" );
148     TNR_ON();
149 }
150
151
152 // ------------------------------------------------------------------------
153 // Compute INET checksum
154 int
155 inet_cksum(u_short *addr, int len)
156 {
157     register int nleft = len;
158     register u_short *w = addr;
159     register u_short answer;
160     register u_int sum = 0;
161     u_short odd_byte = 0;
162
163     /*
164      *  Our algorithm is simple, using a 32 bit accumulator (sum),
165      *  we add sequential 16 bit words to it, and at the end, fold
166      *  back all the carry bits from the top 16 bits into the lower
167      *  16 bits.
168      */
169     while( nleft > 1 )  {
170         sum += *w++;
171         nleft -= 2;
172     }
173
174     /* mop up an odd byte, if necessary */
175     if( nleft == 1 ) {
176         *(u_char *)(&odd_byte) = *(u_char *)w;
177         sum += odd_byte;
178     }
179
180     /*
181      * add back carry outs from top 16 bits to low 16 bits
182      */
183     sum = (sum >> 16) + (sum & 0x0000ffff); /* add hi 16 to low 16 */
184     sum += (sum >> 16);                     /* add carry */
185     answer = ~sum;                          /* truncate to 16 bits */
186     return (answer);
187 }
188
189 // ------------------------------------------------------------------------
190 static void
191 show_icmp(unsigned char *pkt, int len, struct sockaddr_in *from)
192 {
193     cyg_tick_count_t *tp, tv;
194     struct ip *ip;
195     struct icmp *icmp;
196     int which = 2;
197     tv = cyg_current_time();
198 #ifdef CYGHWR_NET_DRIVER_ETH0
199     if (eth0_up && (from->sin_addr.s_addr == host0.sin_addr.s_addr) )
200         which = 0;
201 #endif
202 #ifdef CYGHWR_NET_DRIVER_ETH1
203     if (eth1_up && (from->sin_addr.s_addr == host1.sin_addr.s_addr) )
204         which = 1;
205 #endif
206
207     ip = (struct ip *)pkt;
208     if ((len < sizeof(*ip)) || ip->ip_v != IPVERSION) {
209         TNR_OFF();
210         diag_printf("%s: Short packet or not IP! - Len: %d, Version: %d\n", 
211                     inet_ntoa(from->sin_addr), len, ip->ip_v);
212         TNR_ON();
213         bogus_recv[which]++;
214         return;
215     }
216     icmp = (struct icmp *)(pkt + sizeof(*ip));
217     len -= (sizeof(*ip) + 8);
218     tp = (cyg_tick_count_t *)&icmp->icmp_data;
219     if (icmp->icmp_type != ICMP_ECHOREPLY) { 
220         TNR_OFF();
221         diag_printf("%s: Invalid ICMP - type: %d\n", 
222                     inet_ntoa(from->sin_addr), icmp->icmp_type);
223         TNR_ON();
224         bogus_recv[which]++;
225         return;
226     }
227     ok_recv[which]++;
228     if (icmp->icmp_id != uniqueid[which]) {
229         TNR_OFF();
230         diag_printf("%s: ICMP received for wrong id - sent: %x, recvd: %x\n", 
231                     inet_ntoa(from->sin_addr), uniqueid[which], icmp->icmp_id);
232         TNR_ON();
233     }
234 //    diag_printf("%d bytes from %s: ", len, inet_ntoa(from->sin_addr));
235 //    diag_printf("icmp_seq=%d", icmp->icmp_seq);
236 //    diag_printf(", time=%dms\n", (int)(tv - *tp)*10);
237 }
238
239 // ------------------------------------------------------------------------
240 static void
241 floodrecv(cyg_addrword_t p)
242 {
243     unsigned char pkt[MAX_PACKET];
244     struct sockaddr_in from;
245     int len, fromlen;
246
247     diag_printf("PING listener...\n" );
248     for (;;) {
249         // Wait for a response
250         fromlen = sizeof(from);
251         len = recvfrom(sock, pkt, sizeof(pkt), 0,
252                        (struct sockaddr *)&from, &fromlen);
253         if (len < 0)
254             perror("recvfrom");
255         else
256             show_icmp(pkt, len, &from);
257     }
258 }
259
260 // ------------------------------------------------------------------------
261 static void
262 pingsend( int seq, struct sockaddr_in *host,
263           struct icmp *icmp, int icmp_len, int which )
264 {
265     cyg_tick_count_t *tp;
266     long *dp;
267     int i;
268     // Build ICMP packet for interface
269     icmp->icmp_type = ICMP_ECHO;
270     icmp->icmp_code = 0;
271     icmp->icmp_cksum = 0;
272     icmp->icmp_seq = seq;
273     icmp->icmp_id = uniqueid[which];
274     // Set up ping data
275     tp = (cyg_tick_count_t *)&icmp->icmp_data;
276     *tp++ = cyg_current_time();
277     dp = (long *)tp;
278     for (i = sizeof(*tp);  i < icmp_len;  i += sizeof(*dp))
279         *dp++ = i;
280
281     // Add checksum
282     icmp->icmp_cksum = inet_cksum( (u_short *)icmp, icmp_len+8);
283     // Send it off
284     if (sendto(sock, icmp, icmp_len+8, MSG_DONTWAIT,
285               (struct sockaddr *)host, sizeof(*host)) < 0) {
286         perror("sendto");
287     }
288     pings_sent[which]++;
289 }
290
291 // ------------------------------------------------------------------------
292 static void
293 floodsend(cyg_addrword_t param)
294 {
295     unsigned char pkt0[MAX_PACKET], pkt1[MAX_PACKET];
296
297     struct icmp *icmp0 = (struct icmp *)pkt0;
298     struct icmp *icmp1 = (struct icmp *)pkt1;
299
300     int icmp_len = 64;
301     int seq;
302
303     for (seq = 0; 1 ; seq++) {
304         if ( DO_DUMPSTATS( seq ) )
305             dumpstats();
306
307 #ifdef CYGHWR_NET_DRIVER_ETH0
308         if (eth0_up)
309             pingsend( seq, &host0, icmp0, icmp_len, 0 );
310 #endif
311 #ifdef CYGHWR_NET_DRIVER_ETH1
312         if (eth1_up)
313             pingsend( seq, &host1, icmp1, icmp_len, 1 );
314 #endif
315     }
316 }
317
318
319 // ------------------------------------------------------------------------
320
321 #include "autohost.inl"
322
323 static void
324 do_ping_tests(struct bootp *bp, int testtime, int filesize)
325 {
326     char order[256];
327     diag_printf( "INFO: telling %s to run 1 flood ping for %d seconds, %d bytes\n",
328           inet_ntoa(bp->bp_siaddr), testtime, filesize );
329
330     sprintf( order, "%s %s %d %d", "FLOOD_PING",
331              inet_ntoa(bp->bp_yiaddr),
332              testtime,
333              filesize );
334     autohost_tell( bp, order );
335 }
336
337 // ------------------------------------------------------------------------
338
339 #define TESTTIME (5 * 60) // Seconds
340
341 void
342 net_test(cyg_addrword_t param)
343 {
344     struct protoent *p;
345
346     diag_printf("Start Flood PING test\n");
347     init_all_network_interfaces();
348     diag_printf("Interfaces up:\n");
349
350     if ((p = getprotobyname("icmp")) == (struct protoent *)0) {
351         perror("getprotobyname");
352         return;
353     }
354     sock = socket(AF_INET, SOCK_RAW, p->p_proto);
355     if (sock < 0) {
356         perror("tx socket");
357         return;
358     }
359
360 #ifdef CYGHWR_NET_DRIVER_ETH0
361     if (eth0_up) {
362         host0.sin_family = AF_INET;
363         host0.sin_len = sizeof(host0);
364         host0.sin_addr = eth0_bootp_data.bp_siaddr;
365         host0.sin_port = 0;
366         diag_printf("PING server %16s\n", inet_ntoa(host0.sin_addr));
367     }
368 #endif
369 #ifdef CYGHWR_NET_DRIVER_ETH1
370     if (eth1_up) {
371         host1.sin_family = AF_INET;
372         host1.sin_len = sizeof(host1);
373         host1.sin_addr = eth1_bootp_data.bp_siaddr;
374         host1.sin_port = 0;
375         diag_printf("PING server %16s\n", inet_ntoa(host1.sin_addr));
376     }
377 #endif
378     autohost_init();
379
380     TNR_INIT();
381     
382     // Now command the host to do ping to us...
383 #ifdef CYGHWR_NET_DRIVER_ETH0
384     if (eth0_up) {
385         do_ping_tests(&eth0_bootp_data, TESTTIME, 56);
386     }
387 #endif
388 #ifdef CYGHWR_NET_DRIVER_ETH1
389     if (eth1_up) {
390         do_ping_tests(&eth1_bootp_data, TESTTIME, 56);
391     }
392 #endif
393
394     // And start the threads that make us ping them, and count the results
395     cyg_thread_resume(thread_handle[1]);
396     cyg_thread_resume(thread_handle[2]);
397
398     // Let the server run for 5 minutes
399     cyg_thread_delay(2*100); // let the stuff start up first
400     TNR_ON();
401     cyg_thread_delay(TESTTIME*100); // FIXME - assume cS clock.
402
403     // Stop the threads that make us ping them.  Thread [2] is the pinger,
404     // thread [1] the receiver so that might as well continue.
405     cyg_thread_suspend(thread_handle[2]);
406
407     // Additional delay 'cos host may be slower than us - and it has to
408     // complete a transfer anyway:
409     cyg_thread_delay(  30    *100); // FIXME - assume cS clock.
410     TNR_OFF();
411
412     autohost_end( 0
413 #ifdef CYGHWR_NET_DRIVER_ETH0
414                   + eth0_up
415 #endif
416 #ifdef CYGHWR_NET_DRIVER_ETH1
417                   + eth1_up
418 #endif
419         ); // check for 2 pass messages from hosts
420
421     diag_printf("After running:\n");
422     dumpstats();
423     TNR_OFF(); // dumpstats() turned it on again!
424     TNR_PRINT_ACTIVITY();
425     CYG_TEST_EXIT("Done");
426 }
427
428 void
429 cyg_start(void)
430 {
431     // Create a main thread, so we can run the scheduler and have time 'pass'
432     cyg_thread_create(10,                // Priority - just a number
433                       net_test,          // entry
434                       0,                 // entry parameter
435                       "Network test",    // Name
436                      &thread_stack[0][0], // Stack
437                       STACK_SIZE,        // Size
438                       &thread_handle[0], // Handle
439                       &thread_data[0]    // Thread data structure
440             );
441     cyg_thread_resume(thread_handle[0]);  // Start it
442
443     // Create the secondary threads
444     cyg_thread_create(11,                // Priority - just a number
445                       floodrecv,         // entry
446                       0,                 // entry parameter
447                       "Flood Ping Recv", // Name
448                      &thread_stack[1][0], // Stack
449                       STACK_SIZE,        // Size
450                       &thread_handle[1], // Handle
451                       &thread_data[1]    // Thread data structure
452             );
453     cyg_thread_create(12,                // Priority - just a number
454                       floodsend,         // entry
455                       0,                 // entry parameter
456                       "Flood Ping Send", // Name
457                      &thread_stack[2][0], // Stack
458                       STACK_SIZE,        // Size
459                       &thread_handle[2], // Handle
460                       &thread_data[2]    // Thread data structure
461             );
462
463     cyg_scheduler_start();
464 }
465
466 // EOF floodpingmux.c