kernel tree reorganization stage 1: Major cvs repository work (not logged as
[dragonfly.git] / sys / net / i4b / layer2 / i4b_mbuf.c
1 /*
2  * Copyright (c) 1997, 2001 Hellmuth Michaelis. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  *
25  *---------------------------------------------------------------------------
26  *
27  *      i4b - mbuf handling support routines
28  *      ------------------------------------
29  *
30  * $FreeBSD: src/sys/i4b/layer2/i4b_mbuf.c,v 1.6.2.1 2001/08/10 14:08:41 obrien Exp $
31  * $DragonFly: src/sys/net/i4b/layer2/i4b_mbuf.c,v 1.3 2003/08/07 21:17:29 dillon Exp $
32  *
33  *      last edit-date: [Sat Jan 13 13:15:45 2001]
34  *
35  *---------------------------------------------------------------------------*/
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/mbuf.h>
40 #include <sys/socket.h>
41 #include <net/if.h>
42
43 #include "../include/i4b_mbuf.h"
44
45 #define I4B_MBUF_DEBUG
46 #undef I4B_MBUF_TYPE_DEBUG
47
48 #ifdef I4B_MBUF_TYPE_DEBUG
49
50 #ifdef  __FreeBSD__
51
52 #define MT_DCHAN        42
53 #define MT_BCHAN        43
54
55 #else /* NetBSD */
56
57 #define MT_DCHAN        MT_DATA
58 #define MT_BCHAN        MT_DATA
59
60 #endif
61
62 #define MT_I4B_D        MT_DCHAN
63 #define MT_I4B_B        MT_BCHAN
64
65 #else /* ! I4B_MBUF_TYPE_DEBUG */
66
67 #define MT_I4B_D        MT_DATA
68 #define MT_I4B_B        MT_DATA
69
70 #endif /* I4B_MBUF_TYPE_DEBUG */
71
72 /*---------------------------------------------------------------------------*
73  *      allocate D-channel mbuf space
74  *---------------------------------------------------------------------------*/
75 struct mbuf*
76 i4b_Dgetmbuf(int len)
77 {
78         struct mbuf *m;
79
80         if(len > MCLBYTES)      /* if length > max extension size */
81         {
82
83 #ifdef I4B_MBUF_DEBUG
84                 printf("i4b_getmbuf: error - len(%d) > MCLBYTES(%d)\n",
85                                         len, MCLBYTES);
86 #endif
87                 
88                 return(NULL);
89         }
90
91         MGETHDR(m, M_DONTWAIT, MT_I4B_D);       /* get mbuf with pkthdr */
92
93         /* did we actually get the mbuf ? */
94
95         if(!m)  
96         {
97
98 #ifdef I4B_MBUF_DEBUG
99                 printf("i4b_getbuf: error - MGETHDR failed!\n");
100 #endif
101
102                 return(NULL);
103         }
104
105         if(len >= MHLEN)
106         {
107                 MCLGET(m, M_DONTWAIT);
108
109                 if(!(m->m_flags & M_EXT))
110                 {
111                         m_freem(m);
112
113 #ifdef I4B_MBUF_DEBUG
114                         printf("i4b_getbuf: error - MCLGET failed, len(%d)\n", len);
115 #endif
116                         
117                         return (NULL);
118                 }
119         }
120
121         m->m_len = len;
122
123         return(m);
124 }
125
126 /*---------------------------------------------------------------------------*
127  *      free a D-channel mbuf
128  *---------------------------------------------------------------------------*/
129 void
130 i4b_Dfreembuf(struct mbuf *m)
131 {
132         if(m)
133                 m_freem(m);
134 }
135
136 /*---------------------------------------------------------------------------*
137  *      clear a D-channel ifqueue from data
138  *---------------------------------------------------------------------------*/
139 void
140 i4b_Dcleanifq(struct ifqueue *ifq)
141 {
142         int x = splimp();
143
144 #if defined (__FreeBSD__) && __FreeBSD__ > 4
145         IF_DRAIN(ifq);
146 #else
147         struct mbuf *m;
148         while(!IF_QEMPTY(ifq))
149         {
150                 IF_DEQUEUE(ifq, m);
151                 i4b_Dfreembuf(m);
152         }
153 #endif
154         splx(x);
155 }
156
157 /*---------------------------------------------------------------------------*
158  *      allocate B-channel mbuf space
159  *---------------------------------------------------------------------------*/
160 struct mbuf*
161 i4b_Bgetmbuf(int len)
162 {
163         struct mbuf *m;
164
165         if(len > MCLBYTES)      /* if length > max extension size */
166         {
167
168 #ifdef I4B_MBUF_DEBUG
169                 printf("i4b_getmbuf: error - len(%d) > MCLBYTES(%d)\n",
170                                         len, MCLBYTES);
171 #endif
172                 
173                 return(NULL);
174         }
175
176         MGETHDR(m, M_DONTWAIT, MT_I4B_B);       /* get mbuf with pkthdr */
177
178         /* did we actually get the mbuf ? */
179
180         if(!m)  
181         {
182
183 #ifdef I4B_MBUF_DEBUG
184                 printf("i4b_getbuf: error - MGETHDR failed!\n");
185 #endif
186
187                 return(NULL);
188         }
189
190         if(len >= MHLEN)
191         {
192                 MCLGET(m, M_DONTWAIT);
193
194                 if(!(m->m_flags & M_EXT))
195                 {
196                         m_freem(m);
197
198 #ifdef I4B_MBUF_DEBUG
199                         printf("i4b_getbuf: error - MCLGET failed, len(%d)\n", len);
200 #endif
201                         
202                         return (NULL);
203                 }
204         }
205
206         m->m_len = len;
207
208         return(m);
209 }
210
211 /*---------------------------------------------------------------------------*
212  *      free a B-channel mbuf
213  *---------------------------------------------------------------------------*/
214 void
215 i4b_Bfreembuf(struct mbuf *m)
216 {
217         if(m)
218                 m_freem(m);
219 }
220
221 /*---------------------------------------------------------------------------*
222  *      clear a B-channel ifqueue from data
223  *---------------------------------------------------------------------------*/
224 void
225 i4b_Bcleanifq(struct ifqueue *ifq)
226 {
227         int x = splimp();
228         
229 #if defined (__FreeBSD__) && __FreeBSD__ > 4
230         IF_DRAIN(ifq);
231 #else
232         struct mbuf *m;
233         while(!IF_QEMPTY(ifq))
234         {
235                 IF_DEQUEUE(ifq, m);
236                 i4b_Bfreembuf(m);
237         }
238 #endif
239         splx(x);
240 }
241
242 /* EOF */