]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/i4b/layer2/i4b_mbuf.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / sys / i4b / layer2 / i4b_mbuf.c
1 /*-
2  * Copyright (c) 1997, 2002 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  *
28  *      i4b - mbuf handling support routines
29  *      ------------------------------------
30  *      last edit-date: [Sat Mar  9 17:51:22 2002]
31  *
32  *---------------------------------------------------------------------------*/
33
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
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 <i4b/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 #define MT_DCHAN        42
51 #define MT_BCHAN        43
52
53 #define MT_I4B_D        MT_DCHAN
54 #define MT_I4B_B        MT_BCHAN
55
56 #else /* ! I4B_MBUF_TYPE_DEBUG */
57
58 #define MT_I4B_D        MT_DATA
59 #define MT_I4B_B        MT_DATA
60
61 #endif /* I4B_MBUF_TYPE_DEBUG */
62
63 /*---------------------------------------------------------------------------*
64  *      allocate D-channel mbuf space
65  *---------------------------------------------------------------------------*/
66 struct mbuf*
67 i4b_Dgetmbuf(int len)
68 {
69         struct mbuf *m;
70
71         if(len > MCLBYTES)      /* if length > max extension size */
72         {
73
74 #ifdef I4B_MBUF_DEBUG
75                 printf("i4b_getmbuf: error - len(%d) > MCLBYTES(%d)\n",
76                                         len, MCLBYTES);
77 #endif
78                 
79                 return(NULL);
80         }
81
82         MGETHDR(m, M_DONTWAIT, MT_I4B_D);       /* get mbuf with pkthdr */
83
84         /* did we actually get the mbuf ? */
85
86         if(!m)  
87         {
88
89 #ifdef I4B_MBUF_DEBUG
90                 printf("i4b_getbuf: error - MGETHDR failed!\n");
91 #endif
92
93                 return(NULL);
94         }
95
96         if(len >= MHLEN)
97         {
98                 MCLGET(m, M_DONTWAIT);
99
100                 if(!(m->m_flags & M_EXT))
101                 {
102                         m_freem(m);
103
104 #ifdef I4B_MBUF_DEBUG
105                         printf("i4b_getbuf: error - MCLGET failed, len(%d)\n", len);
106 #endif
107                         
108                         return (NULL);
109                 }
110         }
111
112         m->m_len = len;
113
114         return(m);
115 }
116
117 /*---------------------------------------------------------------------------*
118  *      free a D-channel mbuf
119  *---------------------------------------------------------------------------*/
120 void
121 i4b_Dfreembuf(struct mbuf *m)
122 {
123         if(m)
124                 m_freem(m);
125 }
126
127 /*---------------------------------------------------------------------------*
128  *      clear a D-channel ifqueue from data
129  *---------------------------------------------------------------------------*/
130 void
131 i4b_Dcleanifq(struct ifqueue *ifq)
132 {
133         int x = splimp();
134
135         IF_DRAIN(ifq);
136
137         splx(x);
138 }
139
140 /*---------------------------------------------------------------------------*
141  *      allocate B-channel mbuf space
142  *---------------------------------------------------------------------------*/
143 struct mbuf*
144 i4b_Bgetmbuf(int len)
145 {
146         struct mbuf *m;
147
148         if(len > MCLBYTES)      /* if length > max extension size */
149         {
150
151 #ifdef I4B_MBUF_DEBUG
152                 printf("i4b_getmbuf: error - len(%d) > MCLBYTES(%d)\n",
153                                         len, MCLBYTES);
154 #endif
155                 
156                 return(NULL);
157         }
158
159         MGETHDR(m, M_DONTWAIT, MT_I4B_B);       /* get mbuf with pkthdr */
160
161         /* did we actually get the mbuf ? */
162
163         if(!m)  
164         {
165
166 #ifdef I4B_MBUF_DEBUG
167                 printf("i4b_getbuf: error - MGETHDR failed!\n");
168 #endif
169
170                 return(NULL);
171         }
172
173         if(len >= MHLEN)
174         {
175                 MCLGET(m, M_DONTWAIT);
176
177                 if(!(m->m_flags & M_EXT))
178                 {
179                         m_freem(m);
180
181 #ifdef I4B_MBUF_DEBUG
182                         printf("i4b_getbuf: error - MCLGET failed, len(%d)\n", len);
183 #endif
184                         
185                         return (NULL);
186                 }
187         }
188
189         m->m_len = len;
190
191         return(m);
192 }
193
194 /*---------------------------------------------------------------------------*
195  *      free a B-channel mbuf
196  *---------------------------------------------------------------------------*/
197 void
198 i4b_Bfreembuf(struct mbuf *m)
199 {
200         if(m)
201                 m_freem(m);
202 }
203
204 /*---------------------------------------------------------------------------*
205  *      clear a B-channel ifqueue from data
206  *---------------------------------------------------------------------------*/
207 void
208 i4b_Bcleanifq(struct ifqueue *ifq)
209 {
210         int x = splimp();
211         
212         IF_DRAIN(ifq);
213
214         splx(x);
215 }
216
217 /* EOF */