]>
git.saurik.com Git - apple/xnu.git/blob - bsd/kern/uipc_mbuf2.c
1a4340bb9c8e67201dc8b85871cb3396ba64f261
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_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. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* $NetBSD: uipc_mbuf.c,v 1.40 1999/04/01 00:23:25 thorpej Exp $ */
31 * Copyright (C) 1999 WIDE Project.
32 * All rights reserved.
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
37 * 1. Redistributions of source code must retain the above copyright
38 * notice, this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution.
42 * 3. Neither the name of the project nor the names of its contributors
43 * may be used to endorse or promote products derived from this software
44 * without specific prior written permission.
46 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * Copyright (c) 1982, 1986, 1988, 1991, 1993
61 * The Regents of the University of California. All rights reserved.
63 * Redistribution and use in source and binary forms, with or without
64 * modification, are permitted provided that the following conditions
66 * 1. Redistributions of source code must retain the above copyright
67 * notice, this list of conditions and the following disclaimer.
68 * 2. Redistributions in binary form must reproduce the above copyright
69 * notice, this list of conditions and the following disclaimer in the
70 * documentation and/or other materials provided with the distribution.
71 * 3. All advertising materials mentioning features or use of this software
72 * must display the following acknowledgement:
73 * This product includes software developed by the University of
74 * California, Berkeley and its contributors.
75 * 4. Neither the name of the University nor the names of its contributors
76 * may be used to endorse or promote products derived from this software
77 * without specific prior written permission.
79 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
80 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
81 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
82 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
83 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
84 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
85 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
86 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
87 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
88 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
91 * @(#)uipc_mbuf.c 8.4 (Berkeley) 2/14/95
95 /*#define PULLDOWN_DEBUG*/
97 #include <sys/param.h>
98 #include <sys/systm.h>
99 #include <sys/proc_internal.h>
100 #include <sys/malloc.h>
101 #include <sys/mbuf.h>
102 #if defined(PULLDOWN_STAT) && defined(INET6)
103 #include <netinet/in.h>
104 #include <netinet/ip6.h>
105 #include <netinet6/ip6_var.h>
109 * ensure that [off, off + len) is contiguous on the mbuf chain "m".
110 * packet chain before "off" is kept untouched.
111 * if offp == NULL, the target will start at <retval, 0> on resulting chain.
112 * if offp != NULL, the target will start at <retval, *offp> on resulting chain.
114 * on error return (NULL return value), original "m" will be freed.
116 * XXX M_TRAILINGSPACE/M_LEADINGSPACE on shared cluster (sharedcluster)
119 m_pulldown(m
, off
, len
, offp
)
125 int hlen
, tlen
, olen
;
127 #if defined(PULLDOWN_STAT) && defined(INET6)
128 static struct mbuf
*prev
= NULL
;
129 int prevlen
= 0, prevmlen
= 0;
132 /* check invalid arguments. */
134 panic("m == NULL in m_pulldown()");
135 if (len
> MCLBYTES
) {
137 return NULL
; /* impossible */
140 #if defined(PULLDOWN_STAT) && defined(INET6)
141 ip6stat
.ip6s_pulldown
++;
144 #if defined(PULLDOWN_STAT) && defined(INET6)
145 /* statistics for m_pullup */
146 ip6stat
.ip6s_pullup
++;
147 if (off
+ len
> MHLEN
)
148 ip6stat
.ip6s_pullup_fail
++;
152 dlen
= (prev
== m
) ? prevlen
: m
->m_len
;
153 mlen
= (prev
== m
) ? prevmlen
: m
->m_len
+ M_TRAILINGSPACE(m
);
155 if (dlen
>= off
+ len
)
156 ip6stat
.ip6s_pullup
--; /* call will not be made! */
157 else if ((m
->m_flags
& M_EXT
) != 0) {
158 ip6stat
.ip6s_pullup_alloc
++;
159 ip6stat
.ip6s_pullup_copy
++;
161 if (mlen
>= off
+ len
)
162 ip6stat
.ip6s_pullup_copy
++;
164 ip6stat
.ip6s_pullup_alloc
++;
165 ip6stat
.ip6s_pullup_copy
++;
173 /* statistics for m_pullup2 */
174 ip6stat
.ip6s_pullup2
++;
175 if (off
+ len
> MCLBYTES
)
176 ip6stat
.ip6s_pullup2_fail
++;
180 dlen
= (prev
== m
) ? prevlen
: m
->m_len
;
181 mlen
= (prev
== m
) ? prevmlen
: m
->m_len
+ M_TRAILINGSPACE(m
);
185 if (dlen
>= off
+ len
)
186 ip6stat
.ip6s_pullup2
--; /* call will not be made! */
187 else if ((m
->m_flags
& M_EXT
) != 0) {
188 ip6stat
.ip6s_pullup2_alloc
++;
189 ip6stat
.ip6s_pullup2_copy
++;
190 prevmlen
= (off
+ len
> MHLEN
) ? MCLBYTES
: MHLEN
;
192 if (mlen
>= off
+ len
)
193 ip6stat
.ip6s_pullup2_copy
++;
195 ip6stat
.ip6s_pullup2_alloc
++;
196 ip6stat
.ip6s_pullup2_copy
++;
197 prevmlen
= (off
+ len
> MHLEN
) ? MCLBYTES
206 #ifdef PULLDOWN_DEBUG
210 for (t
= m
; t
; t
= t
->m_next
)
211 printf(" %d", t
->m_len
);
216 while (n
!= NULL
&& off
> 0) {
222 /* be sure to point non-empty mbuf */
223 while (n
!= NULL
&& n
->m_len
== 0)
227 return NULL
; /* mbuf chain too short */
231 * the target data is on <n, off>.
232 * if we got enough data on the mbuf "n", we're done.
234 if ((off
== 0 || offp
) && len
<= n
->m_len
- off
)
237 #if defined(PULLDOWN_STAT) && defined(INET6)
238 ip6stat
.ip6s_pulldown_copy
++;
242 * when len < n->m_len - off and off != 0, it is a special case.
243 * len bytes from <n, off> sits in single mbuf, but the caller does
244 * not like the starting position (off).
245 * chop the current mbuf into two pieces, set off to 0.
247 if (len
< n
->m_len
- off
) {
248 o
= m_copym(n
, off
, n
->m_len
- off
, M_DONTWAIT
);
251 return NULL
; /* ENOBUFS */
254 o
->m_next
= n
->m_next
;
262 * we need to take hlen from <n, off> and tlen from <n->m_next, 0>,
263 * and construct contiguous mbuf with m_len == len.
264 * note that hlen + tlen == len, and tlen > 0.
266 hlen
= n
->m_len
- off
;
270 * ensure that we have enough trailing data on mbuf chain.
271 * if not, we can do nothing about the chain.
274 for (o
= n
->m_next
; o
!= NULL
; o
= o
->m_next
)
276 if (hlen
+ olen
< len
) {
278 return NULL
; /* mbuf chain too short */
283 * we need to use m_copydata() to get data from <n->m_next, 0>.
285 if ((n
->m_flags
& M_EXT
) == 0)
288 if (n
->m_ext
.ext_free
)
290 else if (m_mclhasreference(n
))
295 if ((off
== 0 || offp
) && M_TRAILINGSPACE(n
) >= tlen
297 m_copydata(n
->m_next
, 0, tlen
, mtod(n
, caddr_t
) + n
->m_len
);
299 m_adj(n
->m_next
, tlen
);
302 if ((off
== 0 || offp
) && M_LEADINGSPACE(n
->m_next
) >= hlen
304 n
->m_next
->m_data
-= hlen
;
305 n
->m_next
->m_len
+= hlen
;
306 bcopy(mtod(n
, caddr_t
) + off
, mtod(n
->m_next
, caddr_t
), hlen
);
314 * now, we need to do the hard way. don't m_copy as there's no room
317 #if defined(PULLDOWN_STAT) && defined(INET6)
318 ip6stat
.ip6s_pulldown_alloc
++;
320 MGET(o
, M_DONTWAIT
, m
->m_type
);
323 return NULL
; /* ENOBUFS */
325 if (len
> MHLEN
) { /* use MHLEN just for safety */
326 MCLGET(o
, M_DONTWAIT
);
327 if ((o
->m_flags
& M_EXT
) == 0) {
330 return NULL
; /* ENOBUFS */
333 /* get hlen from <n, off> into <o, 0> */
335 bcopy(mtod(n
, caddr_t
) + off
, mtod(o
, caddr_t
), hlen
);
337 /* get tlen from <n->m_next, 0> into <o, hlen> */
338 m_copydata(n
->m_next
, 0, tlen
, mtod(o
, caddr_t
) + o
->m_len
);
340 m_adj(n
->m_next
, tlen
);
341 o
->m_next
= n
->m_next
;
347 #ifdef PULLDOWN_DEBUG
351 for (t
= m
; t
; t
= t
->m_next
)
352 printf("%c%d", t
== n
? '*' : ' ', t
->m_len
);
353 printf(" (off=%d)\n", off
);
362 * pkthdr.aux chain manipulation.
363 * we don't allow clusters at this moment.
366 m_aux_add(m
, af
, type
)
373 if ((m
->m_flags
& M_PKTHDR
) == 0)
376 n
= m_aux_find(m
, af
, type
);
380 MGET(n
, M_DONTWAIT
, m
->m_type
);
384 t
= mtod(n
, struct mauxtag
*);
387 n
->m_data
+= sizeof(struct mauxtag
);
389 n
->m_next
= m
->m_pkthdr
.aux
;
395 m_aux_find(m
, af
, type
)
402 if ((m
->m_flags
& M_PKTHDR
) == 0)
405 for (n
= m
->m_pkthdr
.aux
; n
; n
= n
->m_next
) {
406 t
= (struct mauxtag
*)n
->m_dat
;
407 if (t
->af
== af
&& t
->type
== type
)
414 m_aux_delete(m
, victim
)
418 struct mbuf
*n
, *prev
, *next
;
421 if ((m
->m_flags
& M_PKTHDR
) == 0)
427 t
= (struct mauxtag
*)n
->m_dat
;
431 prev
->m_next
= n
->m_next
;
433 m
->m_pkthdr
.aux
= n
->m_next
;
443 m_aux_copy(struct mbuf
*to
, struct mbuf
*from
)
446 struct mbuf
*top
= NULL
, **np
= &top
;
448 if (!(to
->m_flags
& M_PKTHDR
) || !(from
->m_flags
& M_PKTHDR
))
451 if ((m
= from
->m_pkthdr
.aux
) == NULL
)
457 MGET(n
, M_DONTWAIT
, m
->m_type
);
463 /* Set length and data offset accordingly */
465 n
->m_data
+= (m
->m_data
- m
->m_dat
);
467 /* Copy databuf (mauxtag + possible aux data) */
468 bcopy(m
->m_dat
, n
->m_dat
, sizeof (m
->m_dat
));
475 if (to
->m_pkthdr
.aux
!= NULL
)
476 m_freem(to
->m_pkthdr
.aux
);
478 to
->m_pkthdr
.aux
= top
;
482 /* Get a packet tag structure along with specified data following. */
484 m_tag_alloc(u_int32_t id
, u_int16_t type
, int len
, int wait
)
491 t
= malloc(len
+ sizeof(struct m_tag
), M_PACKET_TAGS
, wait
);
493 /*MALLOC(t, struct m_tag *, len + sizeof(struct m_tag), M_TEMP, M_WAITOK);*/
494 if (len
+ sizeof(struct m_tag
) <= MLEN
) {
495 struct mbuf
*m
= m_get(wait
, MT_TAG
);
498 t
= (struct m_tag
*) m
->m_dat
;
499 } else if (len
+ sizeof(struct m_tag
) <= MCLBYTES
) {
500 MCLALLOC((caddr_t
)t
, wait
);
506 t
->m_tag_type
= type
;
513 /* Free a packet tag. */
515 m_tag_free(struct m_tag
*t
)
518 free(t
, M_PACKET_TAGS
);
520 /* FREE(t, M_TEMP); */
523 if (t
->m_tag_len
<= MLEN
) {
524 struct mbuf
* m
= m_dtom(t
);
532 /* Prepend a packet tag. */
534 m_tag_prepend(struct mbuf
*m
, struct m_tag
*t
)
536 KASSERT(m
&& t
, ("m_tag_prepend: null argument, m %p t %p", m
, t
));
537 SLIST_INSERT_HEAD(&m
->m_pkthdr
.tags
, t
, m_tag_link
);
540 /* Unlink a packet tag. */
542 m_tag_unlink(struct mbuf
*m
, struct m_tag
*t
)
544 KASSERT(m
&& t
, ("m_tag_unlink: null argument, m %p t %p", m
, t
));
545 SLIST_REMOVE(&m
->m_pkthdr
.tags
, t
, m_tag
, m_tag_link
);
548 /* Unlink and free a packet tag. */
550 m_tag_delete(struct mbuf
*m
, struct m_tag
*t
)
552 KASSERT(m
&& t
, ("m_tag_delete: null argument, m %p t %p", m
, t
));
557 /* Unlink and free a packet tag chain, starting from given tag. */
559 m_tag_delete_chain(struct mbuf
*m
, struct m_tag
*t
)
563 KASSERT(m
, ("m_tag_delete_chain: null mbuf"));
567 p
= SLIST_FIRST(&m
->m_pkthdr
.tags
);
570 while ((q
= SLIST_NEXT(p
, m_tag_link
)) != NULL
)
575 /* Find a tag, starting from a given position. */
577 m_tag_locate(struct mbuf
*m
, u_int32_t id
, u_int16_t type
, struct m_tag
*t
)
581 KASSERT(m
, ("m_tag_find: null mbuf"));
583 p
= SLIST_FIRST(&m
->m_pkthdr
.tags
);
585 p
= SLIST_NEXT(t
, m_tag_link
);
587 if (p
->m_tag_id
== id
&& p
->m_tag_type
== type
)
589 p
= SLIST_NEXT(p
, m_tag_link
);
594 /* Copy a single tag. */
596 m_tag_copy(struct m_tag
*t
, int how
)
600 KASSERT(t
, ("m_tag_copy: null tag"));
601 p
= m_tag_alloc(t
->m_tag_id
, t
->m_tag_type
, t
->m_tag_len
, how
);
604 bcopy(t
+ 1, p
+ 1, t
->m_tag_len
); /* Copy the data */
609 * Copy two tag chains. The destination mbuf (to) loses any attached
610 * tags even if the operation fails. This should not be a problem, as
611 * m_tag_copy_chain() is typically called with a newly-allocated
615 m_tag_copy_chain(struct mbuf
*to
, struct mbuf
*from
, int how
)
617 struct m_tag
*p
, *t
, *tprev
= NULL
;
620 ("m_tag_copy: null argument, to %p from %p", to
, from
));
621 m_tag_delete_chain(to
, NULL
);
622 SLIST_FOREACH(p
, &from
->m_pkthdr
.tags
, m_tag_link
) {
623 t
= m_tag_copy(p
, how
);
625 m_tag_delete_chain(to
, NULL
);
629 SLIST_INSERT_HEAD(&to
->m_pkthdr
.tags
, t
, m_tag_link
);
631 SLIST_INSERT_AFTER(tprev
, t
, m_tag_link
);
638 /* Initialize tags on an mbuf. */
640 m_tag_init(struct mbuf
*m
)
642 SLIST_INIT(&m
->m_pkthdr
.tags
);
645 /* Get first tag in chain. */
647 m_tag_first(struct mbuf
*m
)
649 return SLIST_FIRST(&m
->m_pkthdr
.tags
);
652 /* Get next tag in chain. */
654 m_tag_next(__unused
struct mbuf
*m
, struct m_tag
*t
)
656 return SLIST_NEXT(t
, m_tag_link
);