28bdaf756240b932e1730c6aa9ad30bdad8aa7a4
[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.7 2005/06/15 11:56:03 joerg 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 <sys/thread2.h>
42
43 #include <net/if.h>
44
45 #include "../include/i4b_mbuf.h"
46
47 #define I4B_MBUF_DEBUG
48 #undef I4B_MBUF_TYPE_DEBUG
49
50 #ifdef I4B_MBUF_TYPE_DEBUG
51
52 #define MT_DCHAN        42
53 #define MT_BCHAN        43
54 #define MT_I4B_D        MT_DCHAN
55 #define MT_I4B_B        MT_BCHAN
56
57 #else /* ! I4B_MBUF_TYPE_DEBUG */
58
59 #define MT_I4B_D        MT_DATA
60 #define MT_I4B_B        MT_DATA
61
62 #endif /* I4B_MBUF_TYPE_DEBUG */
63
64 /*---------------------------------------------------------------------------*
65  *      allocate D-channel mbuf space
66  *---------------------------------------------------------------------------*/
67 struct mbuf*
68 i4b_Dgetmbuf(int len)
69 {
70         struct mbuf *m;
71
72         if(len > MCLBYTES)      /* if length > max extension size */
73         {
74
75 #ifdef I4B_MBUF_DEBUG
76                 printf("i4b_getmbuf: error - len(%d) > MCLBYTES(%d)\n",
77                                         len, MCLBYTES);
78 #endif
79                 
80                 return(NULL);
81         }
82
83         MGETHDR(m, MB_DONTWAIT, MT_I4B_D);      /* get mbuf with pkthdr */
84
85         /* did we actually get the mbuf ? */
86
87         if(!m)  
88         {
89
90 #ifdef I4B_MBUF_DEBUG
91                 printf("i4b_getbuf: error - MGETHDR failed!\n");
92 #endif
93
94                 return(NULL);
95         }
96
97         if(len >= MHLEN)
98         {
99                 MCLGET(m, MB_DONTWAIT);
100
101                 if(!(m->m_flags & M_EXT))
102                 {
103                         m_freem(m);
104
105 #ifdef I4B_MBUF_DEBUG
106                         printf("i4b_getbuf: error - MCLGET failed, len(%d)\n", len);
107 #endif
108                         
109                         return (NULL);
110                 }
111         }
112
113         m->m_len = len;
114
115         return(m);
116 }
117
118 /*---------------------------------------------------------------------------*
119  *      free a D-channel mbuf
120  *---------------------------------------------------------------------------*/
121 void
122 i4b_Dfreembuf(struct mbuf *m)
123 {
124         if(m)
125                 m_freem(m);
126 }
127
128 /*---------------------------------------------------------------------------*
129  *      clear a D-channel ifqueue from data
130  *---------------------------------------------------------------------------*/
131 void
132 i4b_Dcleanifq(struct ifqueue *ifq)
133 {
134         crit_enter();
135
136         IF_DRAIN(ifq);
137
138         crit_exit();
139 }
140
141 /*---------------------------------------------------------------------------*
142  *      allocate B-channel mbuf space
143  *---------------------------------------------------------------------------*/
144 struct mbuf*
145 i4b_Bgetmbuf(int len)
146 {
147         struct mbuf *m;
148
149         if(len > MCLBYTES)      /* if length > max extension size */
150         {
151
152 #ifdef I4B_MBUF_DEBUG
153                 printf("i4b_getmbuf: error - len(%d) > MCLBYTES(%d)\n",
154                                         len, MCLBYTES);
155 #endif
156                 
157                 return(NULL);
158         }
159
160         MGETHDR(m, MB_DONTWAIT, MT_I4B_B);      /* get mbuf with pkthdr */
161
162         /* did we actually get the mbuf ? */
163
164         if(!m)  
165         {
166
167 #ifdef I4B_MBUF_DEBUG
168                 printf("i4b_getbuf: error - MGETHDR failed!\n");
169 #endif
170
171                 return(NULL);
172         }
173
174         if(len >= MHLEN)
175         {
176                 MCLGET(m, MB_DONTWAIT);
177
178                 if(!(m->m_flags & M_EXT))
179                 {
180                         m_freem(m);
181
182 #ifdef I4B_MBUF_DEBUG
183                         printf("i4b_getbuf: error - MCLGET failed, len(%d)\n", len);
184 #endif
185                         
186                         return (NULL);
187                 }
188         }
189
190         m->m_len = len;
191
192         return(m);
193 }
194
195 /*---------------------------------------------------------------------------*
196  *      free a B-channel mbuf
197  *---------------------------------------------------------------------------*/
198 void
199 i4b_Bfreembuf(struct mbuf *m)
200 {
201         if(m)
202                 m_freem(m);
203 }
204
205 /*---------------------------------------------------------------------------*
206  *      clear a B-channel ifqueue from data
207  *---------------------------------------------------------------------------*/
208 void
209 i4b_Bcleanifq(struct ifqueue *ifq)
210 {
211         crit_enter();
212         
213         IF_DRAIN(ifq);
214
215         crit_exit();
216 }
217
218 /* EOF */