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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [net/] [common/] [v2_0/] [tests/] [dhcp_test2.c] - Blame information for rev 174

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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