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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [net/] [common/] [current/] [tests/] [dhcp_test2.c] - Blame information for rev 856

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

Line No. Rev Author Line
1 786 skrzyp
//==========================================================================
2
//
3
//      tests/dhcp_test2.c
4
//
5
//      Test of repeatedly releasing and reacquiring DHCP leases
6
//
7
//==========================================================================
8
// ####BSDALTCOPYRIGHTBEGIN####                                             
9
// -------------------------------------------                              
10
// Portions of this software may have been derived from FreeBSD, OpenBSD,   
11
// or other sources, and if so are covered by the appropriate copyright     
12
// and license included herein.                                             
13
// -------------------------------------------                              
14
// ####BSDALTCOPYRIGHTEND####                                               
15
//==========================================================================
16
//#####DESCRIPTIONBEGIN####
17
//
18
// Author(s):    hmt, tomislav.sostaric@ascom.ch
19
// Contributors: 
20
// Date:         2002-03-11
21
// Purpose:      Test repeated up-down cycles of interfaces with DHCP.
22
// Description:  
23
//              
24
//
25
//####DESCRIPTIONEND####
26
//
27
//==========================================================================
28
 
29
/*
30
 * A test program for bringing out the DHCP memory leak bug
31
 * This file is in the public domain and may be used for any purpose
32
 */
33
 
34
/* INCLUDES */
35
 
36
#include <network.h>
37
 
38
#include <pkgconf/system.h>
39
#include <pkgconf/net.h>
40
 
41
#include <cyg/infra/testcase.h>
42
 
43
#ifdef CYGPKG_NET_DHCP
44
 
45
#ifdef CYGBLD_DEVS_ETH_DEVICE_H    // Get the device config if it exists
46
#include CYGBLD_DEVS_ETH_DEVICE_H  // May provide CYGTST_DEVS_ETH_TEST_NET_REALTIME
47
#endif
48
 
49
 
50
#include <stdio.h>                      /* printf */
51
#include <string.h>                     /* strlen */
52
#include <cyg/kernel/kapi.h>            /* All the kernel specific stuff */
53
#include <cyg/io/io.h>                  /* I/O functions */
54
#include <cyg/hal/hal_arch.h>           /* CYGNUM_HAL_STACK_SIZE_TYPICAL */
55
#include <network.h>
56
#include <dhcp.h>
57
 
58
// ------------------------------------------------------------------------
59
 
60
#define FALSE 0
61
#define TRUE 1
62
#define TICKS_PER_SEC 100
63
 
64
#define nLOOPS 1000000
65
#define LOOPS 10
66
 
67
// ------------------------------------------------------------------------
68
 
69
#define NINTERFACES (2)
70
 
71
struct bootp bootp_data[ NINTERFACES ];
72
cyg_uint8 state[ NINTERFACES ];
73
struct dhcp_lease lease[ NINTERFACES ];
74
int up[ NINTERFACES ], oldup[ NINTERFACES ];
75
 
76
char *eth[ NINTERFACES ] = { "eth0", "eth1" };
77
 
78
int counter = 0;
79
 
80
void
81
tdhcp_init( int which )
82
{
83
    printf("%s: Initializing device to use DHCP.\n", eth[which] );
84
    bzero( &bootp_data[which], sizeof( *bootp_data ) );
85
    state[which] = 0;
86
    bzero( &lease[which], sizeof( *lease ) );
87
    up[which] = oldup[which] = FALSE;
88
}
89
 
90
 
91
void
92
tdhcp_do_one( int which )
93
{
94
    oldup[which] = up[which];
95
    up[which] = do_dhcp( eth[which], &bootp_data[which], &state[which], &lease[which] );
96
}
97
 
98
void
99
tdhcp_updown_one( int which )
100
{
101
    /* DHCP wants interface to go up or down, do it. */
102
    if (up[which] != oldup[which]) {
103
        if (up[which]) {
104
            char tbuf[256];
105
            int result;
106
 
107
            result = init_net( eth[which], &bootp_data[which]);
108
            if (!result) {
109
                printf("%s: Initialization (DHCP) failed.\n", eth[which] );
110
                return;
111
            }
112
 
113
            if (lease[which].expiry == (cyg_tick_count_t)-1) {
114
                strcpy(tbuf, "infinite");
115
            } else {
116
                cyg_tick_count_t now;
117
                unsigned int exp;
118
 
119
                now = cyg_current_time();
120
                exp = ((lease[which].expiry > now) ? lease[which].expiry - now : 0) / TICKS_PER_SEC;
121
                sprintf(tbuf, "%ud %uh %um %us", exp / 86400,
122
                        (exp / 3600) % 24, (exp / 60) % 60, exp % 60);
123
            }
124
            printf("%s: Configured by DHCP, IP address %s, "
125
                   "lease expiry: %s.\n", eth[which],
126
                   inet_ntoa(bootp_data[which].bp_yiaddr), tbuf);
127
            printf("%s: Interface ready\n", eth[which] );
128
        } else {
129
            printf("%s: Deconfigured by DHCP.\n", eth[which] );
130
            cyg_thread_delay(10);
131
            do_dhcp_down_net( eth[which], &bootp_data[which], &state[which], &lease[which]);
132
            state[which] = DHCPSTATE_INIT;
133
        }
134
    }
135
}
136
 
137
void
138
tdhcp_release_one( int which )
139
{
140
    /* If DHCP failed (most probably because there was no DHCP server around),
141
     * sleep a bit, then try again. Otherwise, just wait until DHCP needs our
142
     * attention.
143
     */
144
    if (state[which] == DHCPSTATE_FAILED) {
145
        printf("%s: DHCP failed, will retry later.\n", eth[which] );
146
        cyg_thread_delay(10);
147
        printf("%s: Retrying DHCP.\n", eth[which] );
148
    }
149
    cyg_thread_delay(10);
150
    printf("%s: Releasing DHCP lease.\n", eth[which] );
151
    do_dhcp_release( eth[which], &bootp_data[which], &state[which], &lease[which]);
152
    cyg_thread_delay(10);
153
    up[which] = FALSE;
154
    state[which] = DHCPSTATE_INIT;
155
}
156
 
157
 
158
static void
159
dhcp_if_fn(cyg_addrword_t data)
160
{
161
    CYG_TEST_INIT();
162
 
163
#ifdef CYGHWR_NET_DRIVER_ETH0
164
    tdhcp_init( 0 );
165
#endif // CYGHWR_NET_DRIVER_ETH0
166
#ifdef CYGHWR_NET_DRIVER_ETH1
167
    tdhcp_init( 1 );
168
#endif // CYGHWR_NET_DRIVER_ETH1
169
 
170
    while ( ++counter < LOOPS ) {
171
        diag_printf( "--------- counter %d ---------\n", counter );
172
#ifdef CYGHWR_NET_DRIVER_ETH0
173
        tdhcp_do_one( 0 );
174
#endif // CYGHWR_NET_DRIVER_ETH0
175
#ifdef CYGHWR_NET_DRIVER_ETH1
176
        tdhcp_do_one( 1 );
177
#endif // CYGHWR_NET_DRIVER_ETH1
178
 
179
#ifdef CYGHWR_NET_DRIVER_ETH0
180
        tdhcp_updown_one( 0 );
181
#endif // CYGHWR_NET_DRIVER_ETH0
182
#ifdef CYGHWR_NET_DRIVER_ETH1
183
        tdhcp_updown_one( 1 );
184
#endif // CYGHWR_NET_DRIVER_ETH1
185
 
186
#ifdef CYGHWR_NET_DRIVER_ETH0
187
        tdhcp_release_one( 0 );
188
#endif // CYGHWR_NET_DRIVER_ETH0
189
#ifdef CYGHWR_NET_DRIVER_ETH1
190
        tdhcp_release_one( 1 );
191
#endif // CYGHWR_NET_DRIVER_ETH1
192
    }
193
    CYG_TEST_PASS_EXIT( "All done" );
194
}
195
 
196
 
197
// ------------------------------------------------------------------------
198
 
199
#define NTHREADS 1
200
#define STACKSIZE ( CYGNUM_HAL_STACK_SIZE_TYPICAL + 4096 )
201
 
202
static cyg_handle_t thread[NTHREADS];
203
static cyg_thread thread_obj[NTHREADS];
204
static char stack[NTHREADS][STACKSIZE];
205
 
206
void cyg_user_start(void)
207
{
208
    // Use the DHCP thread prio as provided even though we're not using the thread itself;
209
    // This priroty should be right (lower than net thread prio) by default.
210
    cyg_thread_create(CYGPKG_NET_DHCP_THREAD_PRIORITY, dhcp_if_fn, (cyg_addrword_t) 0, "dhcp",
211
                      (void *)stack[0], STACKSIZE, &thread[0], &thread_obj[0]);
212
    cyg_thread_resume(thread[0]);
213
}
214
 
215
// ------------------------------------------------------------------------
216
 
217
#else // CYGPKG_NET_DHCP
218
 
219
void cyg_user_start(void)
220
{
221
    CYG_TEST_INIT();
222
    CYG_TEST_NA( "DHCP is not enabled" );
223
    CYG_TEST_EXIT( "DHCP is not enabled" );
224
}
225
 
226
#endif // CYGPKG_NET_DHCP
227
 
228
// ------------------------------------------------------------------------
229
 
230
// EOF dhcp_test2.c

powered by: WebSVN 2.1.0

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