]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/net/autotest/v2_0/tests/routeping.inl
Initial revision
[karo-tx-redboot.git] / packages / net / autotest / v2_0 / tests / routeping.inl
1 //==========================================================================
2 //
3 //      autotest/current/tests/routeping.inl
4 //
5 //      Simple pinging test, the server pings me and I ping her.
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 #include "autohost.inl"
99
100 // ------------------------------------------------------------------------
101
102 externC int sscanf( char * /* str */, const char * /* format */, ... );
103
104 // ------------------------------------------------------------------------
105
106 static int route_add_s( struct sockaddr_in *target,
107                         struct sockaddr_in *gw,
108                         int maskbits,
109                         int metric )
110 {
111     struct ecos_rtentry route;
112     struct sockaddr_in mask;
113
114     int s;
115
116     memcpy( &mask, gw, sizeof(*gw) );
117     maskbits--;
118     mask.sin_addr.s_addr = htonl( (0xfffffffful ^ ((0x80000000ul >> maskbits)-1)) );
119
120     memset( &route, 0, sizeof(route) );
121
122     memcpy( &route.rt_dst    ,  target, sizeof(*target) );
123     memcpy( &route.rt_gateway,  gw    , sizeof(*gw) );
124     memcpy( &route.rt_genmask, &mask  , sizeof(mask) ); 
125
126     route.rt_flags = RTF_UP|RTF_GATEWAY;
127     route.rt_metric = metric;
128
129     route.rt_dev = NULL;
130
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));
137
138     s = socket( AF_INET, SOCK_DGRAM, 0 );
139     if (s < 0) {
140         perror( "socket" );
141         return false;
142     }
143     if (ioctl(s, SIOCADDRT, &route)) {
144         perror("SIOCADDRT");
145         close(s);
146         return false;
147     }
148     diag_printf( "PASS:<Route added OK>\n" );
149     close(s);
150     return true;
151 }
152
153
154
155 int route_add( char *target,
156                       char *gw,
157                       int maskbits,
158                       int metric )
159 {
160     struct sockaddr_in t_s, gw_s;
161     int ints[4];
162
163     memset( &t_s,  0, sizeof(t_s)  );
164     memset( &gw_s, 0, sizeof(gw_s) );
165
166     t_s.sin_len    = gw_s.sin_len    = sizeof(t_s);
167     t_s.sin_family = gw_s.sin_family = AF_INET;
168
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" );
171     else
172         t_s.sin_addr.s_addr = htonl( 
173             (ints[0] << 24) |
174             (ints[1] << 16) |
175             (ints[2] <<  8) |
176             (ints[3]      )  );
177     
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" );
180     else
181         gw_s.sin_addr.s_addr = htonl( 
182             (ints[0] << 24) |
183             (ints[1] << 16) |
184             (ints[2] <<  8) |
185             (ints[3]      )  );
186
187     return route_add_s( &t_s, &gw_s, maskbits, metric );
188 }
189
190 // ------------------------------------------------------------------------
191
192 #define NUM_PINGS 16
193 #define MAX_PACKET 4096
194 #define MIN_PACKET   64
195 #define MAX_SEND   4000
196
197 #define PACKET_ADD  ((MAX_SEND - MIN_PACKET)/NUM_PINGS)
198 #define nPACKET_ADD  1 
199
200 static unsigned char pkt1[MAX_PACKET], pkt2[MAX_PACKET];
201
202 #define UNIQUEID 0x1234
203
204 // Compute INET checksum
205 int
206 inet_cksum(u_short *addr, int len)
207 {
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;
213
214     /*
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
218      *  16 bits.
219      */
220     while( nleft > 1 )  {
221         sum += *w++;
222         nleft -= 2;
223     }
224
225     /* mop up an odd byte, if necessary */
226     if( nleft == 1 ) {
227         *(u_char *)(&odd_byte) = *(u_char *)w;
228         sum += odd_byte;
229     }
230
231     /*
232      * add back carry outs from top 16 bits to low 16 bits
233      */
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 */
237     return (answer);
238 }
239
240 static int
241 errshorts, errinvalids, errbadids, errwronghosts, errsendtos, errrecvfroms;
242 #define ERRRESET() \
243 errshorts=errinvalids=errbadids=errwronghosts=errsendtos=errrecvfroms=0
244
245 static int
246 check_icmp(unsigned char *pkt, int len, 
247           struct sockaddr_in *from, struct sockaddr_in *to)
248 {
249     cyg_tick_count_t *tp, tv;
250     struct ip *ip;
251     struct icmp *icmp;
252     tv = cyg_current_time();
253     ip = (struct ip *)pkt;
254     if (len < sizeof(*ip))
255         if (ip->ip_v != IPVERSION) {
256         errshorts++;
257         return 0;
258     }
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) {
263         errinvalids++;
264         return 0;
265     }
266     if (icmp->icmp_id != UNIQUEID) {
267         errbadids++;
268         return 0;
269     }
270     if (from->sin_addr.s_addr != to->sin_addr.s_addr) {
271         errwronghosts++;
272         return 0;
273     }
274     return 1;
275 }
276
277 // expect is
278 // 0 - require failure
279 // 1 - require success
280 // 2 - don't care
281
282 #define FAILURE_REQUIRED 0
283 #define SUCCESS_REQUIRED 1
284 #define NOTHING_REQUIRED 2
285
286 static void
287 ping_host(int s, struct sockaddr_in *host, int expect )
288 {
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;
293     long *dp;
294     struct sockaddr_in from;
295     int i, len, fromlen;
296
297     ERRRESET();
298     ok_recv = 0;
299     bogus_recv = 0;
300     TNR_OFF();
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 ) {
303         TNR_ON();
304         cyg_thread_delay( 47 ); // Half a second...
305         // Build ICMP packet
306         icmp->icmp_type = ICMP_ECHO;
307         icmp->icmp_code = 0;
308         icmp->icmp_cksum = 0;
309         icmp->icmp_seq = seq;
310         icmp->icmp_id = 0x1234;
311         // Set up ping data
312         tp = (cyg_tick_count_t *)&icmp->icmp_data;
313         *tp++ = cyg_current_time();
314         dp = (long *)tp;
315         for (i = sizeof(*tp);  i < icmp_len;  i += sizeof(*dp)) {
316             *dp++ = i;
317         }
318         // Add checksum
319         icmp->icmp_cksum = inet_cksum( (u_short *)icmp, icmp_len+8);
320         // Send it off
321         if (sendto(s, icmp, icmp_len+8, 0, (struct sockaddr *)host, sizeof(*host)) < 0) {
322             errsendtos++;
323             continue;
324         }
325         // Wait for a response
326         fromlen = sizeof(from);
327         len = recvfrom(s, pkt2, sizeof(pkt2), 0, (struct sockaddr *)&from, &fromlen);
328         if (len < 0) {
329             errrecvfroms++;
330             icmp_len = MIN_PACKET - PACKET_ADD; // just in case - long routes
331         } else {
332             if ( check_icmp(pkt2, len, &from, host) )
333                 ok_recv++;
334             else
335                 bogus_recv++;
336         }
337     }
338     TNR_OFF();
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 );
342
343     if ( SUCCESS_REQUIRED == expect ) {
344         CYG_TEST_CHECK( 0 < ok_recv, "No pings succeeded" );
345         CYG_TEST_CHECK( 0 == errsendtos, "Sendto failures" );
346 #ifndef XFAIL
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" );
349 #endif
350     }
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" );
354 #ifndef XFAIL
355         CYG_TEST_CHECK( 0 == bogus_recv, "Some pings got bogus recv" );
356 #endif
357     }
358     CYG_TEST_PASS( SUCCESS_REQUIRED == expect ? "Expected successful ping behaviour" :
359                    FAILURE_REQUIRED == expect ? "Expected failure ping behaviour" :
360                                                 "Non-predicted ping behaviour" );
361
362     TNR_ON();
363     return;
364 }
365
366 static void
367 ping_test(struct bootp *bp)
368 {
369     struct protoent *p;
370     struct timeval tv;
371     struct sockaddr_in host;
372     int s;
373
374     if ((p = getprotobyname("icmp")) == (struct protoent *)0) {
375         pexit("getprotobyname");
376         return;
377     }
378     s = socket(AF_INET, SOCK_RAW, p->p_proto);
379     if (s < 0) {
380         pexit("socket");
381         return;
382     }
383     tv.tv_sec = 1;
384     tv.tv_usec = 0;
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;
390     host.sin_port = 0;
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
395     close(s);
396 }
397
398 static void
399 station_ping(char *name, int expect)
400 {
401     struct protoent *p;
402     struct timeval tv;
403     struct sockaddr_in host;
404     int s;
405
406     int ints[4];
407
408     if ((p = getprotobyname("icmp")) == (struct protoent *)0) {
409         pexit("getprotobyname");
410         return;
411     }
412     s = socket(AF_INET, SOCK_RAW, p->p_proto);
413     if (s < 0) {
414         pexit("socket");
415         return;
416     }
417     tv.tv_sec = 1;
418     tv.tv_usec = 0;
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);
423     host.sin_port = 0;
424
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" );
427     else {
428         host.sin_addr.s_addr = htonl( 
429             (ints[0] << 24) |
430             (ints[1] << 16) |
431             (ints[2] <<  8) |
432             (ints[3]      )  );
433             
434         ping_host(s, &host, expect );
435
436     }
437     close(s);
438 }
439
440 // ------------------------------------------------------------------------
441
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;
446
447 static void
448 do_ping_tests(struct bootp *bp, int N, int testtime, int filesize)
449 {
450     int i;
451     char order[256];
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 );
454
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),
459                  testtime,
460                  filesize + 100 * i );
461         autohost_tell( bp, order );
462     }
463 }
464
465 #define TESTTIME (5 * 60) // Seconds
466
467 #define NUM_SESSIONS 10 // why not?
468
469 // ------------------------------------------------------------------------
470 //
471 // The plan is as follows: 
472 //
473 // The host(s) will be set up so as to have additional interfaces as follows:
474 //
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
478 //
479 // These additional addresses can exist as extra loopback addresses, lo0:0,
480 // lo0:1 and so on in a linux box.
481 //
482 // We want to ping all of the following:
483 //
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
495 //
496 // So we test those places in the following contexts:
497 // 
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
502 //
503 // and check for correct operation and correct failure in all cases.
504 //
505 // Because of the time taken, these are separate test cases which include
506 // this INL file after making suitable #defines.
507 //
508 // All the while, the hosts shall ping us, and when all is going we loop for
509 // the rest of the test time.
510 //
511
512 void
513 net_test(cyg_addrword_t param)
514 {
515 #ifdef DOHOSTPINGS
516     int i;
517     int numtests;
518 #endif
519     int net3expect = FAILURE_REQUIRED;
520     int net4expect = FAILURE_REQUIRED;
521
522     cyg_tick_count_t ticks;
523     CYG_TEST_INIT();
524     CYG_TEST_INFO("Start PING routing test");
525     init_all_network_interfaces();
526
527     autohost_init();
528
529     TNR_INIT();
530
531 #ifdef ADDROUTETONET_10_0_3_x
532 #ifdef CYGHWR_NET_DRIVER_ETH0
533     if (eth0_up) {
534         route_add( "10.0.3.0", inet_ntoa(eth0_bootp_data.bp_siaddr), 24, 1 );
535         net3expect = SUCCESS_REQUIRED;
536     }
537 #endif
538 #endif // ADDROUTETONET_10_0_3_x
539
540 #ifdef ADDROUTETONET_10_0_4_x
541 #ifdef CYGHWR_NET_DRIVER_ETH1
542     if (eth1_up) {
543         route_add( "10.0.4.0", inet_ntoa(eth1_bootp_data.bp_siaddr), 24, 1 );
544         net4expect = SUCCESS_REQUIRED;
545     }
546 #endif
547 #endif // ADDROUTETONET_10_0_4_x
548
549     numtests = NUM_SESSIONS; // The number of pingers started OK
550     i = numtests;
551 #ifdef DOHOSTPINGS
552     // Now command the host to do ping to us...
553 #ifdef CYGHWR_NET_DRIVER_ETH0
554     if (eth0_up) {
555         do_ping_tests(&eth0_bootp_data, 1, TESTTIME, 64);
556         do_ping_tests(&eth0_bootp_data, 1, TESTTIME, 2000);
557         i -= 2;
558     }
559 #endif
560 #ifdef CYGHWR_NET_DRIVER_ETH1
561     if (eth1_up && i > 0) {
562         do_ping_tests(&eth1_bootp_data, 1, TESTTIME, 100);
563         do_ping_tests(&eth1_bootp_data, 1, TESTTIME, 1800);
564         i -= 2;
565     }
566 #endif
567 #endif // DOHOSTPINGS
568     numtests -= i; // Adjust to how many we *actually* requested
569
570     ticks = cyg_current_time() + TESTTIME * 100; // FIXME - assume cS clock.
571
572     // Let the server run for 5 minutes
573     TNR_ON();
574
575     while (1) {
576 #ifdef CYGHWR_NET_DRIVER_ETH0
577         if (eth0_up)
578             ping_test(&eth0_bootp_data);
579         if ( ticks < cyg_current_time() ) break;
580 #endif
581 #ifdef CYGHWR_NET_DRIVER_ETH1
582         if (eth1_up)
583             ping_test(&eth1_bootp_data);
584         if ( ticks < cyg_current_time() ) break;
585 #endif
586 #ifdef CYGHWR_NET_DRIVER_ETH0
587         if (eth0_up) {
588             station_ping( "10.0.3.1",  net3expect );
589             if ( ticks < cyg_current_time() ) break;
590             station_ping( "10.0.3.99", net3expect );
591         }
592         if ( ticks < cyg_current_time() ) break;
593 #endif
594 #ifdef CYGHWR_NET_DRIVER_ETH1
595         if (eth1_up) {
596             station_ping( "10.0.4.1",  net4expect );
597             if ( ticks < cyg_current_time() ) break;
598             station_ping( "10.0.4.99", net4expect );
599         }
600         if ( ticks < cyg_current_time() ) break;
601 #endif
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;
609 #endif
610     };
611
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.
615     TNR_OFF();
616
617     autohost_end( 0 ); // check for N pass messages from hosts
618
619     TNR_PRINT_ACTIVITY();
620     CYG_TEST_EXIT("Done");
621 }
622
623 void
624 cyg_start(void)
625 {
626     // Create a main thread, so we can run the scheduler and have time 'pass'
627     cyg_thread_create(10,                // Priority - just a number
628                       net_test,          // entry
629                       0,                 // entry parameter
630                       "Network test",    // Name
631                       &stack[0],         // Stack
632                       STACK_SIZE,        // Size
633                       &thread_handle,    // Handle
634                       &thread_data       // Thread data structure
635             );
636     cyg_thread_resume(thread_handle);  // Start it
637     cyg_scheduler_start();
638 }
639
640 // EOF routeping.inl