1 /* $FreeBSD: src/sys/kern/uipc_mbuf2.c,v 1.2.2.5 2003/01/23 21:06:44 sam Exp $ */
2 /* $KAME: uipc_mbuf2.c,v 1.31 2001/11/28 11:08:53 itojun Exp $ */
3 /* $NetBSD: uipc_mbuf.c,v 1.40 1999/04/01 00:23:25 thorpej Exp $ */
6 * Copyright (C) 1999 WIDE Project.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * Copyright (c) 1982, 1986, 1988, 1991, 1993
36 * The Regents of the University of California. All rights reserved.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by the University of
49 * California, Berkeley and its contributors.
50 * 4. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * @(#)uipc_mbuf.c 8.4 (Berkeley) 2/14/95
69 /*#define PULLDOWN_DEBUG*/
71 #include <sys/param.h>
72 #include <sys/systm.h>
73 #include <sys/kernel.h>
75 #include <sys/malloc.h>
78 #define M_SHAREDCLUSTER(m) \
79 (((m)->m_flags & M_EXT) != 0 && \
80 ((m)->m_ext.ext_free || mclrefcnt[mtocl((m)->m_ext.ext_buf)] > 1))
82 MALLOC_DEFINE(M_PACKET_TAGS, "tag", "packet-attached information");
84 /* can't call it m_dup(), as freebsd[34] uses m_dup() with different arg */
85 static struct mbuf *m_dup1 __P((struct mbuf *, int, int, int));
88 * ensure that [off, off + len) is contiguous on the mbuf chain "m".
89 * packet chain before "off" is kept untouched.
90 * if offp == NULL, the target will start at <retval, 0> on resulting chain.
91 * if offp != NULL, the target will start at <retval, *offp> on resulting chain.
93 * on error return (NULL return value), original "m" will be freed.
95 * XXX M_TRAILINGSPACE/M_LEADINGSPACE on shared cluster (sharedcluster)
98 m_pulldown(m, off, len, offp)
104 int hlen, tlen, olen;
107 /* check invalid arguments. */
109 panic("m == NULL in m_pulldown()");
110 if (len > MCLBYTES) {
112 return NULL; /* impossible */
115 #ifdef PULLDOWN_DEBUG
119 for (t = m; t; t = t->m_next)
120 printf(" %d", t->m_len);
125 while (n != NULL && off > 0) {
131 /* be sure to point non-empty mbuf */
132 while (n != NULL && n->m_len == 0)
136 return NULL; /* mbuf chain too short */
139 sharedcluster = M_SHAREDCLUSTER(n);
142 * the target data is on <n, off>.
143 * if we got enough data on the mbuf "n", we're done.
145 if ((off == 0 || offp) && len <= n->m_len - off && !sharedcluster)
149 * when len <= n->m_len - off and off != 0, it is a special case.
150 * len bytes from <n, off> sits in single mbuf, but the caller does
151 * not like the starting position (off).
152 * chop the current mbuf into two pieces, set off to 0.
154 if (len <= n->m_len - off) {
155 o = m_dup1(n, off, n->m_len - off, M_DONTWAIT);
158 return NULL; /* ENOBUFS */
161 o->m_next = n->m_next;
169 * we need to take hlen from <n, off> and tlen from <n->m_next, 0>,
170 * and construct contiguous mbuf with m_len == len.
171 * note that hlen + tlen == len, and tlen > 0.
173 hlen = n->m_len - off;
177 * ensure that we have enough trailing data on mbuf chain.
178 * if not, we can do nothing about the chain.
181 for (o = n->m_next; o != NULL; o = o->m_next)
183 if (hlen + olen < len) {
185 return NULL; /* mbuf chain too short */
190 * we need to use m_copydata() to get data from <n->m_next, 0>.
192 if ((off == 0 || offp) && M_TRAILINGSPACE(n) >= tlen &&
194 m_copydata(n->m_next, 0, tlen, mtod(n, caddr_t) + n->m_len);
196 m_adj(n->m_next, tlen);
199 if ((off == 0 || offp) && M_LEADINGSPACE(n->m_next) >= hlen &&
201 n->m_next->m_data -= hlen;
202 n->m_next->m_len += hlen;
203 bcopy(mtod(n, caddr_t) + off, mtod(n->m_next, caddr_t), hlen);
211 * now, we need to do the hard way. don't m_copy as there's no room
214 MGET(o, M_DONTWAIT, m->m_type);
215 if (o && len > MLEN) {
216 MCLGET(o, M_DONTWAIT);
217 if ((o->m_flags & M_EXT) == 0) {
224 return NULL; /* ENOBUFS */
226 /* get hlen from <n, off> into <o, 0> */
228 bcopy(mtod(n, caddr_t) + off, mtod(o, caddr_t), hlen);
230 /* get tlen from <n->m_next, 0> into <o, hlen> */
231 m_copydata(n->m_next, 0, tlen, mtod(o, caddr_t) + o->m_len);
233 m_adj(n->m_next, tlen);
234 o->m_next = n->m_next;
240 #ifdef PULLDOWN_DEBUG
244 for (t = m; t; t = t->m_next)
245 printf("%c%d", t == n ? '*' : ' ', t->m_len);
246 printf(" (off=%d)\n", off);
255 m_dup1(m, off, len, wait)
267 if (off == 0 && (m->m_flags & M_PKTHDR) != 0) {
269 MGETHDR(n, wait, m->m_type);
273 MGET(n, wait, m->m_type);
278 if ((n->m_flags & M_EXT) == 0) {
286 if (copyhdr && !m_dup_pkthdr(n, m, wait)) {
290 m_copydata(m, off, len, mtod(n, caddr_t));
294 /* Get a packet tag structure along with specified data following. */
296 m_tag_alloc(u_int32_t cookie, int type, int len, int wait)
302 t = malloc(len + sizeof(struct m_tag), M_PACKET_TAGS, wait);
307 t->m_tag_cookie = cookie;
312 /* Free a packet tag. */
314 m_tag_free(struct m_tag *t)
316 free(t, M_PACKET_TAGS);
319 /* Prepend a packet tag. */
321 m_tag_prepend(struct mbuf *m, struct m_tag *t)
323 KASSERT(m && t, ("m_tag_prepend: null argument, m %p t %p", m, t));
324 SLIST_INSERT_HEAD(&m->m_pkthdr.tags, t, m_tag_link);
327 /* Unlink a packet tag. */
329 m_tag_unlink(struct mbuf *m, struct m_tag *t)
331 KASSERT(m && t, ("m_tag_unlink: null argument, m %p t %p", m, t));
332 SLIST_REMOVE(&m->m_pkthdr.tags, t, m_tag, m_tag_link);
335 /* Unlink and free a packet tag. */
337 m_tag_delete(struct mbuf *m, struct m_tag *t)
339 KASSERT(m && t, ("m_tag_delete: null argument, m %p t %p", m, t));
344 /* Unlink and free a packet tag chain, starting from given tag. */
346 m_tag_delete_chain(struct mbuf *m, struct m_tag *t)
350 KASSERT(m, ("m_tag_delete_chain: null mbuf"));
354 p = SLIST_FIRST(&m->m_pkthdr.tags);
357 while ((q = SLIST_NEXT(p, m_tag_link)) != NULL)
362 /* Find a tag, starting from a given position. */
364 m_tag_locate(struct mbuf *m, u_int32_t cookie, int type, struct m_tag *t)
368 KASSERT(m, ("m_tag_find: null mbuf"));
370 p = SLIST_FIRST(&m->m_pkthdr.tags);
372 p = SLIST_NEXT(t, m_tag_link);
374 if (p->m_tag_cookie == cookie && p->m_tag_id == type)
376 p = SLIST_NEXT(p, m_tag_link);
381 /* Copy a single tag. */
383 m_tag_copy(struct m_tag *t, int how)
387 KASSERT(t, ("m_tag_copy: null tag"));
388 p = m_tag_alloc(t->m_tag_cookie, t->m_tag_id, t->m_tag_len, how);
391 bcopy(t + 1, p + 1, t->m_tag_len); /* Copy the data */
396 * Copy two tag chains. The destination mbuf (to) loses any attached
397 * tags even if the operation fails. This should not be a problem, as
398 * m_tag_copy_chain() is typically called with a newly-allocated
402 m_tag_copy_chain(struct mbuf *to, struct mbuf *from, int how)
404 struct m_tag *p, *t, *tprev = NULL;
407 ("m_tag_copy: null argument, to %p from %p", to, from));
408 m_tag_delete_chain(to, NULL);
409 SLIST_FOREACH(p, &from->m_pkthdr.tags, m_tag_link) {
410 t = m_tag_copy(p, how);
412 m_tag_delete_chain(to, NULL);
416 SLIST_INSERT_HEAD(&to->m_pkthdr.tags, t, m_tag_link);
418 SLIST_INSERT_AFTER(tprev, t, m_tag_link);
425 /* Initialize tags on an mbuf. */
427 m_tag_init(struct mbuf *m)
429 SLIST_INIT(&m->m_pkthdr.tags);
432 /* Get first tag in chain. */
434 m_tag_first(struct mbuf *m)
436 return SLIST_FIRST(&m->m_pkthdr.tags);
439 /* Get next tag in chain. */
441 m_tag_next(struct mbuf *m, struct m_tag *t)
443 return SLIST_NEXT(t, m_tag_link);