]>
git.saurik.com Git - apple/xnu.git/blob - bsd/kern/uipc_mbuf2.c
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
23 /* $NetBSD: uipc_mbuf.c,v 1.40 1999/04/01 00:23:25 thorpej Exp $ */
26 * Copyright (C) 1999 WIDE Project.
27 * All rights reserved.
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
32 * 1. Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in the
36 * documentation and/or other materials provided with the distribution.
37 * 3. Neither the name of the project nor the names of its contributors
38 * may be used to endorse or promote products derived from this software
39 * without specific prior written permission.
41 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 * Copyright (c) 1982, 1986, 1988, 1991, 1993
56 * The Regents of the University of California. All rights reserved.
58 * Redistribution and use in source and binary forms, with or without
59 * modification, are permitted provided that the following conditions
61 * 1. Redistributions of source code must retain the above copyright
62 * notice, this list of conditions and the following disclaimer.
63 * 2. Redistributions in binary form must reproduce the above copyright
64 * notice, this list of conditions and the following disclaimer in the
65 * documentation and/or other materials provided with the distribution.
66 * 3. All advertising materials mentioning features or use of this software
67 * must display the following acknowledgement:
68 * This product includes software developed by the University of
69 * California, Berkeley and its contributors.
70 * 4. Neither the name of the University nor the names of its contributors
71 * may be used to endorse or promote products derived from this software
72 * without specific prior written permission.
74 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
75 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
76 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
77 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
78 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
79 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
80 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
81 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
82 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
83 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
86 * @(#)uipc_mbuf.c 8.4 (Berkeley) 2/14/95
90 /*#define PULLDOWN_DEBUG*/
92 #include <sys/param.h>
93 #include <sys/systm.h>
94 #include <sys/proc_internal.h>
95 #include <sys/malloc.h>
97 #if defined(PULLDOWN_STAT) && defined(INET6)
98 #include <netinet/in.h>
99 #include <netinet/ip6.h>
100 #include <netinet6/ip6_var.h>
104 * ensure that [off, off + len) is contiguous on the mbuf chain "m".
105 * packet chain before "off" is kept untouched.
106 * if offp == NULL, the target will start at <retval, 0> on resulting chain.
107 * if offp != NULL, the target will start at <retval, *offp> on resulting chain.
109 * on error return (NULL return value), original "m" will be freed.
111 * XXX M_TRAILINGSPACE/M_LEADINGSPACE on shared cluster (sharedcluster)
114 m_pulldown(m
, off
, len
, offp
)
120 int hlen
, tlen
, olen
;
122 #if defined(PULLDOWN_STAT) && defined(INET6)
123 static struct mbuf
*prev
= NULL
;
124 int prevlen
= 0, prevmlen
= 0;
127 /* check invalid arguments. */
129 panic("m == NULL in m_pulldown()");
130 if (len
> MCLBYTES
) {
132 return NULL
; /* impossible */
135 #if defined(PULLDOWN_STAT) && defined(INET6)
136 ip6stat
.ip6s_pulldown
++;
139 #if defined(PULLDOWN_STAT) && defined(INET6)
140 /* statistics for m_pullup */
141 ip6stat
.ip6s_pullup
++;
142 if (off
+ len
> MHLEN
)
143 ip6stat
.ip6s_pullup_fail
++;
147 dlen
= (prev
== m
) ? prevlen
: m
->m_len
;
148 mlen
= (prev
== m
) ? prevmlen
: m
->m_len
+ M_TRAILINGSPACE(m
);
150 if (dlen
>= off
+ len
)
151 ip6stat
.ip6s_pullup
--; /* call will not be made! */
152 else if ((m
->m_flags
& M_EXT
) != 0) {
153 ip6stat
.ip6s_pullup_alloc
++;
154 ip6stat
.ip6s_pullup_copy
++;
156 if (mlen
>= off
+ len
)
157 ip6stat
.ip6s_pullup_copy
++;
159 ip6stat
.ip6s_pullup_alloc
++;
160 ip6stat
.ip6s_pullup_copy
++;
168 /* statistics for m_pullup2 */
169 ip6stat
.ip6s_pullup2
++;
170 if (off
+ len
> MCLBYTES
)
171 ip6stat
.ip6s_pullup2_fail
++;
175 dlen
= (prev
== m
) ? prevlen
: m
->m_len
;
176 mlen
= (prev
== m
) ? prevmlen
: m
->m_len
+ M_TRAILINGSPACE(m
);
180 if (dlen
>= off
+ len
)
181 ip6stat
.ip6s_pullup2
--; /* call will not be made! */
182 else if ((m
->m_flags
& M_EXT
) != 0) {
183 ip6stat
.ip6s_pullup2_alloc
++;
184 ip6stat
.ip6s_pullup2_copy
++;
185 prevmlen
= (off
+ len
> MHLEN
) ? MCLBYTES
: MHLEN
;
187 if (mlen
>= off
+ len
)
188 ip6stat
.ip6s_pullup2_copy
++;
190 ip6stat
.ip6s_pullup2_alloc
++;
191 ip6stat
.ip6s_pullup2_copy
++;
192 prevmlen
= (off
+ len
> MHLEN
) ? MCLBYTES
201 #ifdef PULLDOWN_DEBUG
205 for (t
= m
; t
; t
= t
->m_next
)
206 printf(" %d", t
->m_len
);
211 while (n
!= NULL
&& off
> 0) {
217 /* be sure to point non-empty mbuf */
218 while (n
!= NULL
&& n
->m_len
== 0)
222 return NULL
; /* mbuf chain too short */
226 * the target data is on <n, off>.
227 * if we got enough data on the mbuf "n", we're done.
229 if ((off
== 0 || offp
) && len
<= n
->m_len
- off
)
232 #if defined(PULLDOWN_STAT) && defined(INET6)
233 ip6stat
.ip6s_pulldown_copy
++;
237 * when len < n->m_len - off and off != 0, it is a special case.
238 * len bytes from <n, off> sits in single mbuf, but the caller does
239 * not like the starting position (off).
240 * chop the current mbuf into two pieces, set off to 0.
242 if (len
< n
->m_len
- off
) {
243 o
= m_copym(n
, off
, n
->m_len
- off
, M_DONTWAIT
);
246 return NULL
; /* ENOBUFS */
249 o
->m_next
= n
->m_next
;
257 * we need to take hlen from <n, off> and tlen from <n->m_next, 0>,
258 * and construct contiguous mbuf with m_len == len.
259 * note that hlen + tlen == len, and tlen > 0.
261 hlen
= n
->m_len
- off
;
265 * ensure that we have enough trailing data on mbuf chain.
266 * if not, we can do nothing about the chain.
269 for (o
= n
->m_next
; o
!= NULL
; o
= o
->m_next
)
271 if (hlen
+ olen
< len
) {
273 return NULL
; /* mbuf chain too short */
278 * we need to use m_copydata() to get data from <n->m_next, 0>.
280 if ((n
->m_flags
& M_EXT
) == 0)
283 if (n
->m_ext
.ext_free
)
285 else if (m_mclhasreference(n
))
290 if ((off
== 0 || offp
) && M_TRAILINGSPACE(n
) >= tlen
292 m_copydata(n
->m_next
, 0, tlen
, mtod(n
, caddr_t
) + n
->m_len
);
294 m_adj(n
->m_next
, tlen
);
297 if ((off
== 0 || offp
) && M_LEADINGSPACE(n
->m_next
) >= hlen
299 n
->m_next
->m_data
-= hlen
;
300 n
->m_next
->m_len
+= hlen
;
301 bcopy(mtod(n
, caddr_t
) + off
, mtod(n
->m_next
, caddr_t
), hlen
);
309 * now, we need to do the hard way. don't m_copy as there's no room
312 #if defined(PULLDOWN_STAT) && defined(INET6)
313 ip6stat
.ip6s_pulldown_alloc
++;
315 MGET(o
, M_DONTWAIT
, m
->m_type
);
318 return NULL
; /* ENOBUFS */
320 if (len
> MHLEN
) { /* use MHLEN just for safety */
321 MCLGET(o
, M_DONTWAIT
);
322 if ((o
->m_flags
& M_EXT
) == 0) {
325 return NULL
; /* ENOBUFS */
328 /* get hlen from <n, off> into <o, 0> */
330 bcopy(mtod(n
, caddr_t
) + off
, mtod(o
, caddr_t
), hlen
);
332 /* get tlen from <n->m_next, 0> into <o, hlen> */
333 m_copydata(n
->m_next
, 0, tlen
, mtod(o
, caddr_t
) + o
->m_len
);
335 m_adj(n
->m_next
, tlen
);
336 o
->m_next
= n
->m_next
;
342 #ifdef PULLDOWN_DEBUG
346 for (t
= m
; t
; t
= t
->m_next
)
347 printf("%c%d", t
== n
? '*' : ' ', t
->m_len
);
348 printf(" (off=%d)\n", off
);
357 * pkthdr.aux chain manipulation.
358 * we don't allow clusters at this moment.
361 m_aux_add(m
, af
, type
)
368 if ((m
->m_flags
& M_PKTHDR
) == 0)
371 n
= m_aux_find(m
, af
, type
);
375 MGET(n
, M_DONTWAIT
, m
->m_type
);
379 t
= mtod(n
, struct mauxtag
*);
382 n
->m_data
+= sizeof(struct mauxtag
);
384 n
->m_next
= m
->m_pkthdr
.aux
;
390 m_aux_find(m
, af
, type
)
397 if ((m
->m_flags
& M_PKTHDR
) == 0)
400 for (n
= m
->m_pkthdr
.aux
; n
; n
= n
->m_next
) {
401 t
= (struct mauxtag
*)n
->m_dat
;
402 if (t
->af
== af
&& t
->type
== type
)
409 m_aux_delete(m
, victim
)
413 struct mbuf
*n
, *prev
, *next
;
416 if ((m
->m_flags
& M_PKTHDR
) == 0)
422 t
= (struct mauxtag
*)n
->m_dat
;
426 prev
->m_next
= n
->m_next
;
428 m
->m_pkthdr
.aux
= n
->m_next
;
437 /* Get a packet tag structure along with specified data following. */
439 m_tag_alloc(u_int32_t id
, u_int16_t type
, int len
, int wait
)
446 t
= malloc(len
+ sizeof(struct m_tag
), M_PACKET_TAGS
, wait
);
448 /*MALLOC(t, struct m_tag *, len + sizeof(struct m_tag), M_TEMP, M_WAITOK);*/
449 if (len
+ sizeof(struct m_tag
) <= MLEN
) {
450 struct mbuf
*m
= m_get(wait
, MT_TAG
);
453 t
= (struct m_tag
*) m
->m_dat
;
454 } else if (len
+ sizeof(struct m_tag
) <= MCLBYTES
) {
455 MCLALLOC((caddr_t
)t
, wait
);
461 t
->m_tag_type
= type
;
468 /* Free a packet tag. */
470 m_tag_free(struct m_tag
*t
)
473 free(t
, M_PACKET_TAGS
);
475 /* FREE(t, M_TEMP); */
478 if (t
->m_tag_len
<= MLEN
) {
479 struct mbuf
* m
= m_dtom(t
);
487 /* Prepend a packet tag. */
489 m_tag_prepend(struct mbuf
*m
, struct m_tag
*t
)
491 KASSERT(m
&& t
, ("m_tag_prepend: null argument, m %p t %p", m
, t
));
492 SLIST_INSERT_HEAD(&m
->m_pkthdr
.tags
, t
, m_tag_link
);
495 /* Unlink a packet tag. */
497 m_tag_unlink(struct mbuf
*m
, struct m_tag
*t
)
499 KASSERT(m
&& t
, ("m_tag_unlink: null argument, m %p t %p", m
, t
));
500 SLIST_REMOVE(&m
->m_pkthdr
.tags
, t
, m_tag
, m_tag_link
);
503 /* Unlink and free a packet tag. */
505 m_tag_delete(struct mbuf
*m
, struct m_tag
*t
)
507 KASSERT(m
&& t
, ("m_tag_delete: null argument, m %p t %p", m
, t
));
512 /* Unlink and free a packet tag chain, starting from given tag. */
514 m_tag_delete_chain(struct mbuf
*m
, struct m_tag
*t
)
518 KASSERT(m
, ("m_tag_delete_chain: null mbuf"));
522 p
= SLIST_FIRST(&m
->m_pkthdr
.tags
);
525 while ((q
= SLIST_NEXT(p
, m_tag_link
)) != NULL
)
530 /* Find a tag, starting from a given position. */
532 m_tag_locate(struct mbuf
*m
, u_int32_t id
, u_int16_t type
, struct m_tag
*t
)
536 KASSERT(m
, ("m_tag_find: null mbuf"));
538 p
= SLIST_FIRST(&m
->m_pkthdr
.tags
);
540 p
= SLIST_NEXT(t
, m_tag_link
);
542 if (p
->m_tag_id
== id
&& p
->m_tag_type
== type
)
544 p
= SLIST_NEXT(p
, m_tag_link
);
549 /* Copy a single tag. */
551 m_tag_copy(struct m_tag
*t
, int how
)
555 KASSERT(t
, ("m_tag_copy: null tag"));
556 p
= m_tag_alloc(t
->m_tag_id
, t
->m_tag_type
, t
->m_tag_len
, how
);
559 bcopy(t
+ 1, p
+ 1, t
->m_tag_len
); /* Copy the data */
564 * Copy two tag chains. The destination mbuf (to) loses any attached
565 * tags even if the operation fails. This should not be a problem, as
566 * m_tag_copy_chain() is typically called with a newly-allocated
570 m_tag_copy_chain(struct mbuf
*to
, struct mbuf
*from
, int how
)
572 struct m_tag
*p
, *t
, *tprev
= NULL
;
575 ("m_tag_copy: null argument, to %p from %p", to
, from
));
576 m_tag_delete_chain(to
, NULL
);
577 SLIST_FOREACH(p
, &from
->m_pkthdr
.tags
, m_tag_link
) {
578 t
= m_tag_copy(p
, how
);
580 m_tag_delete_chain(to
, NULL
);
584 SLIST_INSERT_HEAD(&to
->m_pkthdr
.tags
, t
, m_tag_link
);
586 SLIST_INSERT_AFTER(tprev
, t
, m_tag_link
);
593 /* Initialize tags on an mbuf. */
595 m_tag_init(struct mbuf
*m
)
597 SLIST_INIT(&m
->m_pkthdr
.tags
);
600 /* Get first tag in chain. */
602 m_tag_first(struct mbuf
*m
)
604 return SLIST_FIRST(&m
->m_pkthdr
.tags
);
607 /* Get next tag in chain. */
609 m_tag_next(__unused
struct mbuf
*m
, struct m_tag
*t
)
611 return SLIST_NEXT(t
, m_tag_link
);