]>
git.saurik.com Git - apple/xnu.git/blob - bsd/kern/uipc_mbuf2.c
649bc4db5eea857d6deae271d90c6e4b56b82655
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
;
442 /* Get a packet tag structure along with specified data following. */
444 m_tag_alloc(u_int32_t id
, u_int16_t type
, int len
, int wait
)
451 t
= malloc(len
+ sizeof(struct m_tag
), M_PACKET_TAGS
, wait
);
453 /*MALLOC(t, struct m_tag *, len + sizeof(struct m_tag), M_TEMP, M_WAITOK);*/
454 if (len
+ sizeof(struct m_tag
) <= MLEN
) {
455 struct mbuf
*m
= m_get(wait
, MT_TAG
);
458 t
= (struct m_tag
*) m
->m_dat
;
459 } else if (len
+ sizeof(struct m_tag
) <= MCLBYTES
) {
460 MCLALLOC((caddr_t
)t
, wait
);
466 t
->m_tag_type
= type
;
473 /* Free a packet tag. */
475 m_tag_free(struct m_tag
*t
)
478 free(t
, M_PACKET_TAGS
);
480 /* FREE(t, M_TEMP); */
483 if (t
->m_tag_len
<= MLEN
) {
484 struct mbuf
* m
= m_dtom(t
);
492 /* Prepend a packet tag. */
494 m_tag_prepend(struct mbuf
*m
, struct m_tag
*t
)
496 KASSERT(m
&& t
, ("m_tag_prepend: null argument, m %p t %p", m
, t
));
497 SLIST_INSERT_HEAD(&m
->m_pkthdr
.tags
, t
, m_tag_link
);
500 /* Unlink a packet tag. */
502 m_tag_unlink(struct mbuf
*m
, struct m_tag
*t
)
504 KASSERT(m
&& t
, ("m_tag_unlink: null argument, m %p t %p", m
, t
));
505 SLIST_REMOVE(&m
->m_pkthdr
.tags
, t
, m_tag
, m_tag_link
);
508 /* Unlink and free a packet tag. */
510 m_tag_delete(struct mbuf
*m
, struct m_tag
*t
)
512 KASSERT(m
&& t
, ("m_tag_delete: null argument, m %p t %p", m
, t
));
517 /* Unlink and free a packet tag chain, starting from given tag. */
519 m_tag_delete_chain(struct mbuf
*m
, struct m_tag
*t
)
523 KASSERT(m
, ("m_tag_delete_chain: null mbuf"));
527 p
= SLIST_FIRST(&m
->m_pkthdr
.tags
);
530 while ((q
= SLIST_NEXT(p
, m_tag_link
)) != NULL
)
535 /* Find a tag, starting from a given position. */
537 m_tag_locate(struct mbuf
*m
, u_int32_t id
, u_int16_t type
, struct m_tag
*t
)
541 KASSERT(m
, ("m_tag_find: null mbuf"));
543 p
= SLIST_FIRST(&m
->m_pkthdr
.tags
);
545 p
= SLIST_NEXT(t
, m_tag_link
);
547 if (p
->m_tag_id
== id
&& p
->m_tag_type
== type
)
549 p
= SLIST_NEXT(p
, m_tag_link
);
554 /* Copy a single tag. */
556 m_tag_copy(struct m_tag
*t
, int how
)
560 KASSERT(t
, ("m_tag_copy: null tag"));
561 p
= m_tag_alloc(t
->m_tag_id
, t
->m_tag_type
, t
->m_tag_len
, how
);
564 bcopy(t
+ 1, p
+ 1, t
->m_tag_len
); /* Copy the data */
569 * Copy two tag chains. The destination mbuf (to) loses any attached
570 * tags even if the operation fails. This should not be a problem, as
571 * m_tag_copy_chain() is typically called with a newly-allocated
575 m_tag_copy_chain(struct mbuf
*to
, struct mbuf
*from
, int how
)
577 struct m_tag
*p
, *t
, *tprev
= NULL
;
580 ("m_tag_copy: null argument, to %p from %p", to
, from
));
581 m_tag_delete_chain(to
, NULL
);
582 SLIST_FOREACH(p
, &from
->m_pkthdr
.tags
, m_tag_link
) {
583 t
= m_tag_copy(p
, how
);
585 m_tag_delete_chain(to
, NULL
);
589 SLIST_INSERT_HEAD(&to
->m_pkthdr
.tags
, t
, m_tag_link
);
591 SLIST_INSERT_AFTER(tprev
, t
, m_tag_link
);
598 /* Initialize tags on an mbuf. */
600 m_tag_init(struct mbuf
*m
)
602 SLIST_INIT(&m
->m_pkthdr
.tags
);
605 /* Get first tag in chain. */
607 m_tag_first(struct mbuf
*m
)
609 return SLIST_FIRST(&m
->m_pkthdr
.tags
);
612 /* Get next tag in chain. */
614 m_tag_next(__unused
struct mbuf
*m
, struct m_tag
*t
)
616 return SLIST_NEXT(t
, m_tag_link
);