Initial import from FreeBSD RELENG_4:
[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  *
32  *      last edit-date: [Sat Jan 13 13:15:45 2001]
33  *
34  *---------------------------------------------------------------------------*/
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/mbuf.h>
39 #include <sys/socket.h>
40 #include <net/if.h>
41
42 #include <i4b/include/i4b_mbuf.h>
43
44 #define I4B_MBUF_DEBUG
45 #undef I4B_MBUF_TYPE_DEBUG
46
47 #ifdef I4B_MBUF_TYPE_DEBUG
48
49 #ifdef  __FreeBSD__
50
51 #define MT_DCHAN        42
52 #define MT_BCHAN        43
53
54 #else /* NetBSD */
55
56 #define MT_DCHAN        MT_DATA
57 #define MT_BCHAN        MT_DATA
58
59 #endif
60
61 #define MT_I4B_D        MT_DCHAN
62 #define MT_I4B_B        MT_BCHAN
63
64 #else /* ! I4B_MBUF_TYPE_DEBUG */
65
66 #define MT_I4B_D        MT_DATA
67 #define MT_I4B_B        MT_DATA
68
69 #endif /* I4B_MBUF_TYPE_DEBUG */
70
71 /*---------------------------------------------------------------------------*
72  *      allocate D-channel mbuf space
73  *---------------------------------------------------------------------------*/
74 struct mbuf*
75 i4b_Dgetmbuf(int len)
76 {
77         struct mbuf *m;
78
79         if(len > MCLBYTES)      /* if length > max extension size */
80         {
81
82 #ifdef I4B_MBUF_DEBUG
83                 printf("i4b_getmbuf: error - len(%d) > MCLBYTES(%d)\n",
84                                         len, MCLBYTES);
85 #endif
86                 
87                 return(NULL);
88         }
89
90         MGETHDR(m, M_DONTWAIT, MT_I4B_D);       /* get mbuf with pkthdr */
91
92         /* did we actually get the mbuf ? */
93
94         if(!m)  
95         {
96
97 #ifdef I4B_MBUF_DEBUG
98                 printf("i4b_getbuf: error - MGETHDR failed!\n");
99 #endif
100
101                 return(NULL);
102         }
103
104         if(len >= MHLEN)
105         {
106                 MCLGET(m, M_DONTWAIT);
107
108                 if(!(m->m_flags & M_EXT))
109                 {
110                         m_freem(m);
111
112 #ifdef I4B_MBUF_DEBUG
113                         printf("i4b_getbuf: error - MCLGET failed, len(%d)\n", len);
114 #endif
115                         
116                         return (NULL);
117                 }
118         }
119
120         m->m_len = len;
121
122         return(m);
123 }
124
125 /*---------------------------------------------------------------------------*
126  *      free a D-channel mbuf
127  *---------------------------------------------------------------------------*/
128 void
129 i4b_Dfreembuf(struct mbuf *m)
130 {
131         if(m)
132                 m_freem(m);
133 }
134
135 /*---------------------------------------------------------------------------*
136  *      clear a D-channel ifqueue from data
137  *---------------------------------------------------------------------------*/
138 void
139 i4b_Dcleanifq(struct ifqueue *ifq)
140 {
141         int x = splimp();
142
143 #if defined (__FreeBSD__) && __FreeBSD__ > 4
144         IF_DRAIN(ifq);
145 #else
146         struct mbuf *m;
147         while(!IF_QEMPTY(ifq))
148         {
149                 IF_DEQUEUE(ifq, m);
150                 i4b_Dfreembuf(m);
151         }
152 #endif
153         splx(x);
154 }
155
156 /*---------------------------------------------------------------------------*
157  *      allocate B-channel mbuf space
158  *---------------------------------------------------------------------------*/
159 struct mbuf*
160 i4b_Bgetmbuf(int len)
161 {
162         struct mbuf *m;
163
164         if(len > MCLBYTES)      /* if length > max extension size */
165         {
166
167 #ifdef I4B_MBUF_DEBUG
168                 printf("i4b_getmbuf: error - len(%d) > MCLBYTES(%d)\n",
169                                         len, MCLBYTES);
170 #endif
171                 
172                 return(NULL);
173         }
174
175         MGETHDR(m, M_DONTWAIT, MT_I4B_B);       /* get mbuf with pkthdr */
176
177         /* did we actually get the mbuf ? */
178
179         if(!m)  
180         {
181
182 #ifdef I4B_MBUF_DEBUG
183                 printf("i4b_getbuf: error - MGETHDR failed!\n");
184 #endif
185
186                 return(NULL);
187         }
188
189         if(len >= MHLEN)
190         {
191                 MCLGET(m, M_DONTWAIT);
192
193                 if(!(m->m_flags & M_EXT))
194                 {
195                         m_freem(m);
196
197 #ifdef I4B_MBUF_DEBUG
198                         printf("i4b_getbuf: error - MCLGET failed, len(%d)\n", len);
199 #endif
200                         
201                         return (NULL);
202                 }
203         }
204
205         m->m_len = len;
206
207         return(m);
208 }
209
210 /*---------------------------------------------------------------------------*
211  *      free a B-channel mbuf
212  *---------------------------------------------------------------------------*/
213 void
214 i4b_Bfreembuf(struct mbuf *m)
215 {
216         if(m)
217                 m_freem(m);
218 }
219
220 /*---------------------------------------------------------------------------*
221  *      clear a B-channel ifqueue from data
222  *---------------------------------------------------------------------------*/
223 void
224 i4b_Bcleanifq(struct ifqueue *ifq)
225 {
226         int x = splimp();
227         
228 #if defined (__FreeBSD__) && __FreeBSD__ > 4
229         IF_DRAIN(ifq);
230 #else
231         struct mbuf *m;
232         while(!IF_QEMPTY(ifq))
233         {
234                 IF_DEQUEUE(ifq, m);
235                 i4b_Bfreembuf(m);
236         }
237 #endif
238         splx(x);
239 }
240
241 /* EOF */