FreeBSD/Linux Kernel Cross Reference
sys/kern/uipc_mbuf2.c
1 /* $FreeBSD$ */
2 /* $KAME: uipc_mbuf2.c,v 1.31 2001/11/28 11:08:53 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/kernel.h>
74 #include <sys/proc.h>
75 #include <sys/malloc.h>
76 #include <sys/mbuf.h>
77
78 #define M_SHAREDCLUSTER(m) \
79 (((m)->m_flags & M_EXT) != 0 && \
80 ((m)->m_ext.ext_free || mclrefcnt[mtocl((m)->m_ext.ext_buf)] > 1))
81
82 MALLOC_DEFINE(M_PACKET_TAGS, "tag", "packet-attached information");
83
84 /* can't call it m_dup(), as freebsd[34] uses m_dup() with different arg */
85 static struct mbuf *m_dup1 __P((struct mbuf *, int, int, int));
86
87 /*
88 * ensure that [off, off + len) is contiguous on the mbuf chain "m".
89 * packet chain before "off" is kept untouched.
90 * if offp == NULL, the target will start at <retval, 0> on resulting chain.
91 * if offp != NULL, the target will start at <retval, *offp> on resulting chain.
92 *
93 * on error return (NULL return value), original "m" will be freed.
94 *
95 * XXX M_TRAILINGSPACE/M_LEADINGSPACE on shared cluster (sharedcluster)
96 */
97 struct mbuf *
98 m_pulldown(m, off, len, offp)
99 struct mbuf *m;
100 int off, len;
101 int *offp;
102 {
103 struct mbuf *n, *o;
104 int hlen, tlen, olen;
105 int sharedcluster;
106
107 /* check invalid arguments. */
108 if (m == NULL)
109 panic("m == NULL in m_pulldown()");
110 if (len > MCLBYTES) {
111 m_freem(m);
112 return NULL; /* impossible */
113 }
114
115 #ifdef PULLDOWN_DEBUG
116 {
117 struct mbuf *t;
118 printf("before:");
119 for (t = m; t; t = t->m_next)
120 printf(" %d", t->m_len);
121 printf("\n");
122 }
123 #endif
124 n = m;
125 while (n != NULL && off > 0) {
126 if (n->m_len > off)
127 break;
128 off -= n->m_len;
129 n = n->m_next;
130 }
131 /* be sure to point non-empty mbuf */
132 while (n != NULL && n->m_len == 0)
133 n = n->m_next;
134 if (!n) {
135 m_freem(m);
136 return NULL; /* mbuf chain too short */
137 }
138
139 sharedcluster = M_SHAREDCLUSTER(n);
140
141 /*
142 * the target data is on <n, off>.
143 * if we got enough data on the mbuf "n", we're done.
144 */
145 if ((off == 0 || offp) && len <= n->m_len - off && !sharedcluster)
146 goto ok;
147
148 /*
149 * when len <= n->m_len - off and off != 0, it is a special case.
150 * len bytes from <n, off> sits in single mbuf, but the caller does
151 * not like the starting position (off).
152 * chop the current mbuf into two pieces, set off to 0.
153 */
154 if (len <= n->m_len - off) {
155 o = m_dup1(n, off, n->m_len - off, M_DONTWAIT);
156 if (o == NULL) {
157 m_freem(m);
158 return NULL; /* ENOBUFS */
159 }
160 n->m_len = off;
161 o->m_next = n->m_next;
162 n->m_next = o;
163 n = n->m_next;
164 off = 0;
165 goto ok;
166 }
167
168 /*
169 * we need to take hlen from <n, off> and tlen from <n->m_next, 0>,
170 * and construct contiguous mbuf with m_len == len.
171 * note that hlen + tlen == len, and tlen > 0.
172 */
173 hlen = n->m_len - off;
174 tlen = len - hlen;
175
176 /*
177 * ensure that we have enough trailing data on mbuf chain.
178 * if not, we can do nothing about the chain.
179 */
180 olen = 0;
181 for (o = n->m_next; o != NULL; o = o->m_next)
182 olen += o->m_len;
183 if (hlen + olen < len) {
184 m_freem(m);
185 return NULL; /* mbuf chain too short */
186 }
187
188 /*
189 * easy cases first.
190 * we need to use m_copydata() to get data from <n->m_next, 0>.
191 */
192 if ((off == 0 || offp) && M_TRAILINGSPACE(n) >= tlen &&
193 !sharedcluster) {
194 m_copydata(n->m_next, 0, tlen, mtod(n, caddr_t) + n->m_len);
195 n->m_len += tlen;
196 m_adj(n->m_next, tlen);
197 goto ok;
198 }
199 if ((off == 0 || offp) && M_LEADINGSPACE(n->m_next) >= hlen &&
200 !sharedcluster) {
201 n->m_next->m_data -= hlen;
202 n->m_next->m_len += hlen;
203 bcopy(mtod(n, caddr_t) + off, mtod(n->m_next, caddr_t), hlen);
204 n->m_len -= hlen;
205 n = n->m_next;
206 off = 0;
207 goto ok;
208 }
209
210 /*
211 * now, we need to do the hard way. don't m_copy as there's no room
212 * on both end.
213 */
214 MGET(o, M_DONTWAIT, m->m_type);
215 if (o && len > MLEN) {
216 MCLGET(o, M_DONTWAIT);
217 if ((o->m_flags & M_EXT) == 0) {
218 m_free(o);
219 o = NULL;
220 }
221 }
222 if (!o) {
223 m_freem(m);
224 return NULL; /* ENOBUFS */
225 }
226 /* get hlen from <n, off> into <o, 0> */
227 o->m_len = hlen;
228 bcopy(mtod(n, caddr_t) + off, mtod(o, caddr_t), hlen);
229 n->m_len -= hlen;
230 /* get tlen from <n->m_next, 0> into <o, hlen> */
231 m_copydata(n->m_next, 0, tlen, mtod(o, caddr_t) + o->m_len);
232 o->m_len += tlen;
233 m_adj(n->m_next, tlen);
234 o->m_next = n->m_next;
235 n->m_next = o;
236 n = o;
237 off = 0;
238
239 ok:
240 #ifdef PULLDOWN_DEBUG
241 {
242 struct mbuf *t;
243 printf("after:");
244 for (t = m; t; t = t->m_next)
245 printf("%c%d", t == n ? '*' : ' ', t->m_len);
246 printf(" (off=%d)\n", off);
247 }
248 #endif
249 if (offp)
250 *offp = off;
251 return n;
252 }
253
254 static struct mbuf *
255 m_dup1(m, off, len, wait)
256 struct mbuf *m;
257 int off;
258 int len;
259 int wait;
260 {
261 struct mbuf *n;
262 int l;
263 int copyhdr;
264
265 if (len > MCLBYTES)
266 return NULL;
267 if (off == 0 && (m->m_flags & M_PKTHDR) != 0) {
268 copyhdr = 1;
269 MGETHDR(n, wait, m->m_type);
270 l = MHLEN;
271 } else {
272 copyhdr = 0;
273 MGET(n, wait, m->m_type);
274 l = MLEN;
275 }
276 if (n && len > l) {
277 MCLGET(n, wait);
278 if ((n->m_flags & M_EXT) == 0) {
279 m_free(n);
280 n = NULL;
281 }
282 }
283 if (!n)
284 return NULL;
285
286 if (copyhdr && !m_dup_pkthdr(n, m, wait)) {
287 m_free(n);
288 return NULL;
289 }
290 m_copydata(m, off, len, mtod(n, caddr_t));
291 return n;
292 }
293
294 /* Get a packet tag structure along with specified data following. */
295 struct m_tag *
296 m_tag_alloc(u_int32_t cookie, int type, int len, int wait)
297 {
298 struct m_tag *t;
299
300 if (len < 0)
301 return NULL;
302 t = malloc(len + sizeof(struct m_tag), M_PACKET_TAGS, wait);
303 if (t == NULL)
304 return NULL;
305 t->m_tag_id = type;
306 t->m_tag_len = len;
307 t->m_tag_cookie = cookie;
308 return t;
309 }
310
311
312 /* Free a packet tag. */
313 void
314 m_tag_free(struct m_tag *t)
315 {
316 free(t, M_PACKET_TAGS);
317 }
318
319 /* Prepend a packet tag. */
320 void
321 m_tag_prepend(struct mbuf *m, struct m_tag *t)
322 {
323 KASSERT(m && t, ("m_tag_prepend: null argument, m %p t %p", m, t));
324 SLIST_INSERT_HEAD(&m->m_pkthdr.tags, t, m_tag_link);
325 }
326
327 /* Unlink a packet tag. */
328 void
329 m_tag_unlink(struct mbuf *m, struct m_tag *t)
330 {
331 KASSERT(m && t, ("m_tag_unlink: null argument, m %p t %p", m, t));
332 SLIST_REMOVE(&m->m_pkthdr.tags, t, m_tag, m_tag_link);
333 }
334
335 /* Unlink and free a packet tag. */
336 void
337 m_tag_delete(struct mbuf *m, struct m_tag *t)
338 {
339 KASSERT(m && t, ("m_tag_delete: null argument, m %p t %p", m, t));
340 m_tag_unlink(m, t);
341 m_tag_free(t);
342 }
343
344 /* Unlink and free a packet tag chain, starting from given tag. */
345 void
346 m_tag_delete_chain(struct mbuf *m, struct m_tag *t)
347 {
348 struct m_tag *p, *q;
349
350 KASSERT(m, ("m_tag_delete_chain: null mbuf"));
351 if (t != NULL)
352 p = t;
353 else
354 p = SLIST_FIRST(&m->m_pkthdr.tags);
355 if (p == NULL)
356 return;
357 while ((q = SLIST_NEXT(p, m_tag_link)) != NULL)
358 m_tag_delete(m, q);
359 m_tag_delete(m, p);
360 }
361
362 /* Find a tag, starting from a given position. */
363 struct m_tag *
364 m_tag_locate(struct mbuf *m, u_int32_t cookie, int type, struct m_tag *t)
365 {
366 struct m_tag *p;
367
368 KASSERT(m, ("m_tag_find: null mbuf"));
369 if (t == NULL)
370 p = SLIST_FIRST(&m->m_pkthdr.tags);
371 else
372 p = SLIST_NEXT(t, m_tag_link);
373 while (p != NULL) {
374 if (p->m_tag_cookie == cookie && p->m_tag_id == type)
375 return p;
376 p = SLIST_NEXT(p, m_tag_link);
377 }
378 return NULL;
379 }
380
381 /* Copy a single tag. */
382 struct m_tag *
383 m_tag_copy(struct m_tag *t, int how)
384 {
385 struct m_tag *p;
386
387 KASSERT(t, ("m_tag_copy: null tag"));
388 p = m_tag_alloc(t->m_tag_cookie, t->m_tag_id, t->m_tag_len, how);
389 if (p == NULL)
390 return (NULL);
391 bcopy(t + 1, p + 1, t->m_tag_len); /* Copy the data */
392 return p;
393 }
394
395 /*
396 * Copy two tag chains. The destination mbuf (to) loses any attached
397 * tags even if the operation fails. This should not be a problem, as
398 * m_tag_copy_chain() is typically called with a newly-allocated
399 * destination mbuf.
400 */
401 int
402 m_tag_copy_chain(struct mbuf *to, struct mbuf *from, int how)
403 {
404 struct m_tag *p, *t, *tprev = NULL;
405
406 KASSERT(to && from,
407 ("m_tag_copy: null argument, to %p from %p", to, from));
408 m_tag_delete_chain(to, NULL);
409 SLIST_FOREACH(p, &from->m_pkthdr.tags, m_tag_link) {
410 t = m_tag_copy(p, how);
411 if (t == NULL) {
412 m_tag_delete_chain(to, NULL);
413 return 0;
414 }
415 if (tprev == NULL)
416 SLIST_INSERT_HEAD(&to->m_pkthdr.tags, t, m_tag_link);
417 else {
418 SLIST_INSERT_AFTER(tprev, t, m_tag_link);
419 tprev = t;
420 }
421 }
422 return 1;
423 }
424
425 /* Initialize tags on an mbuf. */
426 void
427 m_tag_init(struct mbuf *m)
428 {
429 SLIST_INIT(&m->m_pkthdr.tags);
430 }
431
432 /* Get first tag in chain. */
433 struct m_tag *
434 m_tag_first(struct mbuf *m)
435 {
436 return SLIST_FIRST(&m->m_pkthdr.tags);
437 }
438
439 /* Get next tag in chain. */
440 struct m_tag *
441 m_tag_next(struct mbuf *m, struct m_tag *t)
442 {
443 return SLIST_NEXT(t, m_tag_link);
444 }
Cache object: 3650ac71fbdcc6375c3e1c8415f35bdc
|