1 /* $NetBSD: uipc_mbuf2.c,v 1.27 2009/03/18 17:06:51 cegger Exp $ */
2 /* $KAME: uipc_mbuf2.c,v 1.29 2001/02/14 13:42:10 itojun Exp $ */
5 * Copyright (C) 1999 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * Copyright (c) 1982, 1986, 1988, 1991, 1993
35 * The Regents of the University of California. All rights reserved.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * @(#)uipc_mbuf.c 8.4 (Berkeley) 2/14/95
64 #include <sys/cdefs.h>
65 __KERNEL_RCSID(0, "$NetBSD: uipc_mbuf2.c,v 1.27 2009/03/18 17:06:51 cegger Exp $");
67 #include <sys/param.h>
68 #include <sys/systm.h>
70 #include <sys/malloc.h>
73 MALLOC_DEFINE(M_PACKET_TAGS
, "packet tags", "Packet-attached information");
76 * ensure that [off, off + len) is contiguous on the mbuf chain "m".
77 * packet chain before "off" is kept untouched.
78 * if offp == NULL, the target will start at <retval, 0> on resulting chain.
79 * if offp != NULL, the target will start at <retval, *offp> on resulting chain.
81 * on error return (NULL return value), original "m" will be freed.
83 * XXX M_TRAILINGSPACE/M_LEADINGSPACE on shared cluster (sharedcluster)
86 m_pulldown(struct mbuf
*m
, int off
, int len
, int *offp
)
92 /* check invalid arguments. */
94 panic("m == NULL in m_pulldown()");
97 return NULL
; /* impossible */
101 while (n
!= NULL
&& off
> 0) {
107 /* be sure to point non-empty mbuf */
108 while (n
!= NULL
&& n
->m_len
== 0)
112 return NULL
; /* mbuf chain too short */
115 sharedcluster
= M_READONLY(n
);
118 * the target data is on <n, off>.
119 * if we got enough data on the mbuf "n", we're done.
121 if ((off
== 0 || offp
) && len
<= n
->m_len
- off
&& !sharedcluster
)
125 * when len <= n->m_len - off and off != 0, it is a special case.
126 * len bytes from <n, off> sits in single mbuf, but the caller does
127 * not like the starting position (off).
128 * chop the current mbuf into two pieces, set off to 0.
130 if (len
<= n
->m_len
- off
) {
133 o
= m_dup(n
, off
, n
->m_len
- off
, M_DONTWAIT
);
136 return NULL
; /* ENOBUFS */
138 KASSERT(o
->m_len
>= len
);
139 for (mlast
= o
; mlast
->m_next
!= NULL
; mlast
= mlast
->m_next
)
142 mlast
->m_next
= n
->m_next
;
150 * we need to take hlen from <n, off> and tlen from <n->m_next, 0>,
151 * and construct contiguous mbuf with m_len == len.
152 * note that hlen + tlen == len, and tlen > 0.
154 hlen
= n
->m_len
- off
;
158 * ensure that we have enough trailing data on mbuf chain.
159 * if not, we can do nothing about the chain.
162 for (o
= n
->m_next
; o
!= NULL
; o
= o
->m_next
)
164 if (hlen
+ olen
< len
) {
166 return NULL
; /* mbuf chain too short */
171 * we need to use m_copydata() to get data from <n->m_next, 0>.
173 if ((off
== 0 || offp
) && M_TRAILINGSPACE(n
) >= tlen
&&
175 m_copydata(n
->m_next
, 0, tlen
, mtod(n
, char *) + n
->m_len
);
177 m_adj(n
->m_next
, tlen
);
180 if ((off
== 0 || offp
) && M_LEADINGSPACE(n
->m_next
) >= hlen
&&
181 !sharedcluster
&& n
->m_next
->m_len
>= tlen
) {
182 n
->m_next
->m_data
-= hlen
;
183 n
->m_next
->m_len
+= hlen
;
184 memcpy(mtod(n
->m_next
, void *), mtod(n
, char *) + off
, hlen
);
192 * now, we need to do the hard way. don't m_copy as there's no room
195 MGET(o
, M_DONTWAIT
, m
->m_type
);
196 if (o
&& len
> MLEN
) {
197 MCLGET(o
, M_DONTWAIT
);
198 if ((o
->m_flags
& M_EXT
) == 0) {
205 return NULL
; /* ENOBUFS */
207 /* get hlen from <n, off> into <o, 0> */
209 memcpy(mtod(o
, void *), mtod(n
, char *) + off
, hlen
);
211 /* get tlen from <n->m_next, 0> into <o, hlen> */
212 m_copydata(n
->m_next
, 0, tlen
, mtod(o
, char *) + o
->m_len
);
214 m_adj(n
->m_next
, tlen
);
215 o
->m_next
= n
->m_next
;
227 * FreeBSD 4.6 introduced m_getcl(), which performs `fast' allocation
228 * mbuf clusters from a cache of recently-freed clusters. (If the cache
229 * is empty, new clusters are allocated en-masse).
230 * On NetBSD, for now, implement the `cache' as a function
231 * using normal NetBSD mbuf/cluster allocation macros. Replace this
232 * with fast-cache code, if and when NetBSD implements one.
235 m_getcl(int how
, int type
, int flags
)
239 if ((flags
& M_PKTHDR
) != 0)
240 MGETHDR(mp
, how
, type
);
248 if ((mp
->m_flags
& M_EXT
) != 0)
255 /* Get a packet tag structure along with specified data following. */
257 m_tag_get(int type
, int len
, int wait
)
263 t
= malloc(len
+ sizeof(struct m_tag
), M_PACKET_TAGS
, wait
);
271 /* Free a packet tag. */
273 m_tag_free(struct m_tag
*t
)
276 free(t
, M_PACKET_TAGS
);
279 /* Prepend a packet tag. */
281 m_tag_prepend(struct mbuf
*m
, struct m_tag
*t
)
284 SLIST_INSERT_HEAD(&m
->m_pkthdr
.tags
, t
, m_tag_link
);
287 /* Unlink a packet tag. */
289 m_tag_unlink(struct mbuf
*m
, struct m_tag
*t
)
292 SLIST_REMOVE(&m
->m_pkthdr
.tags
, t
, m_tag
, m_tag_link
);
295 /* Unlink and free a packet tag. */
297 m_tag_delete(struct mbuf
*m
, struct m_tag
*t
)
304 /* Unlink and free a packet tag chain, starting from given tag. */
306 m_tag_delete_chain(struct mbuf
*m
, struct m_tag
*t
)
313 p
= SLIST_FIRST(&m
->m_pkthdr
.tags
);
316 while ((q
= SLIST_NEXT(p
, m_tag_link
)) != NULL
)
322 * Strip off all tags that would normally vanish when
323 * passing through a network interface. Only persistent
324 * tags will exist after this; these are expected to remain
325 * so long as the mbuf chain exists, regardless of the
326 * path the mbufs take.
329 m_tag_delete_nonpersistent(struct mbuf
*m
)
331 /* NetBSD has no persistent tags yet, so just delete all tags. */
332 m_tag_delete_chain(m
, NULL
);
336 /* Find a tag, starting from a given position. */
338 m_tag_find(struct mbuf
*m
, int type
, struct m_tag
*t
)
343 p
= SLIST_FIRST(&m
->m_pkthdr
.tags
);
345 p
= SLIST_NEXT(t
, m_tag_link
);
347 if (p
->m_tag_id
== type
)
349 p
= SLIST_NEXT(p
, m_tag_link
);
354 /* Copy a single tag. */
356 m_tag_copy(struct m_tag
*t
)
360 p
= m_tag_get(t
->m_tag_id
, t
->m_tag_len
, M_NOWAIT
);
363 memcpy(p
+ 1, t
+ 1, t
->m_tag_len
); /* Copy the data */
368 * Copy two tag chains. The destination mbuf (to) loses any attached
369 * tags even if the operation fails. This should not be a problem, as
370 * m_tag_copy_chain() is typically called with a newly-allocated
374 m_tag_copy_chain(struct mbuf
*to
, struct mbuf
*from
)
376 struct m_tag
*p
, *t
, *tprev
= NULL
;
378 m_tag_delete_chain(to
, NULL
);
379 SLIST_FOREACH(p
, &from
->m_pkthdr
.tags
, m_tag_link
) {
382 m_tag_delete_chain(to
, NULL
);
386 SLIST_INSERT_HEAD(&to
->m_pkthdr
.tags
, t
, m_tag_link
);
388 SLIST_INSERT_AFTER(tprev
, t
, m_tag_link
);
394 /* Initialize tags on an mbuf. */
396 m_tag_init(struct mbuf
*m
)
399 SLIST_INIT(&m
->m_pkthdr
.tags
);
402 /* Get first tag in chain. */
404 m_tag_first(struct mbuf
*m
)
407 return (SLIST_FIRST(&m
->m_pkthdr
.tags
));
410 /* Get next tag in chain. */
412 m_tag_next(struct mbuf
*m
, struct m_tag
*t
)
415 return (SLIST_NEXT(t
, m_tag_link
));