1 //==========================================================================
3 // autotest/current/tests/routeping.inl
5 // Simple pinging test, the server pings me and I ping her.
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 #include "autohost.inl"
100 // ------------------------------------------------------------------------
102 externC int sscanf( char * /* str */, const char * /* format */, ... );
104 // ------------------------------------------------------------------------
106 static int route_add_s( struct sockaddr_in *target,
107 struct sockaddr_in *gw,
111 struct ecos_rtentry route;
112 struct sockaddr_in mask;
116 memcpy( &mask, gw, sizeof(*gw) );
118 mask.sin_addr.s_addr = htonl( (0xfffffffful ^ ((0x80000000ul >> maskbits)-1)) );
120 memset( &route, 0, sizeof(route) );
122 memcpy( &route.rt_dst , target, sizeof(*target) );
123 memcpy( &route.rt_gateway, gw , sizeof(*gw) );
124 memcpy( &route.rt_genmask, &mask , sizeof(mask) );
126 route.rt_flags = RTF_UP|RTF_GATEWAY;
127 route.rt_metric = metric;
131 diag_printf("INFO:<Route - dst: %s",
132 inet_ntoa(((struct sockaddr_in *)&route.rt_dst)->sin_addr));
133 diag_printf(", mask: %s",
134 inet_ntoa(((struct sockaddr_in *)&route.rt_genmask)->sin_addr));
135 diag_printf(", gateway: %s>\n",
136 inet_ntoa(((struct sockaddr_in *)&route.rt_gateway)->sin_addr));
138 s = socket( AF_INET, SOCK_DGRAM, 0 );
143 if (ioctl(s, SIOCADDRT, &route)) {
148 diag_printf( "PASS:<Route added OK>\n" );
155 int route_add( char *target,
160 struct sockaddr_in t_s, gw_s;
163 memset( &t_s, 0, sizeof(t_s) );
164 memset( &gw_s, 0, sizeof(gw_s) );
166 t_s.sin_len = gw_s.sin_len = sizeof(t_s);
167 t_s.sin_family = gw_s.sin_family = AF_INET;
169 if ( 4 != sscanf( target, "%d.%d.%d.%d", ints, ints+1, ints+2, ints+3 ) )
170 CYG_TEST_FAIL( "sscanf of target IP addr" );
172 t_s.sin_addr.s_addr = htonl(
178 if ( 4 != sscanf( gw, "%d.%d.%d.%d", ints, ints+1, ints+2, ints+3 ) )
179 CYG_TEST_FAIL( "sscanf of target IP addr" );
181 gw_s.sin_addr.s_addr = htonl(
187 return route_add_s( &t_s, &gw_s, maskbits, metric );
190 // ------------------------------------------------------------------------
193 #define MAX_PACKET 4096
194 #define MIN_PACKET 64
195 #define MAX_SEND 4000
197 #define PACKET_ADD ((MAX_SEND - MIN_PACKET)/NUM_PINGS)
198 #define nPACKET_ADD 1
200 static unsigned char pkt1[MAX_PACKET], pkt2[MAX_PACKET];
202 #define UNIQUEID 0x1234
204 // Compute INET checksum
206 inet_cksum(u_short *addr, int len)
208 register int nleft = len;
209 register u_short *w = addr;
210 register u_short answer;
211 register u_int sum = 0;
212 u_short odd_byte = 0;
215 * Our algorithm is simple, using a 32 bit accumulator (sum),
216 * we add sequential 16 bit words to it, and at the end, fold
217 * back all the carry bits from the top 16 bits into the lower
225 /* mop up an odd byte, if necessary */
227 *(u_char *)(&odd_byte) = *(u_char *)w;
232 * add back carry outs from top 16 bits to low 16 bits
234 sum = (sum >> 16) + (sum & 0x0000ffff); /* add hi 16 to low 16 */
235 sum += (sum >> 16); /* add carry */
236 answer = ~sum; /* truncate to 16 bits */
241 errshorts, errinvalids, errbadids, errwronghosts, errsendtos, errrecvfroms;
243 errshorts=errinvalids=errbadids=errwronghosts=errsendtos=errrecvfroms=0
246 check_icmp(unsigned char *pkt, int len,
247 struct sockaddr_in *from, struct sockaddr_in *to)
249 cyg_tick_count_t *tp, tv;
252 tv = cyg_current_time();
253 ip = (struct ip *)pkt;
254 if (len < sizeof(*ip))
255 if (ip->ip_v != IPVERSION) {
259 icmp = (struct icmp *)(pkt + sizeof(*ip));
260 len -= (sizeof(*ip) + 8);
261 tp = (cyg_tick_count_t *)&icmp->icmp_data;
262 if (icmp->icmp_type != ICMP_ECHOREPLY) {
266 if (icmp->icmp_id != UNIQUEID) {
270 if (from->sin_addr.s_addr != to->sin_addr.s_addr) {
278 // 0 - require failure
279 // 1 - require success
282 #define FAILURE_REQUIRED 0
283 #define SUCCESS_REQUIRED 1
284 #define NOTHING_REQUIRED 2
287 ping_host(int s, struct sockaddr_in *host, int expect )
289 struct icmp *icmp = (struct icmp *)pkt1;
290 int icmp_len = MIN_PACKET;
291 int seq, ok_recv, bogus_recv;
292 cyg_tick_count_t *tp;
294 struct sockaddr_in from;
301 diag_printf("INFO:<PING server %s ....>\n", inet_ntoa(host->sin_addr));
302 for (seq = 0; seq < NUM_PINGS; seq++, icmp_len += PACKET_ADD ) {
304 cyg_thread_delay( 47 ); // Half a second...
306 icmp->icmp_type = ICMP_ECHO;
308 icmp->icmp_cksum = 0;
309 icmp->icmp_seq = seq;
310 icmp->icmp_id = 0x1234;
312 tp = (cyg_tick_count_t *)&icmp->icmp_data;
313 *tp++ = cyg_current_time();
315 for (i = sizeof(*tp); i < icmp_len; i += sizeof(*dp)) {
319 icmp->icmp_cksum = inet_cksum( (u_short *)icmp, icmp_len+8);
321 if (sendto(s, icmp, icmp_len+8, 0, (struct sockaddr *)host, sizeof(*host)) < 0) {
325 // Wait for a response
326 fromlen = sizeof(from);
327 len = recvfrom(s, pkt2, sizeof(pkt2), 0, (struct sockaddr *)&from, &fromlen);
330 icmp_len = MIN_PACKET - PACKET_ADD; // just in case - long routes
332 if ( check_icmp(pkt2, len, &from, host) )
339 diag_printf("INFO:<Sent %d packets, received %d OK, %d bad>\n", NUM_PINGS, ok_recv, bogus_recv);
340 if ( errsendtos + errrecvfroms )
341 diag_printf("INFO:<%d sendto errors, %d recvfrom errors>\n", errsendtos, errrecvfroms );
343 if ( SUCCESS_REQUIRED == expect ) {
344 CYG_TEST_CHECK( 0 < ok_recv, "No pings succeeded" );
345 CYG_TEST_CHECK( 0 == errsendtos, "Sendto failures" );
347 CYG_TEST_CHECK( NUM_PINGS/2 < ok_recv, "Not enough pings succeeded" );
348 CYG_TEST_CHECK( NUM_PINGS/2 > bogus_recv, "Too many pings failed" );
351 if ( FAILURE_REQUIRED == expect ) {
352 CYG_TEST_CHECK( 0 == ok_recv, "Pings succeeded" );
353 CYG_TEST_CHECK( bogus_recv + errsendtos + errrecvfroms == NUM_PINGS, "Not enough failures" );
355 CYG_TEST_CHECK( 0 == bogus_recv, "Some pings got bogus recv" );
358 CYG_TEST_PASS( SUCCESS_REQUIRED == expect ? "Expected successful ping behaviour" :
359 FAILURE_REQUIRED == expect ? "Expected failure ping behaviour" :
360 "Non-predicted ping behaviour" );
367 ping_test(struct bootp *bp)
371 struct sockaddr_in host;
374 if ((p = getprotobyname("icmp")) == (struct protoent *)0) {
375 pexit("getprotobyname");
378 s = socket(AF_INET, SOCK_RAW, p->p_proto);
385 setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
386 // Set up host address
387 host.sin_family = AF_INET;
388 host.sin_len = sizeof(host);
389 host.sin_addr = bp->bp_siaddr;
391 ping_host(s, &host, SUCCESS_REQUIRED );
392 // Now try a bogus host
393 host.sin_addr.s_addr = htonl(ntohl(host.sin_addr.s_addr) + 32);
394 ping_host(s, &host, NOTHING_REQUIRED ); // Success is optional
399 station_ping(char *name, int expect)
403 struct sockaddr_in host;
408 if ((p = getprotobyname("icmp")) == (struct protoent *)0) {
409 pexit("getprotobyname");
412 s = socket(AF_INET, SOCK_RAW, p->p_proto);
419 setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
420 // Set up host address
421 host.sin_family = AF_INET;
422 host.sin_len = sizeof(host);
425 if ( 4 != sscanf( name, "%d.%d.%d.%d", ints, ints+1, ints+2, ints+3 ) )
426 CYG_TEST_FAIL( "sscanf of host IP addr" );
428 host.sin_addr.s_addr = htonl(
434 ping_host(s, &host, expect );
440 // ------------------------------------------------------------------------
442 #define STACK_SIZE (CYGNUM_HAL_STACK_SIZE_TYPICAL + 0x10000)
443 static char stack[STACK_SIZE];
444 static cyg_thread thread_data;
445 static cyg_handle_t thread_handle;
448 do_ping_tests(struct bootp *bp, int N, int testtime, int filesize)
452 diag_printf( "INFO: telling %s to run %d pings for %d seconds, %d bytes and up\n",
453 inet_ntoa(bp->bp_siaddr), N, testtime, filesize );
455 // For 2nd and subsequent orders, we vary the ping data size:
456 for ( i = 0; i < N; i++ ) {
457 sprintf( order, "%s %s %d %d", "SLOW_PING",
458 inet_ntoa(bp->bp_yiaddr),
460 filesize + 100 * i );
461 autohost_tell( bp, order );
465 #define TESTTIME (5 * 60) // Seconds
467 #define NUM_SESSIONS 10 // why not?
469 // ------------------------------------------------------------------------
471 // The plan is as follows:
473 // The host(s) will be set up so as to have additional interfaces as follows:
475 // eth0-host -> 10.0.3.1 and maybe others
476 // eth1-host -> 10.0.4.1 and maybe others
477 // 10.0.5.x should not exist
479 // These additional addresses can exist as extra loopback addresses, lo0:0,
480 // lo0:1 and so on in a linux box.
482 // We want to ping all of the following:
484 // eth0-host should always work
485 // eth0-host+32 not expected to work
486 // eth1-host should always work
487 // eth1-host+32 not expected to work
488 // www.cygnus.com should always work if default gw is correct
489 // 10.0.3.1 should only work if route via eth0-host is added
490 // 10.0.3.99 might work if route via eth0-host is added
491 // 10.0.4.1 should only work if route via eth0-host is added
492 // 10.0.4.99 might work if route via eth0-host is added
493 // 10.0.5.1 should never work
494 // 10.0.5.99 should never work
496 // So we test those places in the following contexts:
498 // 1) No special routing added
499 // 2) route via eth0-host to 10.0.3.x is added
500 // 3) route via eth1-host to 10.0.4.x is added
501 // 4) Both routes are added
503 // and check for correct operation and correct failure in all cases.
505 // Because of the time taken, these are separate test cases which include
506 // this INL file after making suitable #defines.
508 // All the while, the hosts shall ping us, and when all is going we loop for
509 // the rest of the test time.
513 net_test(cyg_addrword_t param)
519 int net3expect = FAILURE_REQUIRED;
520 int net4expect = FAILURE_REQUIRED;
522 cyg_tick_count_t ticks;
524 CYG_TEST_INFO("Start PING routing test");
525 init_all_network_interfaces();
531 #ifdef ADDROUTETONET_10_0_3_x
532 #ifdef CYGHWR_NET_DRIVER_ETH0
534 route_add( "10.0.3.0", inet_ntoa(eth0_bootp_data.bp_siaddr), 24, 1 );
535 net3expect = SUCCESS_REQUIRED;
538 #endif // ADDROUTETONET_10_0_3_x
540 #ifdef ADDROUTETONET_10_0_4_x
541 #ifdef CYGHWR_NET_DRIVER_ETH1
543 route_add( "10.0.4.0", inet_ntoa(eth1_bootp_data.bp_siaddr), 24, 1 );
544 net4expect = SUCCESS_REQUIRED;
547 #endif // ADDROUTETONET_10_0_4_x
549 numtests = NUM_SESSIONS; // The number of pingers started OK
552 // Now command the host to do ping to us...
553 #ifdef CYGHWR_NET_DRIVER_ETH0
555 do_ping_tests(ð0_bootp_data, 1, TESTTIME, 64);
556 do_ping_tests(ð0_bootp_data, 1, TESTTIME, 2000);
560 #ifdef CYGHWR_NET_DRIVER_ETH1
561 if (eth1_up && i > 0) {
562 do_ping_tests(ð1_bootp_data, 1, TESTTIME, 100);
563 do_ping_tests(ð1_bootp_data, 1, TESTTIME, 1800);
567 #endif // DOHOSTPINGS
568 numtests -= i; // Adjust to how many we *actually* requested
570 ticks = cyg_current_time() + TESTTIME * 100; // FIXME - assume cS clock.
572 // Let the server run for 5 minutes
576 #ifdef CYGHWR_NET_DRIVER_ETH0
578 ping_test(ð0_bootp_data);
579 if ( ticks < cyg_current_time() ) break;
581 #ifdef CYGHWR_NET_DRIVER_ETH1
583 ping_test(ð1_bootp_data);
584 if ( ticks < cyg_current_time() ) break;
586 #ifdef CYGHWR_NET_DRIVER_ETH0
588 station_ping( "10.0.3.1", net3expect );
589 if ( ticks < cyg_current_time() ) break;
590 station_ping( "10.0.3.99", net3expect );
592 if ( ticks < cyg_current_time() ) break;
594 #ifdef CYGHWR_NET_DRIVER_ETH1
596 station_ping( "10.0.4.1", net4expect );
597 if ( ticks < cyg_current_time() ) break;
598 station_ping( "10.0.4.99", net4expect );
600 if ( ticks < cyg_current_time() ) break;
602 station_ping( "10.0.5.1", FAILURE_REQUIRED ); // Not valid
603 if ( ticks < cyg_current_time() ) break;
604 station_ping( "10.0.5.99", FAILURE_REQUIRED ); // Not valid
605 if ( ticks < cyg_current_time() ) break;
606 #ifdef PING_WWW_CYGNUS_COM
607 station_ping( "205.180.83.41", NOTHING_REQUIRED ); // www.cygnus.com
608 if ( ticks < cyg_current_time() ) break;
612 // Additional delay 'cos host may be slower than us - and it has to
613 // complete a transfer anyway:
614 cyg_thread_delay( 30 *100); // FIXME - assume cS clock.
617 autohost_end( 0 ); // check for N pass messages from hosts
619 TNR_PRINT_ACTIVITY();
620 CYG_TEST_EXIT("Done");
626 // Create a main thread, so we can run the scheduler and have time 'pass'
627 cyg_thread_create(10, // Priority - just a number
629 0, // entry parameter
630 "Network test", // Name
633 &thread_handle, // Handle
634 &thread_data // Thread data structure
636 cyg_thread_resume(thread_handle); // Start it
637 cyg_scheduler_start();