root/ar5315_microredboot/microredboot/ecos/packages/redboot/current/src/net/pktbuf.c

Revision 12368, 4.1 kB (checked in by BrainSlayer, 5 months ago)

tftp server added, supports wiligear, ubiquiti and dd-wrt webflash format

Line 
1 //==========================================================================
2 //
3 //      net/pktbuf.c
4 //
5 //      Stand-alone network packet support for RedBoot
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 Red Hat, 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 // Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
37 // at http://sources.redhat.com/ecos/ecos-license/
38 // -------------------------------------------
39 //####ECOSGPLCOPYRIGHTEND####
40 //==========================================================================
41 //#####DESCRIPTIONBEGIN####
42 //
43 // Author(s):    gthomas
44 // Contributors: gthomas
45 // Date:         2000-07-14
46 // Purpose:     
47 // Description: 
48 //             
49 // This code is part of RedBoot (tm).
50 //
51 //####DESCRIPTIONEND####
52 //
53 //==========================================================================
54
55 #include <redboot.h>
56 #include <net/net.h>
57
58 #define MAX_PKTBUF CYGNUM_REDBOOT_NETWORKING_MAX_PKTBUF
59
60 #define BUFF_STATS 1
61
62 #if BUFF_STATS
63 int max_alloc = 0;
64 int num_alloc = 0;
65 int num_free = 0;
66 #endif
67
68 static pktbuf_t pktbuf_list[MAX_PKTBUF];
69 static word bufdata[MAX_PKTBUF][ETH_MAX_PKTLEN / 2 + 1];
70 static pktbuf_t *free_list;
71
72 /*
73  * Initialize the free list.
74  */
75 void __pktbuf_init(void)
76 {
77         int i;
78         word *p;
79         static int init = 0;
80
81         if (init)
82                 return;
83         init = 1;
84
85         for (i = 0; i < MAX_PKTBUF; i++) {
86                 p = bufdata[i];
87                 if ((((unsigned long)p) & 2) != 0)
88                         ++p;
89                 pktbuf_list[i].buf = p;
90                 pktbuf_list[i].bufsize = ETH_MAX_PKTLEN;
91                 pktbuf_list[i].next = free_list;
92                 free_list = &pktbuf_list[i];
93         }
94 }
95
96 void __pktbuf_dump(void)
97 {
98         int i;
99         for (i = 0; i < MAX_PKTBUF; i++) {
100                 diag_printf("Buf[%d]/%p: buf: %p, len: %d/%d, next: %p\n",
101                             i,
102                             (void *)&pktbuf_list[i],
103                             (void *)pktbuf_list[i].buf,
104                             pktbuf_list[i].bufsize,
105                             pktbuf_list[i].pkt_bytes,
106                             (void *)pktbuf_list[i].next);
107         }
108         diag_printf("Free list = %p\n", (void *)free_list);
109 }
110
111 /*
112  * simple pktbuf allocation
113  */
114 pktbuf_t *__pktbuf_alloc(int nbytes)
115 {
116         pktbuf_t *p = free_list;
117
118         if (p) {
119                 free_list = p->next;
120                 p->ip_hdr = (ip_header_t *) p->buf;
121                 p->tcp_hdr = (tcp_header_t *) (p->ip_hdr + 1);
122                 p->pkt_bytes = 0;
123 #if BUFF_STATS
124                 ++num_alloc;
125                 if ((num_alloc - num_free) > max_alloc)
126                         max_alloc = num_alloc - num_free;
127 #endif
128         }
129         return p;
130 }
131
132 /*
133  * free a pktbuf.
134  */
135 void __pktbuf_free(pktbuf_t * pkt)
136 {
137 #if BUFF_STATS
138         --num_alloc;
139 #endif
140 #ifdef BSP_LOG
141         {
142                 int i;
143                 word *p;
144
145                 for (i = 0; i < MAX_PKTBUF; i++) {
146                         p = bufdata[i];
147                         if ((((unsigned long)p) & 2) == 0)
148                                 ++p;
149                         if (p == (word *) pkt)
150                                 break;
151                 }
152                 if (i < MAX_PKTBUF) {
153                         BSPLOG(bsp_log("__pktbuf_free: bad pkt[%x].\n", pkt));
154                         BSPLOG(while (1)) ;
155                 }
156         }
157 #endif
158         pkt->next = free_list;
159         free_list = pkt;
160 }
Note: See TracBrowser for help on using the browser.