1 //==========================================================================
3 // autotest/current/tests/floodpingmux.c
5 // Flood pinging test, the server floods me and I flood hir.
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.
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.
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
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.
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.
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.
36 // -------------------------------------------
37 //####ECOSGPLCOPYRIGHTEND####
38 //####BSDCOPYRIGHTBEGIN####
40 // -------------------------------------------
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.
45 // -------------------------------------------
47 //####BSDCOPYRIGHTEND####
48 //==========================================================================
49 //#####DESCRIPTIONBEGIN####
51 // Author(s): hmt,gthomas
52 // Contributors: hmt,gthomas
58 //####DESCRIPTIONEND####
60 //==========================================================================
62 #include <pkgconf/system.h>
64 #include <cyg/infra/testcase.h> // testing infrastructure
66 #include <pkgconf/net.h>
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
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
79 // Fill in the blanks if necessary
89 #ifndef TNR_PRINT_ACTIVITY
90 # define TNR_PRINT_ACTIVITY()
93 // ------------------------------------------------------------------------
98 #define MAX_PACKET 4096
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];
106 #define DO_DUMPSTATS( seq ) (0 == (0xffff & seq))
108 #ifdef CYGHWR_NET_DRIVER_ETH0
109 struct sockaddr_in host0;
111 #ifdef CYGHWR_NET_DRIVER_ETH1
112 struct sockaddr_in host1;
116 static int uniqueid[3] = { 0x1234, 0x4321, 0xdead };
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 };
122 // ------------------------------------------------------------------------
123 static void dumpstats(void)
126 diag_printf( "------------------------\n" );
127 #ifdef CYGHWR_NET_DRIVER_ETH0
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]);
134 #ifdef CYGHWR_NET_DRIVER_ETH1
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]);
142 diag_printf("Wierd! %d unknown sends!\n", pings_sent[2] );
144 diag_printf("Wierd! %d unknown good recvs!\n", ok_recv[2] );
146 diag_printf("Wierd! %d unknown bogus recvs!\n", bogus_recv[2] );
147 diag_printf( "------------------------\n" );
152 // ------------------------------------------------------------------------
153 // Compute INET checksum
155 inet_cksum(u_short *addr, int len)
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;
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
174 /* mop up an odd byte, if necessary */
176 *(u_char *)(&odd_byte) = *(u_char *)w;
181 * add back carry outs from top 16 bits to low 16 bits
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 */
189 // ------------------------------------------------------------------------
191 show_icmp(unsigned char *pkt, int len, struct sockaddr_in *from)
193 cyg_tick_count_t *tp, tv;
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) )
202 #ifdef CYGHWR_NET_DRIVER_ETH1
203 if (eth1_up && (from->sin_addr.s_addr == host1.sin_addr.s_addr) )
207 ip = (struct ip *)pkt;
208 if ((len < sizeof(*ip)) || ip->ip_v != IPVERSION) {
210 diag_printf("%s: Short packet or not IP! - Len: %d, Version: %d\n",
211 inet_ntoa(from->sin_addr), len, ip->ip_v);
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) {
221 diag_printf("%s: Invalid ICMP - type: %d\n",
222 inet_ntoa(from->sin_addr), icmp->icmp_type);
228 if (icmp->icmp_id != uniqueid[which]) {
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);
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);
239 // ------------------------------------------------------------------------
241 floodrecv(cyg_addrword_t p)
243 unsigned char pkt[MAX_PACKET];
244 struct sockaddr_in from;
247 diag_printf("PING listener...\n" );
249 // Wait for a response
250 fromlen = sizeof(from);
251 len = recvfrom(sock, pkt, sizeof(pkt), 0,
252 (struct sockaddr *)&from, &fromlen);
256 show_icmp(pkt, len, &from);
260 // ------------------------------------------------------------------------
262 pingsend( int seq, struct sockaddr_in *host,
263 struct icmp *icmp, int icmp_len, int which )
265 cyg_tick_count_t *tp;
268 // Build ICMP packet for interface
269 icmp->icmp_type = ICMP_ECHO;
271 icmp->icmp_cksum = 0;
272 icmp->icmp_seq = seq;
273 icmp->icmp_id = uniqueid[which];
275 tp = (cyg_tick_count_t *)&icmp->icmp_data;
276 *tp++ = cyg_current_time();
278 for (i = sizeof(*tp); i < icmp_len; i += sizeof(*dp))
282 icmp->icmp_cksum = inet_cksum( (u_short *)icmp, icmp_len+8);
284 if (sendto(sock, icmp, icmp_len+8, MSG_DONTWAIT,
285 (struct sockaddr *)host, sizeof(*host)) < 0) {
291 // ------------------------------------------------------------------------
293 floodsend(cyg_addrword_t param)
295 unsigned char pkt0[MAX_PACKET], pkt1[MAX_PACKET];
297 struct icmp *icmp0 = (struct icmp *)pkt0;
298 struct icmp *icmp1 = (struct icmp *)pkt1;
303 for (seq = 0; 1 ; seq++) {
304 if ( DO_DUMPSTATS( seq ) )
307 #ifdef CYGHWR_NET_DRIVER_ETH0
309 pingsend( seq, &host0, icmp0, icmp_len, 0 );
311 #ifdef CYGHWR_NET_DRIVER_ETH1
313 pingsend( seq, &host1, icmp1, icmp_len, 1 );
319 // ------------------------------------------------------------------------
321 #include "autohost.inl"
324 do_ping_tests(struct bootp *bp, int testtime, int filesize)
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 );
330 sprintf( order, "%s %s %d %d", "FLOOD_PING",
331 inet_ntoa(bp->bp_yiaddr),
334 autohost_tell( bp, order );
337 // ------------------------------------------------------------------------
339 #define TESTTIME (5 * 60) // Seconds
342 net_test(cyg_addrword_t param)
346 diag_printf("Start Flood PING test\n");
347 init_all_network_interfaces();
348 diag_printf("Interfaces up:\n");
350 if ((p = getprotobyname("icmp")) == (struct protoent *)0) {
351 perror("getprotobyname");
354 sock = socket(AF_INET, SOCK_RAW, p->p_proto);
360 #ifdef CYGHWR_NET_DRIVER_ETH0
362 host0.sin_family = AF_INET;
363 host0.sin_len = sizeof(host0);
364 host0.sin_addr = eth0_bootp_data.bp_siaddr;
366 diag_printf("PING server %16s\n", inet_ntoa(host0.sin_addr));
369 #ifdef CYGHWR_NET_DRIVER_ETH1
371 host1.sin_family = AF_INET;
372 host1.sin_len = sizeof(host1);
373 host1.sin_addr = eth1_bootp_data.bp_siaddr;
375 diag_printf("PING server %16s\n", inet_ntoa(host1.sin_addr));
382 // Now command the host to do ping to us...
383 #ifdef CYGHWR_NET_DRIVER_ETH0
385 do_ping_tests(ð0_bootp_data, TESTTIME, 56);
388 #ifdef CYGHWR_NET_DRIVER_ETH1
390 do_ping_tests(ð1_bootp_data, TESTTIME, 56);
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]);
398 // Let the server run for 5 minutes
399 cyg_thread_delay(2*100); // let the stuff start up first
401 cyg_thread_delay(TESTTIME*100); // FIXME - assume cS clock.
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]);
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.
413 #ifdef CYGHWR_NET_DRIVER_ETH0
416 #ifdef CYGHWR_NET_DRIVER_ETH1
419 ); // check for 2 pass messages from hosts
421 diag_printf("After running:\n");
423 TNR_OFF(); // dumpstats() turned it on again!
424 TNR_PRINT_ACTIVITY();
425 CYG_TEST_EXIT("Done");
431 // Create a main thread, so we can run the scheduler and have time 'pass'
432 cyg_thread_create(10, // Priority - just a number
434 0, // entry parameter
435 "Network test", // Name
436 &thread_stack[0][0], // Stack
438 &thread_handle[0], // Handle
439 &thread_data[0] // Thread data structure
441 cyg_thread_resume(thread_handle[0]); // Start it
443 // Create the secondary threads
444 cyg_thread_create(11, // Priority - just a number
446 0, // entry parameter
447 "Flood Ping Recv", // Name
448 &thread_stack[1][0], // Stack
450 &thread_handle[1], // Handle
451 &thread_data[1] // Thread data structure
453 cyg_thread_create(12, // Priority - just a number
455 0, // entry parameter
456 "Flood Ping Send", // Name
457 &thread_stack[2][0], // Stack
459 &thread_handle[2], // Handle
460 &thread_data[2] // Thread data structure
463 cyg_scheduler_start();
466 // EOF floodpingmux.c