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