OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [net/] [autotest/] [current/] [tests/] [slowpingmux.c] - Blame information for rev 854

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
//==========================================================================
2
//
3
//      autotest/current/tests/slowpingmux.c
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      
16
// version.                                                                 
17
//
18
// eCos is distributed in the hope that it will be useful, but WITHOUT      
19
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or    
20
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License    
21
// for more details.                                                        
22
//
23
// You should have received a copy of the GNU General Public License        
24
// along with eCos; if not, write to the Free Software Foundation, Inc.,    
25
// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.            
26
//
27
// As a special exception, if other files instantiate templates or use      
28
// macros or inline functions from this file, or you compile this file      
29
// and link it with other works to produce a work based on this file,       
30
// this file does not by itself cause the resulting work to be covered by   
31
// the GNU General Public License. However the source code for this file    
32
// must still be made available in accordance with section (3) of the GNU   
33
// General Public License v2.                                               
34
//
35
// This exception does not invalidate any other reasons why a work based    
36
// on this file might be covered by the GNU General Public License.         
37
// -------------------------------------------                              
38
// ####ECOSGPLCOPYRIGHTEND####                                              
39
// ####BSDALTCOPYRIGHTBEGIN####                                             
40
// -------------------------------------------                              
41
// Portions of this software may have been derived from FreeBSD, OpenBSD,   
42
// or other sources, and if so are covered by the appropriate copyright     
43
// and license included herein.                                             
44
// -------------------------------------------                              
45
// ####BSDALTCOPYRIGHTEND####                                               
46
//==========================================================================
47
//#####DESCRIPTIONBEGIN####
48
//
49
// Author(s):    hmt,gthomas
50
// Contributors: hmt,gthomas
51
// Date:         2000-10-23
52
// Purpose:      
53
// Description:  
54
//              
55
//
56
//####DESCRIPTIONEND####
57
//
58
//==========================================================================
59
 
60
#include <pkgconf/system.h>
61
 
62
#include <cyg/infra/testcase.h>         // testing infrastructure
63
 
64
#include <pkgconf/net.h>
65
 
66
#ifdef CYGBLD_DEVS_ETH_DEVICE_H    // Get the device config if it exists
67
#include CYGBLD_DEVS_ETH_DEVICE_H  // May provide CYGTST_DEVS_ETH_TEST_NET_REALTIME
68
#endif
69
 
70
#ifdef CYGPKG_NET_TESTS_USE_RT_TEST_HARNESS // do we use the rt test?
71
# ifdef CYGTST_DEVS_ETH_TEST_NET_REALTIME // Get the test ancilla if it exists
72
#  include CYGTST_DEVS_ETH_TEST_NET_REALTIME
73
# endif
74
#endif
75
 
76
 
77
// Fill in the blanks if necessary
78
#ifndef TNR_OFF
79
# define TNR_OFF()
80
#endif
81
#ifndef TNR_ON
82
# define TNR_ON()
83
#endif
84
#ifndef TNR_INIT
85
# define TNR_INIT()
86
#endif
87
#ifndef TNR_PRINT_ACTIVITY
88
# define TNR_PRINT_ACTIVITY()
89
#endif
90
 
91
// ------------------------------------------------------------------------
92
 
93
#include <errno.h>
94
#include <network.h>
95
 
96
#include "autohost.inl"
97
 
98
 
99
#define NUM_PINGS 16
100
#define MAX_PACKET 4096
101
#define MIN_PACKET   64
102
#define MAX_SEND   4000
103
 
104
#define PACKET_ADD  ((MAX_SEND - MIN_PACKET)/NUM_PINGS)
105
#define nPACKET_ADD  1 
106
 
107
static unsigned char pkt1[MAX_PACKET], pkt2[MAX_PACKET];
108
 
109
#define UNIQUEID 0x1234
110
 
111
// Compute INET checksum
112
int
113
inet_cksum(u_short *addr, int len)
114
{
115
    register int nleft = len;
116
    register u_short *w = addr;
117
    register u_short answer;
118
    register u_int sum = 0;
119
    u_short odd_byte = 0;
120
 
121
    /*
122
     *  Our algorithm is simple, using a 32 bit accumulator (sum),
123
     *  we add sequential 16 bit words to it, and at the end, fold
124
     *  back all the carry bits from the top 16 bits into the lower
125
     *  16 bits.
126
     */
127
    while( nleft > 1 )  {
128
        sum += *w++;
129
        nleft -= 2;
130
    }
131
 
132
    /* mop up an odd byte, if necessary */
133
    if( nleft == 1 ) {
134
        *(u_char *)(&odd_byte) = *(u_char *)w;
135
        sum += odd_byte;
136
    }
137
 
138
    /*
139
     * add back carry outs from top 16 bits to low 16 bits
140
     */
141
    sum = (sum >> 16) + (sum & 0x0000ffff); /* add hi 16 to low 16 */
142
    sum += (sum >> 16);                     /* add carry */
143
    answer = ~sum;                          /* truncate to 16 bits */
144
    return (answer);
145
}
146
 
147
static int
148
show_icmp(unsigned char *pkt, int len,
149
          struct sockaddr_in *from, struct sockaddr_in *to)
150
{
151
    cyg_tick_count_t *tp, tv;
152
    struct ip *ip;
153
    struct icmp *icmp;
154
    tv = cyg_current_time();
155
    ip = (struct ip *)pkt;
156
    if ((len < sizeof(*ip)) || ip->ip_v != IPVERSION) {
157
        diag_printf("%s: Short packet or not IP! - Len: %d, Version: %d\n",
158
                    inet_ntoa(from->sin_addr), len, ip->ip_v);
159
        return 0;
160
    }
161
    icmp = (struct icmp *)(pkt + sizeof(*ip));
162
    len -= (sizeof(*ip) + 8);
163
    tp = (cyg_tick_count_t *)&icmp->icmp_data;
164
    if (icmp->icmp_type != ICMP_ECHOREPLY) {
165
        diag_printf("%s: Invalid ICMP - type: %d\n",
166
                    inet_ntoa(from->sin_addr), icmp->icmp_type);
167
        return 0;
168
    }
169
    if (icmp->icmp_id != UNIQUEID) {
170
        diag_printf("%s: ICMP received for wrong id - sent: %x, recvd: %x\n",
171
                    inet_ntoa(from->sin_addr), UNIQUEID, icmp->icmp_id);
172
    }
173
//    diag_printf("%d bytes from %s: ", len, inet_ntoa(from->sin_addr));
174
//    diag_printf("icmp_seq=%d", icmp->icmp_seq);
175
//    diag_printf(", time=%dms\n", (int)(tv - *tp)*10);
176
    return (from->sin_addr.s_addr == to->sin_addr.s_addr);
177
}
178
 
179
static void
180
ping_host(int s, struct sockaddr_in *host)
181
{
182
    struct icmp *icmp = (struct icmp *)pkt1;
183
    int icmp_len = MIN_PACKET;
184
    int seq, ok_recv, bogus_recv;
185
    cyg_tick_count_t *tp;
186
    long *dp;
187
    struct sockaddr_in from;
188
    int i, len, fromlen;
189
    int maxlen = 0;
190
 
191
    ok_recv = 0;
192
    bogus_recv = 0;
193
    TNR_OFF();
194
    diag_printf("PING server %s\n", inet_ntoa(host->sin_addr));
195
    for (seq = 0;  seq < NUM_PINGS;  seq++, icmp_len += PACKET_ADD ) {
196
        TNR_ON();
197
        cyg_thread_delay( 47 ); // Half a second...
198
        // Build ICMP packet
199
        icmp->icmp_type = ICMP_ECHO;
200
        icmp->icmp_code = 0;
201
        icmp->icmp_cksum = 0;
202
        icmp->icmp_seq = seq;
203
        icmp->icmp_id = 0x1234;
204
        // Set up ping data
205
        tp = (cyg_tick_count_t *)&icmp->icmp_data;
206
        *tp++ = cyg_current_time();
207
        dp = (long *)tp;
208
        for (i = sizeof(*tp);  i < icmp_len;  i += sizeof(*dp)) {
209
            *dp++ = i;
210
        }
211
        // Add checksum
212
        icmp->icmp_cksum = inet_cksum( (u_short *)icmp, icmp_len+8);
213
        // Send it off
214
        if (sendto(s, icmp, icmp_len+8, 0, (struct sockaddr *)host, sizeof(*host)) < 0) {
215
            TNR_OFF();
216
            perror("sendto");
217
            continue;
218
        }
219
        // Wait for a response
220
        fromlen = sizeof(from);
221
        len = recvfrom(s, pkt2, sizeof(pkt2), 0, (struct sockaddr *)&from, &fromlen);
222
        TNR_OFF();
223
        if (len < 0) {
224
//            perror("recvfrom");
225
            icmp_len = MIN_PACKET - PACKET_ADD; // just in case - long routes
226
        } else {
227
            if (show_icmp(pkt2, len, &from, host)) {
228
                ok_recv++;
229
                if ( len > maxlen )
230
                    maxlen = len;
231
            } else {
232
                bogus_recv++;
233
            }
234
        }
235
    }
236
    TNR_OFF();
237
    diag_printf("Sent %d packets, received %d OK, %d bad max len %d\n",
238
                NUM_PINGS, ok_recv, bogus_recv, maxlen);
239
}
240
 
241
static void
242
ping_test(struct bootp *bp)
243
{
244
    struct protoent *p;
245
    struct timeval tv;
246
    struct sockaddr_in host;
247
    int s;
248
 
249
    if ((p = getprotobyname("icmp")) == (struct protoent *)0) {
250
        pexit("getprotobyname");
251
        return;
252
    }
253
    s = socket(AF_INET, SOCK_RAW, p->p_proto);
254
    if (s < 0) {
255
        pexit("socket");
256
        return;
257
    }
258
    tv.tv_sec = 1;
259
    tv.tv_usec = 0;
260
    setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
261
    // Set up host address
262
    host.sin_family = AF_INET;
263
    host.sin_len = sizeof(host);
264
    host.sin_addr = bp->bp_siaddr;
265
    host.sin_port = 0;
266
    ping_host(s, &host);
267
    // Now try a bogus host
268
    host.sin_addr.s_addr = htonl(ntohl(host.sin_addr.s_addr) + 32);
269
    ping_host(s, &host);
270
    close(s);
271
}
272
 
273
// ------------------------------------------------------------------------
274
 
275
#define STACK_SIZE (CYGNUM_HAL_STACK_SIZE_TYPICAL + 0x10000)
276
static char stack[STACK_SIZE];
277
static cyg_thread thread_data;
278
static cyg_handle_t thread_handle;
279
 
280
static void
281
do_ping_tests(struct bootp *bp, int N, int testtime, int filesize)
282
{
283
    int i;
284
    char order[256];
285
    diag_printf( "INFO: telling %s to run %d pings for %d seconds, %d bytes and up\n",
286
          inet_ntoa(bp->bp_siaddr), N, testtime, filesize );
287
 
288
    // For 2nd and subsequent orders, we vary the ping data size:
289
    for ( i = 0; i < N; i++ ) {
290
        sprintf( order, "%s %s %d %d", "SLOW_PING",
291
                 inet_ntoa(bp->bp_yiaddr),
292
                 testtime,
293
                 filesize + 100 * i );
294
        autohost_tell( bp, order );
295
    }
296
}
297
 
298
#define TESTTIME (5 * 60) // Seconds
299
 
300
#define NUM_SESSIONS 10 // why not?
301
 
302
void
303
net_test(cyg_addrword_t param)
304
{
305
    int i;
306
    int numtests;
307
    cyg_tick_count_t ticks;
308
    CYG_TEST_INIT();
309
    CYG_TEST_INFO("Start PING server test");
310
    init_all_network_interfaces();
311
 
312
    autohost_init();
313
 
314
    TNR_INIT();
315
 
316
    numtests = NUM_SESSIONS; // The number of pingers started OK
317
    i = numtests;
318
    // Now command the host to do ping to us...
319
#ifdef CYGHWR_NET_DRIVER_ETH0
320
    if (eth0_up) {
321
        // 2 small tests by default and a couple of larger ones
322
        do_ping_tests(&eth0_bootp_data, 1, TESTTIME, 64);
323
        do_ping_tests(&eth0_bootp_data, 2, TESTTIME, 1000);
324
        do_ping_tests(&eth0_bootp_data, 2, TESTTIME, 10000);
325
        i -= 5;
326
    }
327
#endif
328
#ifdef CYGHWR_NET_DRIVER_ETH1
329
    if (eth1_up && i > 0) {
330
        // rest of the tests depending...
331
        do_ping_tests(&eth1_bootp_data, 2, TESTTIME, 64);
332
        i -= 2;
333
        do_ping_tests(&eth1_bootp_data, 1, TESTTIME, 4000);
334
        i -= 1;
335
        do_ping_tests(&eth1_bootp_data, i, TESTTIME, 1200);
336
        i = 0; // Since we just used up the rest
337
    }
338
#endif
339
    numtests -= i; // Adjust to how many we *actually* requested
340
 
341
    // Let the server run for 5 minutes
342
    cyg_thread_delay(2*100); // let the stuff start up first
343
    TNR_ON();
344
 
345
    ticks = cyg_current_time() + TESTTIME * 100; // FIXME - assume cS clock.
346
    while (1) {
347
        if ( ticks < cyg_current_time() ) break;
348
#ifdef CYGHWR_NET_DRIVER_ETH0
349
        if (eth0_up) {
350
            ping_test(&eth0_bootp_data);
351
        }
352
#endif
353
#ifdef CYGHWR_NET_DRIVER_ETH1
354
        if ( ticks < cyg_current_time() ) break;
355
        if (eth1_up) {
356
            ping_test(&eth1_bootp_data);
357
        }
358
#endif
359
    };
360
 
361
    // Additional delay 'cos host may be slower than us - and it has to
362
    // complete a transfer anyway:
363
    cyg_thread_delay(  30    *100); // FIXME - assume cS clock.
364
    TNR_OFF();
365
 
366
    autohost_end( numtests ); // check for N pass messages from hosts
367
 
368
    TNR_PRINT_ACTIVITY();
369
    CYG_TEST_EXIT("Done");
370
}
371
 
372
void
373
cyg_start(void)
374
{
375
    // Create a main thread, so we can run the scheduler and have time 'pass'
376
    cyg_thread_create(10,                // Priority - just a number
377
                      net_test,          // entry
378
                      0,                 // entry parameter
379
                      "Network test",    // Name
380
                      &stack[0],         // Stack
381
                      STACK_SIZE,        // Size
382
                      &thread_handle,    // Handle
383
                      &thread_data       // Thread data structure
384
            );
385
    cyg_thread_resume(thread_handle);  // Start it
386
    cyg_scheduler_start();
387
}
388
 
389
// EOF slowpingmux.c

powered by: WebSVN 2.1.0

© copyright 1999-2025 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.