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