]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/kern/uipc_mbuf2.c
This commit was generated by cvs2svn to compensate for changes in r68349,
[FreeBSD/FreeBSD.git] / sys / kern / uipc_mbuf2.c
1 /*      $FreeBSD$       */
2 /*      $KAME: uipc_mbuf2.c,v 1.15 2000/02/22 14:01:37 itojun Exp $     */
3 /*      $NetBSD: uipc_mbuf.c,v 1.40 1999/04/01 00:23:25 thorpej Exp $   */
4
5 /*
6  * Copyright (C) 1999 WIDE Project.
7  * All rights reserved.
8  * 
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the project nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  * 
21  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33
34 /*
35  * Copyright (c) 1982, 1986, 1988, 1991, 1993
36  *      The Regents of the University of California.  All rights reserved.
37  *
38  * Redistribution and use in source and binary forms, with or without
39  * modification, are permitted provided that the following conditions
40  * are met:
41  * 1. Redistributions of source code must retain the above copyright
42  *    notice, this list of conditions and the following disclaimer.
43  * 2. Redistributions in binary form must reproduce the above copyright
44  *    notice, this list of conditions and the following disclaimer in the
45  *    documentation and/or other materials provided with the distribution.
46  * 3. All advertising materials mentioning features or use of this software
47  *    must display the following acknowledgement:
48  *      This product includes software developed by the University of
49  *      California, Berkeley and its contributors.
50  * 4. Neither the name of the University nor the names of its contributors
51  *    may be used to endorse or promote products derived from this software
52  *    without specific prior written permission.
53  *
54  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64  * SUCH DAMAGE.
65  *
66  *      @(#)uipc_mbuf.c 8.4 (Berkeley) 2/14/95
67  */
68
69 /*#define PULLDOWN_DEBUG*/
70
71 #include <sys/param.h>
72 #include <sys/systm.h>
73 #include <sys/malloc.h>
74 #include <sys/mbuf.h>
75
76 /*
77  * ensure that [off, off + len) is contiguous on the mbuf chain "m".
78  * packet chain before "off" is kept untouched.
79  * if offp == NULL, the target will start at <retval, 0> on resulting chain.
80  * if offp != NULL, the target will start at <retval, *offp> on resulting chain.
81  *
82  * on error return (NULL return value), original "m" will be freed.
83  *
84  * XXX M_TRAILINGSPACE/M_LEADINGSPACE on shared cluster (sharedcluster)
85  */
86 struct mbuf *
87 m_pulldown(m, off, len, offp)
88         struct mbuf *m;
89         int off, len;
90         int *offp;
91 {
92         struct mbuf *n, *o;
93         int hlen, tlen, olen;
94         int sharedcluster;
95
96         /* check invalid arguments. */
97         if (m == NULL)
98                 panic("m == NULL in m_pulldown()");
99         if (len > MCLBYTES) {
100                 m_freem(m);
101                 return NULL;    /* impossible */
102         }
103
104 #ifdef PULLDOWN_DEBUG
105     {
106         struct mbuf *t;
107         printf("before:");
108         for (t = m; t; t = t->m_next)
109                 printf(" %d", t->m_len);
110         printf("\n");
111     }
112 #endif
113         n = m;
114         while (n != NULL && off > 0) {
115                 if (n->m_len > off)
116                         break;
117                 off -= n->m_len;
118                 n = n->m_next;
119         }
120         /* be sure to point non-empty mbuf */
121         while (n != NULL && n->m_len == 0)
122                 n = n->m_next;
123         if (!n) {
124                 m_freem(m);
125                 return NULL;    /* mbuf chain too short */
126         }
127
128         /*
129          * the target data is on <n, off>.
130          * if we got enough data on the mbuf "n", we're done.
131          */
132         if ((off == 0 || offp) && len <= n->m_len - off)
133                 goto ok;
134
135         /*
136          * when len < n->m_len - off and off != 0, it is a special case.
137          * len bytes from <n, off> sits in single mbuf, but the caller does
138          * not like the starting position (off).
139          * chop the current mbuf into two pieces, set off to 0.
140          */
141         if (len < n->m_len - off) {
142                 o = m_copym(n, off, n->m_len - off, M_DONTWAIT);
143                 if (o == NULL) {
144                         m_freem(m);
145                         return NULL;    /* ENOBUFS */
146                 }
147                 n->m_len = off;
148                 o->m_next = n->m_next;
149                 n->m_next = o;
150                 n = n->m_next;
151                 off = 0;
152                 goto ok;
153         }
154
155         /*
156          * we need to take hlen from <n, off> and tlen from <n->m_next, 0>,
157          * and construct contiguous mbuf with m_len == len.
158          * note that hlen + tlen == len, and tlen > 0.
159          */
160         hlen = n->m_len - off;
161         tlen = len - hlen;
162
163         /*
164          * ensure that we have enough trailing data on mbuf chain.
165          * if not, we can do nothing about the chain.
166          */
167         olen = 0;
168         for (o = n->m_next; o != NULL; o = o->m_next)
169                 olen += o->m_len;
170         if (hlen + olen < len) {
171                 m_freem(m);
172                 return NULL;    /* mbuf chain too short */
173         }
174
175         /*
176          * easy cases first.
177          * we need to use m_copydata() to get data from <n->m_next, 0>.
178          */
179         if ((n->m_flags & M_EXT) == 0)
180                 sharedcluster = 0;
181         else {
182                 if (n->m_ext.ext_free)
183                         sharedcluster = 1;
184                 else if (MEXT_IS_REF(n))
185                         sharedcluster = 1;
186                 else
187                         sharedcluster = 0;
188         }
189         if ((off == 0 || offp) && M_TRAILINGSPACE(n) >= tlen
190          && !sharedcluster) {
191                 m_copydata(n->m_next, 0, tlen, mtod(n, caddr_t) + n->m_len);
192                 n->m_len += tlen;
193                 m_adj(n->m_next, tlen);
194                 goto ok;
195         }
196         if ((off == 0 || offp) && M_LEADINGSPACE(n->m_next) >= hlen
197          && !sharedcluster) {
198                 n->m_next->m_data -= hlen;
199                 n->m_next->m_len += hlen;
200                 bcopy(mtod(n, caddr_t) + off, mtod(n->m_next, caddr_t), hlen);
201                 n->m_len -= hlen;
202                 n = n->m_next;
203                 off = 0;
204                 goto ok;
205         }
206
207         /*
208          * now, we need to do the hard way.  don't m_copy as there's no room
209          * on both end.
210          */
211         MGET(o, M_DONTWAIT, m->m_type);
212         if (o == NULL) {
213                 m_freem(m);
214                 return NULL;    /* ENOBUFS */
215         }
216         if (len > MHLEN) {      /* use MHLEN just for safety */
217                 MCLGET(o, M_DONTWAIT);
218                 if ((o->m_flags & M_EXT) == 0) {
219                         m_freem(m);
220                         m_free(o);
221                         return NULL;    /* ENOBUFS */
222                 }
223         }
224         /* get hlen from <n, off> into <o, 0> */
225         o->m_len = hlen;
226         bcopy(mtod(n, caddr_t) + off, mtod(o, caddr_t), hlen);
227         n->m_len -= hlen;
228         /* get tlen from <n->m_next, 0> into <o, hlen> */
229         m_copydata(n->m_next, 0, tlen, mtod(o, caddr_t) + o->m_len);
230         o->m_len += tlen;
231         m_adj(n->m_next, tlen);
232         o->m_next = n->m_next;
233         n->m_next = o;
234         n = o;
235         off = 0;
236
237 ok:
238 #ifdef PULLDOWN_DEBUG
239     {
240         struct mbuf *t;
241         printf("after:");
242         for (t = m; t; t = t->m_next)
243                 printf("%c%d", t == n ? '*' : ' ', t->m_len);
244         printf(" (off=%d)\n", off);
245     }
246 #endif
247         if (offp)
248                 *offp = off;
249         return n;
250 }
251
252 /*
253  * pkthdr.aux chain manipulation.
254  * we don't allow clusters at this moment. 
255  */
256 struct mbuf *
257 m_aux_add(m, af, type)
258         struct mbuf *m;
259         int af, type;
260 {
261         struct mbuf *n;
262         struct mauxtag *t;
263
264         if ((m->m_flags & M_PKTHDR) == 0)
265                 return NULL;
266
267         n = m_aux_find(m, af, type);
268         if (n)
269                 return n;
270
271         MGET(n, M_DONTWAIT, m->m_type);
272         if (n == NULL)
273                 return NULL;
274
275         t = mtod(n, struct mauxtag *);
276         t->af = af;
277         t->type = type;
278         n->m_data += sizeof(struct mauxtag);
279         n->m_len = 0;
280         n->m_next = m->m_pkthdr.aux;
281         m->m_pkthdr.aux = n;
282         return n;
283 }
284
285 struct mbuf *
286 m_aux_find(m, af, type)
287         struct mbuf *m;
288         int af, type;
289 {
290         struct mbuf *n;
291         struct mauxtag *t;
292
293         if ((m->m_flags & M_PKTHDR) == 0)
294                 return NULL;
295
296         for (n = m->m_pkthdr.aux; n; n = n->m_next) {
297                 t = (struct mauxtag *)n->m_dat;
298                 if (t->af == af && t->type == type)
299                         return n;
300         }
301         return NULL;
302 }
303
304 void
305 m_aux_delete(m, victim)
306         struct mbuf *m;
307         struct mbuf *victim;
308 {
309         struct mbuf *n, *prev, *next;
310         struct mauxtag *t;
311
312         if ((m->m_flags & M_PKTHDR) == 0)
313                 return;
314
315         prev = NULL;
316         n = m->m_pkthdr.aux;
317         while (n) {
318                 t = (struct mauxtag *)n->m_dat;
319                 next = n->m_next;
320                 if (n == victim) {
321                         if (prev)
322                                 prev->m_next = n->m_next;
323                         else
324                                 m->m_pkthdr.aux = n->m_next;
325                         n->m_next = NULL;
326                         m_free(n);
327                 } else
328                         prev = n;
329                 n = next;
330         }
331 }