]>
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 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 /* $NetBSD: uipc_mbuf.c,v 1.40 1999/04/01 00:23:25 thorpej Exp $ */
25 * Copyright (C) 1999 WIDE Project.
26 * All rights reserved.
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
31 * 1. Redistributions of source code must retain the above copyright
32 * notice, this list of conditions and the following disclaimer.
33 * 2. Redistributions in binary form must reproduce the above copyright
34 * notice, this list of conditions and the following disclaimer in the
35 * documentation and/or other materials provided with the distribution.
36 * 3. Neither the name of the project nor the names of its contributors
37 * may be used to endorse or promote products derived from this software
38 * without specific prior written permission.
40 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 * Copyright (c) 1982, 1986, 1988, 1991, 1993
55 * The Regents of the University of California. All rights reserved.
57 * Redistribution and use in source and binary forms, with or without
58 * modification, are permitted provided that the following conditions
60 * 1. Redistributions of source code must retain the above copyright
61 * notice, this list of conditions and the following disclaimer.
62 * 2. Redistributions in binary form must reproduce the above copyright
63 * notice, this list of conditions and the following disclaimer in the
64 * documentation and/or other materials provided with the distribution.
65 * 3. All advertising materials mentioning features or use of this software
66 * must display the following acknowledgement:
67 * This product includes software developed by the University of
68 * California, Berkeley and its contributors.
69 * 4. Neither the name of the University nor the names of its contributors
70 * may be used to endorse or promote products derived from this software
71 * without specific prior written permission.
73 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
74 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
75 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
76 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
77 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
78 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
79 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
80 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
81 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
82 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
85 * @(#)uipc_mbuf.c 8.4 (Berkeley) 2/14/95
89 /*#define PULLDOWN_DEBUG*/
91 #include <sys/param.h>
92 #include <sys/systm.h>
93 #include <sys/proc_internal.h>
94 #include <sys/malloc.h>
96 #if defined(PULLDOWN_STAT) && defined(INET6)
97 #include <netinet/in.h>
98 #include <netinet/ip6.h>
99 #include <netinet6/ip6_var.h>
103 * ensure that [off, off + len) is contiguous on the mbuf chain "m".
104 * packet chain before "off" is kept untouched.
105 * if offp == NULL, the target will start at <retval, 0> on resulting chain.
106 * if offp != NULL, the target will start at <retval, *offp> on resulting chain.
108 * on error return (NULL return value), original "m" will be freed.
110 * XXX M_TRAILINGSPACE/M_LEADINGSPACE on shared cluster (sharedcluster)
113 m_pulldown(m
, off
, len
, offp
)
119 int hlen
, tlen
, olen
;
121 #if defined(PULLDOWN_STAT) && defined(INET6)
122 static struct mbuf
*prev
= NULL
;
123 int prevlen
= 0, prevmlen
= 0;
126 /* check invalid arguments. */
128 panic("m == NULL in m_pulldown()");
129 if (len
> MCLBYTES
) {
131 return NULL
; /* impossible */
134 #if defined(PULLDOWN_STAT) && defined(INET6)
135 ip6stat
.ip6s_pulldown
++;
138 #if defined(PULLDOWN_STAT) && defined(INET6)
139 /* statistics for m_pullup */
140 ip6stat
.ip6s_pullup
++;
141 if (off
+ len
> MHLEN
)
142 ip6stat
.ip6s_pullup_fail
++;
146 dlen
= (prev
== m
) ? prevlen
: m
->m_len
;
147 mlen
= (prev
== m
) ? prevmlen
: m
->m_len
+ M_TRAILINGSPACE(m
);
149 if (dlen
>= off
+ len
)
150 ip6stat
.ip6s_pullup
--; /* call will not be made! */
151 else if ((m
->m_flags
& M_EXT
) != 0) {
152 ip6stat
.ip6s_pullup_alloc
++;
153 ip6stat
.ip6s_pullup_copy
++;
155 if (mlen
>= off
+ len
)
156 ip6stat
.ip6s_pullup_copy
++;
158 ip6stat
.ip6s_pullup_alloc
++;
159 ip6stat
.ip6s_pullup_copy
++;
167 /* statistics for m_pullup2 */
168 ip6stat
.ip6s_pullup2
++;
169 if (off
+ len
> MCLBYTES
)
170 ip6stat
.ip6s_pullup2_fail
++;
174 dlen
= (prev
== m
) ? prevlen
: m
->m_len
;
175 mlen
= (prev
== m
) ? prevmlen
: m
->m_len
+ M_TRAILINGSPACE(m
);
179 if (dlen
>= off
+ len
)
180 ip6stat
.ip6s_pullup2
--; /* call will not be made! */
181 else if ((m
->m_flags
& M_EXT
) != 0) {
182 ip6stat
.ip6s_pullup2_alloc
++;
183 ip6stat
.ip6s_pullup2_copy
++;
184 prevmlen
= (off
+ len
> MHLEN
) ? MCLBYTES
: MHLEN
;
186 if (mlen
>= off
+ len
)
187 ip6stat
.ip6s_pullup2_copy
++;
189 ip6stat
.ip6s_pullup2_alloc
++;
190 ip6stat
.ip6s_pullup2_copy
++;
191 prevmlen
= (off
+ len
> MHLEN
) ? MCLBYTES
200 #ifdef PULLDOWN_DEBUG
204 for (t
= m
; t
; t
= t
->m_next
)
205 printf(" %d", t
->m_len
);
210 while (n
!= NULL
&& off
> 0) {
216 /* be sure to point non-empty mbuf */
217 while (n
!= NULL
&& n
->m_len
== 0)
221 return NULL
; /* mbuf chain too short */
225 * the target data is on <n, off>.
226 * if we got enough data on the mbuf "n", we're done.
228 if ((off
== 0 || offp
) && len
<= n
->m_len
- off
)
231 #if defined(PULLDOWN_STAT) && defined(INET6)
232 ip6stat
.ip6s_pulldown_copy
++;
236 * when len < n->m_len - off and off != 0, it is a special case.
237 * len bytes from <n, off> sits in single mbuf, but the caller does
238 * not like the starting position (off).
239 * chop the current mbuf into two pieces, set off to 0.
241 if (len
< n
->m_len
- off
) {
242 o
= m_copym(n
, off
, n
->m_len
- off
, M_DONTWAIT
);
245 return NULL
; /* ENOBUFS */
248 o
->m_next
= n
->m_next
;
256 * we need to take hlen from <n, off> and tlen from <n->m_next, 0>,
257 * and construct contiguous mbuf with m_len == len.
258 * note that hlen + tlen == len, and tlen > 0.
260 hlen
= n
->m_len
- off
;
264 * ensure that we have enough trailing data on mbuf chain.
265 * if not, we can do nothing about the chain.
268 for (o
= n
->m_next
; o
!= NULL
; o
= o
->m_next
)
270 if (hlen
+ olen
< len
) {
272 return NULL
; /* mbuf chain too short */
277 * we need to use m_copydata() to get data from <n->m_next, 0>.
279 if ((n
->m_flags
& M_EXT
) == 0)
282 if (n
->m_ext
.ext_free
)
284 else if (m_mclhasreference(n
))
289 if ((off
== 0 || offp
) && M_TRAILINGSPACE(n
) >= tlen
291 m_copydata(n
->m_next
, 0, tlen
, mtod(n
, caddr_t
) + n
->m_len
);
293 m_adj(n
->m_next
, tlen
);
296 if ((off
== 0 || offp
) && M_LEADINGSPACE(n
->m_next
) >= hlen
298 n
->m_next
->m_data
-= hlen
;
299 n
->m_next
->m_len
+= hlen
;
300 bcopy(mtod(n
, caddr_t
) + off
, mtod(n
->m_next
, caddr_t
), hlen
);
308 * now, we need to do the hard way. don't m_copy as there's no room
311 #if defined(PULLDOWN_STAT) && defined(INET6)
312 ip6stat
.ip6s_pulldown_alloc
++;
314 MGET(o
, M_DONTWAIT
, m
->m_type
);
317 return NULL
; /* ENOBUFS */
319 if (len
> MHLEN
) { /* use MHLEN just for safety */
320 MCLGET(o
, M_DONTWAIT
);
321 if ((o
->m_flags
& M_EXT
) == 0) {
324 return NULL
; /* ENOBUFS */
327 /* get hlen from <n, off> into <o, 0> */
329 bcopy(mtod(n
, caddr_t
) + off
, mtod(o
, caddr_t
), hlen
);
331 /* get tlen from <n->m_next, 0> into <o, hlen> */
332 m_copydata(n
->m_next
, 0, tlen
, mtod(o
, caddr_t
) + o
->m_len
);
334 m_adj(n
->m_next
, tlen
);
335 o
->m_next
= n
->m_next
;
341 #ifdef PULLDOWN_DEBUG
345 for (t
= m
; t
; t
= t
->m_next
)
346 printf("%c%d", t
== n
? '*' : ' ', t
->m_len
);
347 printf(" (off=%d)\n", off
);
356 * pkthdr.aux chain manipulation.
357 * we don't allow clusters at this moment.
360 m_aux_add(m
, af
, type
)
367 if ((m
->m_flags
& M_PKTHDR
) == 0)
370 n
= m_aux_find(m
, af
, type
);
374 MGET(n
, M_DONTWAIT
, m
->m_type
);
378 t
= mtod(n
, struct mauxtag
*);
381 n
->m_data
+= sizeof(struct mauxtag
);
383 n
->m_next
= m
->m_pkthdr
.aux
;
389 m_aux_find(m
, af
, type
)
396 if ((m
->m_flags
& M_PKTHDR
) == 0)
399 for (n
= m
->m_pkthdr
.aux
; n
; n
= n
->m_next
) {
400 t
= (struct mauxtag
*)n
->m_dat
;
401 if (t
->af
== af
&& t
->type
== type
)
408 m_aux_delete(m
, victim
)
412 struct mbuf
*n
, *prev
, *next
;
415 if ((m
->m_flags
& M_PKTHDR
) == 0)
421 t
= (struct mauxtag
*)n
->m_dat
;
425 prev
->m_next
= n
->m_next
;
427 m
->m_pkthdr
.aux
= n
->m_next
;
436 /* Get a packet tag structure along with specified data following. */
438 m_tag_alloc(u_int32_t id
, u_int16_t type
, int len
, int wait
)
445 t
= malloc(len
+ sizeof(struct m_tag
), M_PACKET_TAGS
, wait
);
447 /*MALLOC(t, struct m_tag *, len + sizeof(struct m_tag), M_TEMP, M_WAITOK);*/
448 if (len
+ sizeof(struct m_tag
) <= MLEN
) {
449 struct mbuf
*m
= m_get(wait
, MT_TAG
);
452 t
= (struct m_tag
*) m
->m_dat
;
453 } else if (len
+ sizeof(struct m_tag
) <= MCLBYTES
) {
454 MCLALLOC((caddr_t
)t
, wait
);
460 t
->m_tag_type
= type
;
467 /* Free a packet tag. */
469 m_tag_free(struct m_tag
*t
)
472 free(t
, M_PACKET_TAGS
);
474 /* FREE(t, M_TEMP); */
477 if (t
->m_tag_len
<= MLEN
) {
478 struct mbuf
* m
= m_dtom(t
);
486 /* Prepend a packet tag. */
488 m_tag_prepend(struct mbuf
*m
, struct m_tag
*t
)
490 KASSERT(m
&& t
, ("m_tag_prepend: null argument, m %p t %p", m
, t
));
491 SLIST_INSERT_HEAD(&m
->m_pkthdr
.tags
, t
, m_tag_link
);
494 /* Unlink a packet tag. */
496 m_tag_unlink(struct mbuf
*m
, struct m_tag
*t
)
498 KASSERT(m
&& t
, ("m_tag_unlink: null argument, m %p t %p", m
, t
));
499 SLIST_REMOVE(&m
->m_pkthdr
.tags
, t
, m_tag
, m_tag_link
);
502 /* Unlink and free a packet tag. */
504 m_tag_delete(struct mbuf
*m
, struct m_tag
*t
)
506 KASSERT(m
&& t
, ("m_tag_delete: null argument, m %p t %p", m
, t
));
511 /* Unlink and free a packet tag chain, starting from given tag. */
513 m_tag_delete_chain(struct mbuf
*m
, struct m_tag
*t
)
517 KASSERT(m
, ("m_tag_delete_chain: null mbuf"));
521 p
= SLIST_FIRST(&m
->m_pkthdr
.tags
);
524 while ((q
= SLIST_NEXT(p
, m_tag_link
)) != NULL
)
529 /* Find a tag, starting from a given position. */
531 m_tag_locate(struct mbuf
*m
, u_int32_t id
, u_int16_t type
, struct m_tag
*t
)
535 KASSERT(m
, ("m_tag_find: null mbuf"));
537 p
= SLIST_FIRST(&m
->m_pkthdr
.tags
);
539 p
= SLIST_NEXT(t
, m_tag_link
);
541 if (p
->m_tag_id
== id
&& p
->m_tag_type
== type
)
543 p
= SLIST_NEXT(p
, m_tag_link
);
548 /* Copy a single tag. */
550 m_tag_copy(struct m_tag
*t
, int how
)
554 KASSERT(t
, ("m_tag_copy: null tag"));
555 p
= m_tag_alloc(t
->m_tag_type
, t
->m_tag_id
, t
->m_tag_len
, how
);
558 bcopy(t
+ 1, p
+ 1, t
->m_tag_len
); /* Copy the data */
563 * Copy two tag chains. The destination mbuf (to) loses any attached
564 * tags even if the operation fails. This should not be a problem, as
565 * m_tag_copy_chain() is typically called with a newly-allocated
569 m_tag_copy_chain(struct mbuf
*to
, struct mbuf
*from
, int how
)
571 struct m_tag
*p
, *t
, *tprev
= NULL
;
574 ("m_tag_copy: null argument, to %p from %p", to
, from
));
575 m_tag_delete_chain(to
, NULL
);
576 SLIST_FOREACH(p
, &from
->m_pkthdr
.tags
, m_tag_link
) {
577 t
= m_tag_copy(p
, how
);
579 m_tag_delete_chain(to
, NULL
);
583 SLIST_INSERT_HEAD(&to
->m_pkthdr
.tags
, t
, m_tag_link
);
585 SLIST_INSERT_AFTER(tprev
, t
, m_tag_link
);
592 /* Initialize tags on an mbuf. */
594 m_tag_init(struct mbuf
*m
)
596 SLIST_INIT(&m
->m_pkthdr
.tags
);
599 /* Get first tag in chain. */
601 m_tag_first(struct mbuf
*m
)
603 return SLIST_FIRST(&m
->m_pkthdr
.tags
);
606 /* Get next tag in chain. */
608 m_tag_next(__unused
struct mbuf
*m
, struct m_tag
*t
)
610 return SLIST_NEXT(t
, m_tag_link
);