xref: /freebsd/sys/kern/uipc_mbuf.c (revision 099a0e588cbe1bbc56a565bf57d722621b47a866)
1df8bae1dSRodney W. Grimes /*
2df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1988, 1991, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
6df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
7df8bae1dSRodney W. Grimes  * are met:
8df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
9df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
10df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
12df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
14df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
15df8bae1dSRodney W. Grimes  *    without specific prior written permission.
16df8bae1dSRodney W. Grimes  *
17df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
28df8bae1dSRodney W. Grimes  *
29df8bae1dSRodney W. Grimes  *	@(#)uipc_mbuf.c	8.2 (Berkeley) 1/4/94
30df8bae1dSRodney W. Grimes  */
31df8bae1dSRodney W. Grimes 
32677b542eSDavid E. O'Brien #include <sys/cdefs.h>
33677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
34677b542eSDavid E. O'Brien 
35e32a5b94SRobert Watson #include "opt_mac.h"
36240ef842SDavid E. O'Brien #include "opt_param.h"
37352d050eSMike Silbersack #include "opt_mbuf_stress_test.h"
38e32a5b94SRobert Watson 
39df8bae1dSRodney W. Grimes #include <sys/param.h>
40df8bae1dSRodney W. Grimes #include <sys/systm.h>
41fb919e4dSMark Murray #include <sys/kernel.h>
42beb699c7SMike Silbersack #include <sys/limits.h>
43fb919e4dSMark Murray #include <sys/lock.h>
44e32a5b94SRobert Watson #include <sys/mac.h>
45f9d0d524SRobert Watson #include <sys/malloc.h>
46df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
47639acc13SGarrett Wollman #include <sys/sysctl.h>
48df8bae1dSRodney W. Grimes #include <sys/domain.h>
49df8bae1dSRodney W. Grimes #include <sys/protosw.h>
50beb699c7SMike Silbersack #include <sys/uio.h>
51fb919e4dSMark Murray 
5228f8db14SBruce Evans int	max_linkhdr;
5328f8db14SBruce Evans int	max_protohdr;
5428f8db14SBruce Evans int	max_hdr;
5528f8db14SBruce Evans int	max_datalen;
5651710a45SMike Silbersack #ifdef MBUF_STRESS_TEST
5755e9f80dSMike Silbersack int	m_defragpackets;
5855e9f80dSMike Silbersack int	m_defragbytes;
5955e9f80dSMike Silbersack int	m_defraguseless;
6055e9f80dSMike Silbersack int	m_defragfailure;
61352d050eSMike Silbersack int	m_defragrandomfailures;
62352d050eSMike Silbersack #endif
637d032714SBosko Milekic 
647d032714SBosko Milekic /*
657d032714SBosko Milekic  * sysctl(8) exported objects
667d032714SBosko Milekic  */
67ce02431fSDoug Rabson SYSCTL_DECL(_kern_ipc);
68639acc13SGarrett Wollman SYSCTL_INT(_kern_ipc, KIPC_MAX_LINKHDR, max_linkhdr, CTLFLAG_RW,
69639acc13SGarrett Wollman 	   &max_linkhdr, 0, "");
70639acc13SGarrett Wollman SYSCTL_INT(_kern_ipc, KIPC_MAX_PROTOHDR, max_protohdr, CTLFLAG_RW,
71639acc13SGarrett Wollman 	   &max_protohdr, 0, "");
72639acc13SGarrett Wollman SYSCTL_INT(_kern_ipc, KIPC_MAX_HDR, max_hdr, CTLFLAG_RW, &max_hdr, 0, "");
73639acc13SGarrett Wollman SYSCTL_INT(_kern_ipc, KIPC_MAX_DATALEN, max_datalen, CTLFLAG_RW,
74639acc13SGarrett Wollman 	   &max_datalen, 0, "");
7551710a45SMike Silbersack #ifdef MBUF_STRESS_TEST
7655e9f80dSMike Silbersack SYSCTL_INT(_kern_ipc, OID_AUTO, m_defragpackets, CTLFLAG_RD,
7755e9f80dSMike Silbersack 	   &m_defragpackets, 0, "");
7855e9f80dSMike Silbersack SYSCTL_INT(_kern_ipc, OID_AUTO, m_defragbytes, CTLFLAG_RD,
7955e9f80dSMike Silbersack 	   &m_defragbytes, 0, "");
8055e9f80dSMike Silbersack SYSCTL_INT(_kern_ipc, OID_AUTO, m_defraguseless, CTLFLAG_RD,
8155e9f80dSMike Silbersack 	   &m_defraguseless, 0, "");
8255e9f80dSMike Silbersack SYSCTL_INT(_kern_ipc, OID_AUTO, m_defragfailure, CTLFLAG_RD,
8355e9f80dSMike Silbersack 	   &m_defragfailure, 0, "");
84352d050eSMike Silbersack SYSCTL_INT(_kern_ipc, OID_AUTO, m_defragrandomfailures, CTLFLAG_RW,
85352d050eSMike Silbersack 	   &m_defragrandomfailures, 0, "");
86352d050eSMike Silbersack #endif
87df8bae1dSRodney W. Grimes 
88df8bae1dSRodney W. Grimes /*
89099a0e58SBosko Milekic  * Malloc-type for external ext_buf ref counts.
90099a0e58SBosko Milekic  */
91099a0e58SBosko Milekic MALLOC_DEFINE(M_MBUF, "mbextcnt", "mbuf external ref counts");
92099a0e58SBosko Milekic 
93099a0e58SBosko Milekic /*
94099a0e58SBosko Milekic  * Allocate a given length worth of mbufs and/or clusters (whatever fits
95099a0e58SBosko Milekic  * best) and return a pointer to the top of the allocated chain.  If an
96099a0e58SBosko Milekic  * existing mbuf chain is provided, then we will append the new chain
97099a0e58SBosko Milekic  * to the existing one but still return the top of the newly allocated
98099a0e58SBosko Milekic  * chain.
99099a0e58SBosko Milekic  */
100099a0e58SBosko Milekic struct mbuf *
101099a0e58SBosko Milekic m_getm(struct mbuf *m, int len, int how, short type)
102099a0e58SBosko Milekic {
103099a0e58SBosko Milekic 	struct mbuf *mb, *top, *cur, *mtail;
104099a0e58SBosko Milekic 	int num, rem;
105099a0e58SBosko Milekic 	int i;
106099a0e58SBosko Milekic 
107099a0e58SBosko Milekic 	KASSERT(len >= 0, ("m_getm(): len is < 0"));
108099a0e58SBosko Milekic 
109099a0e58SBosko Milekic 	/* If m != NULL, we will append to the end of that chain. */
110099a0e58SBosko Milekic 	if (m != NULL)
111099a0e58SBosko Milekic 		for (mtail = m; mtail->m_next != NULL; mtail = mtail->m_next);
112099a0e58SBosko Milekic 	else
113099a0e58SBosko Milekic 		mtail = NULL;
114099a0e58SBosko Milekic 
115099a0e58SBosko Milekic 	/*
116099a0e58SBosko Milekic 	 * Calculate how many mbufs+clusters ("packets") we need and how much
117099a0e58SBosko Milekic 	 * leftover there is after that and allocate the first mbuf+cluster
118099a0e58SBosko Milekic 	 * if required.
119099a0e58SBosko Milekic 	 */
120099a0e58SBosko Milekic 	num = len / MCLBYTES;
121099a0e58SBosko Milekic 	rem = len % MCLBYTES;
122099a0e58SBosko Milekic 	top = cur = NULL;
123099a0e58SBosko Milekic 	if (num > 0) {
124099a0e58SBosko Milekic 		if ((top = cur = m_getcl(how, type, 0)) == NULL)
125099a0e58SBosko Milekic 			goto failed;
126099a0e58SBosko Milekic 	}
127099a0e58SBosko Milekic 	num--;
128099a0e58SBosko Milekic 	top->m_len = 0;
129099a0e58SBosko Milekic 
130099a0e58SBosko Milekic 	for (i = 0; i < num; i++) {
131099a0e58SBosko Milekic 		mb = m_getcl(how, type, 0);
132099a0e58SBosko Milekic 		if (mb == NULL)
133099a0e58SBosko Milekic 			goto failed;
134099a0e58SBosko Milekic 		mb->m_len = 0;
135099a0e58SBosko Milekic 		cur = (cur->m_next = mb);
136099a0e58SBosko Milekic 	}
137099a0e58SBosko Milekic 	if (rem > 0) {
138099a0e58SBosko Milekic 		mb = (rem > MINCLSIZE) ?
139099a0e58SBosko Milekic 		    m_getcl(how, type, 0) : m_get(how, type);
140099a0e58SBosko Milekic 		if (mb == NULL)
141099a0e58SBosko Milekic 			goto failed;
142099a0e58SBosko Milekic 		mb->m_len = 0;
143099a0e58SBosko Milekic 		if (cur == NULL)
144099a0e58SBosko Milekic 			top = mb;
145099a0e58SBosko Milekic 		else
146099a0e58SBosko Milekic 			cur->m_next = mb;
147099a0e58SBosko Milekic 	}
148099a0e58SBosko Milekic 
149099a0e58SBosko Milekic 	if (mtail != NULL)
150099a0e58SBosko Milekic 		mtail->m_next = top;
151099a0e58SBosko Milekic 	return top;
152099a0e58SBosko Milekic failed:
153099a0e58SBosko Milekic 	if (top != NULL)
154099a0e58SBosko Milekic 		m_freem(top);
155099a0e58SBosko Milekic 	return NULL;
156099a0e58SBosko Milekic }
157099a0e58SBosko Milekic 
158099a0e58SBosko Milekic /*
159099a0e58SBosko Milekic  * Free an entire chain of mbufs and associated external buffers, if
160099a0e58SBosko Milekic  * applicable.
161099a0e58SBosko Milekic  */
162099a0e58SBosko Milekic void
163099a0e58SBosko Milekic m_freem(struct mbuf *mb)
164099a0e58SBosko Milekic {
165099a0e58SBosko Milekic 
166099a0e58SBosko Milekic 	while (mb != NULL)
167099a0e58SBosko Milekic 		mb = m_free(mb);
168099a0e58SBosko Milekic }
169099a0e58SBosko Milekic 
170099a0e58SBosko Milekic /*-
171099a0e58SBosko Milekic  * Configure a provided mbuf to refer to the provided external storage
172099a0e58SBosko Milekic  * buffer and setup a reference count for said buffer.  If the setting
173099a0e58SBosko Milekic  * up of the reference count fails, the M_EXT bit will not be set.  If
174099a0e58SBosko Milekic  * successfull, the M_EXT bit is set in the mbuf's flags.
175099a0e58SBosko Milekic  *
176099a0e58SBosko Milekic  * Arguments:
177099a0e58SBosko Milekic  *    mb     The existing mbuf to which to attach the provided buffer.
178099a0e58SBosko Milekic  *    buf    The address of the provided external storage buffer.
179099a0e58SBosko Milekic  *    size   The size of the provided buffer.
180099a0e58SBosko Milekic  *    freef  A pointer to a routine that is responsible for freeing the
181099a0e58SBosko Milekic  *           provided external storage buffer.
182099a0e58SBosko Milekic  *    args   A pointer to an argument structure (of any type) to be passed
183099a0e58SBosko Milekic  *           to the provided freef routine (may be NULL).
184099a0e58SBosko Milekic  *    flags  Any other flags to be passed to the provided mbuf.
185099a0e58SBosko Milekic  *    type   The type that the external storage buffer should be
186099a0e58SBosko Milekic  *           labeled with.
187099a0e58SBosko Milekic  *
188099a0e58SBosko Milekic  * Returns:
189099a0e58SBosko Milekic  *    Nothing.
190099a0e58SBosko Milekic  */
191099a0e58SBosko Milekic void
192099a0e58SBosko Milekic m_extadd(struct mbuf *mb, caddr_t buf, u_int size,
193099a0e58SBosko Milekic     void (*freef)(void *, void *), void *args, int flags, int type)
194099a0e58SBosko Milekic {
195099a0e58SBosko Milekic 	u_int *ref_cnt = NULL;
196099a0e58SBosko Milekic 
197099a0e58SBosko Milekic 	/* XXX Shouldn't be adding EXT_CLUSTER with this API */
198099a0e58SBosko Milekic 	if (type == EXT_CLUSTER)
199099a0e58SBosko Milekic 		ref_cnt = (u_int *)uma_find_refcnt(zone_clust,
200099a0e58SBosko Milekic 		    mb->m_ext.ext_buf);
201099a0e58SBosko Milekic 	else if (type == EXT_EXTREF)
202099a0e58SBosko Milekic 		ref_cnt = mb->m_ext.ref_cnt;
203099a0e58SBosko Milekic 	mb->m_ext.ref_cnt = (ref_cnt == NULL) ?
204099a0e58SBosko Milekic 	    malloc(sizeof(u_int), M_MBUF, M_NOWAIT) : (u_int *)ref_cnt;
205099a0e58SBosko Milekic 	if (mb->m_ext.ref_cnt != NULL) {
206099a0e58SBosko Milekic 		*(mb->m_ext.ref_cnt) = 1;
207099a0e58SBosko Milekic 		mb->m_flags |= (M_EXT | flags);
208099a0e58SBosko Milekic 		mb->m_ext.ext_buf = buf;
209099a0e58SBosko Milekic 		mb->m_data = mb->m_ext.ext_buf;
210099a0e58SBosko Milekic 		mb->m_ext.ext_size = size;
211099a0e58SBosko Milekic 		mb->m_ext.ext_free = freef;
212099a0e58SBosko Milekic 		mb->m_ext.ext_args = args;
213099a0e58SBosko Milekic 		mb->m_ext.ext_type = type;
214099a0e58SBosko Milekic         }
215099a0e58SBosko Milekic }
216099a0e58SBosko Milekic 
217099a0e58SBosko Milekic /*
218099a0e58SBosko Milekic  * Non-directly-exported function to clean up after mbufs with M_EXT
219099a0e58SBosko Milekic  * storage attached to them if the reference count hits 0.
220099a0e58SBosko Milekic  */
221099a0e58SBosko Milekic void
222099a0e58SBosko Milekic mb_free_ext(struct mbuf *m)
223099a0e58SBosko Milekic {
224099a0e58SBosko Milekic 
225099a0e58SBosko Milekic 	MEXT_REM_REF(m);
226099a0e58SBosko Milekic 	if (atomic_cmpset_int(m->m_ext.ref_cnt, 0, 1)) {
227099a0e58SBosko Milekic 		if (m->m_ext.ext_type == EXT_PACKET) {
228099a0e58SBosko Milekic 			uma_zfree(zone_pack, m);
229099a0e58SBosko Milekic 			return;
230099a0e58SBosko Milekic 		} else if (m->m_ext.ext_type == EXT_CLUSTER) {
231099a0e58SBosko Milekic 			uma_zfree(zone_clust, m->m_ext.ext_buf);
232099a0e58SBosko Milekic 			m->m_ext.ext_buf = NULL;
233099a0e58SBosko Milekic 		} else {
234099a0e58SBosko Milekic 			(*(m->m_ext.ext_free))(m->m_ext.ext_buf,
235099a0e58SBosko Milekic 			    m->m_ext.ext_args);
236099a0e58SBosko Milekic 			if (m->m_ext.ext_type != EXT_EXTREF)
237099a0e58SBosko Milekic 				free(m->m_ext.ref_cnt, M_MBUF);
238099a0e58SBosko Milekic 		}
239099a0e58SBosko Milekic 	}
240099a0e58SBosko Milekic 	uma_zfree(zone_mbuf, m);
241099a0e58SBosko Milekic }
242099a0e58SBosko Milekic 
243099a0e58SBosko Milekic /*
2449967cafcSSam Leffler  * "Move" mbuf pkthdr from "from" to "to".
245e37b1fcdSRobert Watson  * "from" must have M_PKTHDR set, and "to" must be empty.
246e37b1fcdSRobert Watson  */
247e37b1fcdSRobert Watson void
2489967cafcSSam Leffler m_move_pkthdr(struct mbuf *to, struct mbuf *from)
249e37b1fcdSRobert Watson {
250e37b1fcdSRobert Watson 
251e37b1fcdSRobert Watson #if 0
2529967cafcSSam Leffler 	/* see below for why these are not enabled */
253fe584538SDag-Erling Smørgrav 	M_ASSERTPKTHDR(to);
254225bff6fSRobert Watson 	/* Note: with MAC, this may not be a good assertion. */
2559967cafcSSam Leffler 	KASSERT(SLIST_EMPTY(&to->m_pkthdr.tags),
2569967cafcSSam Leffler 	    ("m_move_pkthdr: to has tags"));
257e37b1fcdSRobert Watson #endif
2589967cafcSSam Leffler 	KASSERT((to->m_flags & M_EXT) == 0, ("m_move_pkthdr: to has cluster"));
259e32a5b94SRobert Watson #ifdef MAC
260225bff6fSRobert Watson 	/*
261225bff6fSRobert Watson 	 * XXXMAC: It could be this should also occur for non-MAC?
262225bff6fSRobert Watson 	 */
263e32a5b94SRobert Watson 	if (to->m_flags & M_PKTHDR)
264225bff6fSRobert Watson 		m_tag_delete_chain(to, NULL);
265e32a5b94SRobert Watson #endif
266e37b1fcdSRobert Watson 	to->m_flags = from->m_flags & M_COPYFLAGS;
2679967cafcSSam Leffler 	to->m_data = to->m_pktdat;
2689967cafcSSam Leffler 	to->m_pkthdr = from->m_pkthdr;		/* especially tags */
2699967cafcSSam Leffler 	SLIST_INIT(&from->m_pkthdr.tags);	/* purge tags from src */
2709967cafcSSam Leffler 	from->m_flags &= ~M_PKTHDR;
2719967cafcSSam Leffler }
2729967cafcSSam Leffler 
2739967cafcSSam Leffler /*
2749967cafcSSam Leffler  * Duplicate "from"'s mbuf pkthdr in "to".
2759967cafcSSam Leffler  * "from" must have M_PKTHDR set, and "to" must be empty.
2769967cafcSSam Leffler  * In particular, this does a deep copy of the packet tags.
2779967cafcSSam Leffler  */
2789967cafcSSam Leffler int
2799967cafcSSam Leffler m_dup_pkthdr(struct mbuf *to, struct mbuf *from, int how)
2809967cafcSSam Leffler {
2819967cafcSSam Leffler 
2829967cafcSSam Leffler #if 0
2839967cafcSSam Leffler 	/*
2849967cafcSSam Leffler 	 * The mbuf allocator only initializes the pkthdr
2859967cafcSSam Leffler 	 * when the mbuf is allocated with MGETHDR. Many users
2869967cafcSSam Leffler 	 * (e.g. m_copy*, m_prepend) use MGET and then
2879967cafcSSam Leffler 	 * smash the pkthdr as needed causing these
2889967cafcSSam Leffler 	 * assertions to trip.  For now just disable them.
2899967cafcSSam Leffler 	 */
290fe584538SDag-Erling Smørgrav 	M_ASSERTPKTHDR(to);
291225bff6fSRobert Watson 	/* Note: with MAC, this may not be a good assertion. */
2929967cafcSSam Leffler 	KASSERT(SLIST_EMPTY(&to->m_pkthdr.tags), ("m_dup_pkthdr: to has tags"));
2939967cafcSSam Leffler #endif
2949967cafcSSam Leffler #ifdef MAC
2959967cafcSSam Leffler 	if (to->m_flags & M_PKTHDR)
296225bff6fSRobert Watson 		m_tag_delete_chain(to, NULL);
2979967cafcSSam Leffler #endif
298df8c7fc9SMike Silbersack 	to->m_flags = (from->m_flags & M_COPYFLAGS) | (to->m_flags & M_EXT);
299df8c7fc9SMike Silbersack 	if ((to->m_flags & M_EXT) == 0)
3009967cafcSSam Leffler 		to->m_data = to->m_pktdat;
301e37b1fcdSRobert Watson 	to->m_pkthdr = from->m_pkthdr;
3029967cafcSSam Leffler 	SLIST_INIT(&to->m_pkthdr.tags);
303aa65d9f5SRobert Watson 	return (m_tag_copy_chain(to, from, MBTOM(how)));
304e37b1fcdSRobert Watson }
305e37b1fcdSRobert Watson 
306e37b1fcdSRobert Watson /*
307df8bae1dSRodney W. Grimes  * Lesser-used path for M_PREPEND:
308df8bae1dSRodney W. Grimes  * allocate new mbuf to prepend to chain,
309df8bae1dSRodney W. Grimes  * copy junk along.
310df8bae1dSRodney W. Grimes  */
311df8bae1dSRodney W. Grimes struct mbuf *
312122a814aSBosko Milekic m_prepend(struct mbuf *m, int len, int how)
313df8bae1dSRodney W. Grimes {
314df8bae1dSRodney W. Grimes 	struct mbuf *mn;
315df8bae1dSRodney W. Grimes 
316f8bf8e39SMike Silbersack 	if (m->m_flags & M_PKTHDR)
317f8bf8e39SMike Silbersack 		MGETHDR(mn, how, m->m_type);
318f8bf8e39SMike Silbersack 	else
319df8bae1dSRodney W. Grimes 		MGET(mn, how, m->m_type);
320122a814aSBosko Milekic 	if (mn == NULL) {
321df8bae1dSRodney W. Grimes 		m_freem(m);
322122a814aSBosko Milekic 		return (NULL);
323df8bae1dSRodney W. Grimes 	}
324225bff6fSRobert Watson 	if (m->m_flags & M_PKTHDR)
3259967cafcSSam Leffler 		M_MOVE_PKTHDR(mn, m);
326df8bae1dSRodney W. Grimes 	mn->m_next = m;
327df8bae1dSRodney W. Grimes 	m = mn;
328df8bae1dSRodney W. Grimes 	if (len < MHLEN)
329df8bae1dSRodney W. Grimes 		MH_ALIGN(m, len);
330df8bae1dSRodney W. Grimes 	m->m_len = len;
331df8bae1dSRodney W. Grimes 	return (m);
332df8bae1dSRodney W. Grimes }
333df8bae1dSRodney W. Grimes 
334df8bae1dSRodney W. Grimes /*
335df8bae1dSRodney W. Grimes  * Make a copy of an mbuf chain starting "off0" bytes from the beginning,
336df8bae1dSRodney W. Grimes  * continuing for "len" bytes.  If len is M_COPYALL, copy to end of mbuf.
337a163d034SWarner Losh  * The wait parameter is a choice of M_TRYWAIT/M_DONTWAIT from caller.
3381c38f2eaSArchie Cobbs  * Note that the copy is read-only, because clusters are not copied,
3391c38f2eaSArchie Cobbs  * only their reference counts are incremented.
340df8bae1dSRodney W. Grimes  */
341df8bae1dSRodney W. Grimes struct mbuf *
342122a814aSBosko Milekic m_copym(struct mbuf *m, int off0, int len, int wait)
343df8bae1dSRodney W. Grimes {
344122a814aSBosko Milekic 	struct mbuf *n, **np;
345122a814aSBosko Milekic 	int off = off0;
346df8bae1dSRodney W. Grimes 	struct mbuf *top;
347df8bae1dSRodney W. Grimes 	int copyhdr = 0;
348df8bae1dSRodney W. Grimes 
349e0a653ddSAlfred Perlstein 	KASSERT(off >= 0, ("m_copym, negative off %d", off));
350e0a653ddSAlfred Perlstein 	KASSERT(len >= 0, ("m_copym, negative len %d", len));
351df8bae1dSRodney W. Grimes 	if (off == 0 && m->m_flags & M_PKTHDR)
352df8bae1dSRodney W. Grimes 		copyhdr = 1;
353df8bae1dSRodney W. Grimes 	while (off > 0) {
354e0a653ddSAlfred Perlstein 		KASSERT(m != NULL, ("m_copym, offset > size of mbuf chain"));
355df8bae1dSRodney W. Grimes 		if (off < m->m_len)
356df8bae1dSRodney W. Grimes 			break;
357df8bae1dSRodney W. Grimes 		off -= m->m_len;
358df8bae1dSRodney W. Grimes 		m = m->m_next;
359df8bae1dSRodney W. Grimes 	}
360df8bae1dSRodney W. Grimes 	np = &top;
361df8bae1dSRodney W. Grimes 	top = 0;
362df8bae1dSRodney W. Grimes 	while (len > 0) {
363122a814aSBosko Milekic 		if (m == NULL) {
364e0a653ddSAlfred Perlstein 			KASSERT(len == M_COPYALL,
365e0a653ddSAlfred Perlstein 			    ("m_copym, length > size of mbuf chain"));
366df8bae1dSRodney W. Grimes 			break;
367df8bae1dSRodney W. Grimes 		}
368f8bf8e39SMike Silbersack 		if (copyhdr)
369f8bf8e39SMike Silbersack 			MGETHDR(n, wait, m->m_type);
370f8bf8e39SMike Silbersack 		else
371df8bae1dSRodney W. Grimes 			MGET(n, wait, m->m_type);
372df8bae1dSRodney W. Grimes 		*np = n;
373122a814aSBosko Milekic 		if (n == NULL)
374df8bae1dSRodney W. Grimes 			goto nospace;
375df8bae1dSRodney W. Grimes 		if (copyhdr) {
3769967cafcSSam Leffler 			if (!m_dup_pkthdr(n, m, wait))
3779967cafcSSam Leffler 				goto nospace;
378df8bae1dSRodney W. Grimes 			if (len == M_COPYALL)
379df8bae1dSRodney W. Grimes 				n->m_pkthdr.len -= off0;
380df8bae1dSRodney W. Grimes 			else
381df8bae1dSRodney W. Grimes 				n->m_pkthdr.len = len;
382df8bae1dSRodney W. Grimes 			copyhdr = 0;
383df8bae1dSRodney W. Grimes 		}
384df8bae1dSRodney W. Grimes 		n->m_len = min(len, m->m_len - off);
385df8bae1dSRodney W. Grimes 		if (m->m_flags & M_EXT) {
386df8bae1dSRodney W. Grimes 			n->m_data = m->m_data + off;
387df8bae1dSRodney W. Grimes 			n->m_ext = m->m_ext;
388df8bae1dSRodney W. Grimes 			n->m_flags |= M_EXT;
389a5c4836dSDavid Malone 			MEXT_ADD_REF(m);
390df8bae1dSRodney W. Grimes 		} else
391df8bae1dSRodney W. Grimes 			bcopy(mtod(m, caddr_t)+off, mtod(n, caddr_t),
392bd395ae8SBosko Milekic 			    (u_int)n->m_len);
393df8bae1dSRodney W. Grimes 		if (len != M_COPYALL)
394df8bae1dSRodney W. Grimes 			len -= n->m_len;
395df8bae1dSRodney W. Grimes 		off = 0;
396df8bae1dSRodney W. Grimes 		m = m->m_next;
397df8bae1dSRodney W. Grimes 		np = &n->m_next;
398df8bae1dSRodney W. Grimes 	}
39908442f8aSBosko Milekic 	if (top == NULL)
40008442f8aSBosko Milekic 		mbstat.m_mcfail++;	/* XXX: No consistency. */
40108442f8aSBosko Milekic 
402df8bae1dSRodney W. Grimes 	return (top);
403df8bae1dSRodney W. Grimes nospace:
404df8bae1dSRodney W. Grimes 	m_freem(top);
40508442f8aSBosko Milekic 	mbstat.m_mcfail++;	/* XXX: No consistency. */
406122a814aSBosko Milekic 	return (NULL);
407df8bae1dSRodney W. Grimes }
408df8bae1dSRodney W. Grimes 
409df8bae1dSRodney W. Grimes /*
4106a06dea0SGarrett Wollman  * Copy an entire packet, including header (which must be present).
4116a06dea0SGarrett Wollman  * An optimization of the common case `m_copym(m, 0, M_COPYALL, how)'.
4121c38f2eaSArchie Cobbs  * Note that the copy is read-only, because clusters are not copied,
4131c38f2eaSArchie Cobbs  * only their reference counts are incremented.
4145fe86675SLuigi Rizzo  * Preserve alignment of the first mbuf so if the creator has left
4155fe86675SLuigi Rizzo  * some room at the beginning (e.g. for inserting protocol headers)
4165fe86675SLuigi Rizzo  * the copies still have the room available.
4176a06dea0SGarrett Wollman  */
4186a06dea0SGarrett Wollman struct mbuf *
419122a814aSBosko Milekic m_copypacket(struct mbuf *m, int how)
4206a06dea0SGarrett Wollman {
4216a06dea0SGarrett Wollman 	struct mbuf *top, *n, *o;
4226a06dea0SGarrett Wollman 
4236a06dea0SGarrett Wollman 	MGET(n, how, m->m_type);
4246a06dea0SGarrett Wollman 	top = n;
425122a814aSBosko Milekic 	if (n == NULL)
4266a06dea0SGarrett Wollman 		goto nospace;
4276a06dea0SGarrett Wollman 
4289967cafcSSam Leffler 	if (!m_dup_pkthdr(n, m, how))
4299967cafcSSam Leffler 		goto nospace;
4306a06dea0SGarrett Wollman 	n->m_len = m->m_len;
4316a06dea0SGarrett Wollman 	if (m->m_flags & M_EXT) {
4326a06dea0SGarrett Wollman 		n->m_data = m->m_data;
4336a06dea0SGarrett Wollman 		n->m_ext = m->m_ext;
4346a06dea0SGarrett Wollman 		n->m_flags |= M_EXT;
435a5c4836dSDavid Malone 		MEXT_ADD_REF(m);
4366a06dea0SGarrett Wollman 	} else {
4375fe86675SLuigi Rizzo 		n->m_data = n->m_pktdat + (m->m_data - m->m_pktdat );
4386a06dea0SGarrett Wollman 		bcopy(mtod(m, char *), mtod(n, char *), n->m_len);
4396a06dea0SGarrett Wollman 	}
4406a06dea0SGarrett Wollman 
4416a06dea0SGarrett Wollman 	m = m->m_next;
4426a06dea0SGarrett Wollman 	while (m) {
4436a06dea0SGarrett Wollman 		MGET(o, how, m->m_type);
444122a814aSBosko Milekic 		if (o == NULL)
4456a06dea0SGarrett Wollman 			goto nospace;
4466a06dea0SGarrett Wollman 
4476a06dea0SGarrett Wollman 		n->m_next = o;
4486a06dea0SGarrett Wollman 		n = n->m_next;
4496a06dea0SGarrett Wollman 
4506a06dea0SGarrett Wollman 		n->m_len = m->m_len;
4516a06dea0SGarrett Wollman 		if (m->m_flags & M_EXT) {
4526a06dea0SGarrett Wollman 			n->m_data = m->m_data;
4536a06dea0SGarrett Wollman 			n->m_ext = m->m_ext;
4546a06dea0SGarrett Wollman 			n->m_flags |= M_EXT;
455a5c4836dSDavid Malone 			MEXT_ADD_REF(m);
4566a06dea0SGarrett Wollman 		} else {
4576a06dea0SGarrett Wollman 			bcopy(mtod(m, char *), mtod(n, char *), n->m_len);
4586a06dea0SGarrett Wollman 		}
4596a06dea0SGarrett Wollman 
4606a06dea0SGarrett Wollman 		m = m->m_next;
4616a06dea0SGarrett Wollman 	}
4626a06dea0SGarrett Wollman 	return top;
4636a06dea0SGarrett Wollman nospace:
4646a06dea0SGarrett Wollman 	m_freem(top);
46508442f8aSBosko Milekic 	mbstat.m_mcfail++;	/* XXX: No consistency. */
466122a814aSBosko Milekic 	return (NULL);
4676a06dea0SGarrett Wollman }
4686a06dea0SGarrett Wollman 
4696a06dea0SGarrett Wollman /*
470df8bae1dSRodney W. Grimes  * Copy data from an mbuf chain starting "off" bytes from the beginning,
471df8bae1dSRodney W. Grimes  * continuing for "len" bytes, into the indicated buffer.
472df8bae1dSRodney W. Grimes  */
47326f9a767SRodney W. Grimes void
474a8cfc0eeSJulian Elischer m_copydata(const struct mbuf *m, int off, int len, caddr_t cp)
475df8bae1dSRodney W. Grimes {
476bd395ae8SBosko Milekic 	u_int count;
477df8bae1dSRodney W. Grimes 
478e0a653ddSAlfred Perlstein 	KASSERT(off >= 0, ("m_copydata, negative off %d", off));
479e0a653ddSAlfred Perlstein 	KASSERT(len >= 0, ("m_copydata, negative len %d", len));
480df8bae1dSRodney W. Grimes 	while (off > 0) {
481e0a653ddSAlfred Perlstein 		KASSERT(m != NULL, ("m_copydata, offset > size of mbuf chain"));
482df8bae1dSRodney W. Grimes 		if (off < m->m_len)
483df8bae1dSRodney W. Grimes 			break;
484df8bae1dSRodney W. Grimes 		off -= m->m_len;
485df8bae1dSRodney W. Grimes 		m = m->m_next;
486df8bae1dSRodney W. Grimes 	}
487df8bae1dSRodney W. Grimes 	while (len > 0) {
488e0a653ddSAlfred Perlstein 		KASSERT(m != NULL, ("m_copydata, length > size of mbuf chain"));
489df8bae1dSRodney W. Grimes 		count = min(m->m_len - off, len);
490df8bae1dSRodney W. Grimes 		bcopy(mtod(m, caddr_t) + off, cp, count);
491df8bae1dSRodney W. Grimes 		len -= count;
492df8bae1dSRodney W. Grimes 		cp += count;
493df8bae1dSRodney W. Grimes 		off = 0;
494df8bae1dSRodney W. Grimes 		m = m->m_next;
495df8bae1dSRodney W. Grimes 	}
496df8bae1dSRodney W. Grimes }
497df8bae1dSRodney W. Grimes 
498df8bae1dSRodney W. Grimes /*
4991c38f2eaSArchie Cobbs  * Copy a packet header mbuf chain into a completely new chain, including
5001c38f2eaSArchie Cobbs  * copying any mbuf clusters.  Use this instead of m_copypacket() when
5011c38f2eaSArchie Cobbs  * you need a writable copy of an mbuf chain.
5021c38f2eaSArchie Cobbs  */
5031c38f2eaSArchie Cobbs struct mbuf *
504122a814aSBosko Milekic m_dup(struct mbuf *m, int how)
5051c38f2eaSArchie Cobbs {
5061c38f2eaSArchie Cobbs 	struct mbuf **p, *top = NULL;
5071c38f2eaSArchie Cobbs 	int remain, moff, nsize;
5081c38f2eaSArchie Cobbs 
5091c38f2eaSArchie Cobbs 	/* Sanity check */
5101c38f2eaSArchie Cobbs 	if (m == NULL)
511122a814aSBosko Milekic 		return (NULL);
512fe584538SDag-Erling Smørgrav 	M_ASSERTPKTHDR(m);
5131c38f2eaSArchie Cobbs 
5141c38f2eaSArchie Cobbs 	/* While there's more data, get a new mbuf, tack it on, and fill it */
5151c38f2eaSArchie Cobbs 	remain = m->m_pkthdr.len;
5161c38f2eaSArchie Cobbs 	moff = 0;
5171c38f2eaSArchie Cobbs 	p = &top;
5181c38f2eaSArchie Cobbs 	while (remain > 0 || top == NULL) {	/* allow m->m_pkthdr.len == 0 */
5191c38f2eaSArchie Cobbs 		struct mbuf *n;
5201c38f2eaSArchie Cobbs 
5211c38f2eaSArchie Cobbs 		/* Get the next new mbuf */
522099a0e58SBosko Milekic 		if (remain >= MINCLSIZE) {
523099a0e58SBosko Milekic 			n = m_getcl(how, m->m_type, 0);
524099a0e58SBosko Milekic 			nsize = MCLBYTES;
525099a0e58SBosko Milekic 		} else {
526099a0e58SBosko Milekic 			n = m_get(how, m->m_type);
527099a0e58SBosko Milekic 			nsize = MLEN;
528099a0e58SBosko Milekic 		}
5291c38f2eaSArchie Cobbs 		if (n == NULL)
5301c38f2eaSArchie Cobbs 			goto nospace;
531099a0e58SBosko Milekic 
532099a0e58SBosko Milekic 		if (top == NULL) {		/* First one, must be PKTHDR */
533099a0e58SBosko Milekic 			if (!m_dup_pkthdr(n, m, how)) {
534099a0e58SBosko Milekic 				m_free(n);
5351c38f2eaSArchie Cobbs 				goto nospace;
5361c38f2eaSArchie Cobbs 			}
537099a0e58SBosko Milekic 			nsize = MHLEN;
5381c38f2eaSArchie Cobbs 		}
5391c38f2eaSArchie Cobbs 		n->m_len = 0;
5401c38f2eaSArchie Cobbs 
5411c38f2eaSArchie Cobbs 		/* Link it into the new chain */
5421c38f2eaSArchie Cobbs 		*p = n;
5431c38f2eaSArchie Cobbs 		p = &n->m_next;
5441c38f2eaSArchie Cobbs 
5451c38f2eaSArchie Cobbs 		/* Copy data from original mbuf(s) into new mbuf */
5461c38f2eaSArchie Cobbs 		while (n->m_len < nsize && m != NULL) {
5471c38f2eaSArchie Cobbs 			int chunk = min(nsize - n->m_len, m->m_len - moff);
5481c38f2eaSArchie Cobbs 
5491c38f2eaSArchie Cobbs 			bcopy(m->m_data + moff, n->m_data + n->m_len, chunk);
5501c38f2eaSArchie Cobbs 			moff += chunk;
5511c38f2eaSArchie Cobbs 			n->m_len += chunk;
5521c38f2eaSArchie Cobbs 			remain -= chunk;
5531c38f2eaSArchie Cobbs 			if (moff == m->m_len) {
5541c38f2eaSArchie Cobbs 				m = m->m_next;
5551c38f2eaSArchie Cobbs 				moff = 0;
5561c38f2eaSArchie Cobbs 			}
5571c38f2eaSArchie Cobbs 		}
5581c38f2eaSArchie Cobbs 
5591c38f2eaSArchie Cobbs 		/* Check correct total mbuf length */
5601c38f2eaSArchie Cobbs 		KASSERT((remain > 0 && m != NULL) || (remain == 0 && m == NULL),
561a48740b6SDavid E. O'Brien 		    	("%s: bogus m_pkthdr.len", __func__));
5621c38f2eaSArchie Cobbs 	}
5631c38f2eaSArchie Cobbs 	return (top);
5641c38f2eaSArchie Cobbs 
5651c38f2eaSArchie Cobbs nospace:
5661c38f2eaSArchie Cobbs 	m_freem(top);
56708442f8aSBosko Milekic 	mbstat.m_mcfail++;	/* XXX: No consistency. */
568122a814aSBosko Milekic 	return (NULL);
5691c38f2eaSArchie Cobbs }
5701c38f2eaSArchie Cobbs 
5711c38f2eaSArchie Cobbs /*
572df8bae1dSRodney W. Grimes  * Concatenate mbuf chain n to m.
573df8bae1dSRodney W. Grimes  * Both chains must be of the same type (e.g. MT_DATA).
574df8bae1dSRodney W. Grimes  * Any m_pkthdr is not updated.
575df8bae1dSRodney W. Grimes  */
57626f9a767SRodney W. Grimes void
577122a814aSBosko Milekic m_cat(struct mbuf *m, struct mbuf *n)
578df8bae1dSRodney W. Grimes {
579df8bae1dSRodney W. Grimes 	while (m->m_next)
580df8bae1dSRodney W. Grimes 		m = m->m_next;
581df8bae1dSRodney W. Grimes 	while (n) {
582df8bae1dSRodney W. Grimes 		if (m->m_flags & M_EXT ||
583df8bae1dSRodney W. Grimes 		    m->m_data + m->m_len + n->m_len >= &m->m_dat[MLEN]) {
584df8bae1dSRodney W. Grimes 			/* just join the two chains */
585df8bae1dSRodney W. Grimes 			m->m_next = n;
586df8bae1dSRodney W. Grimes 			return;
587df8bae1dSRodney W. Grimes 		}
588df8bae1dSRodney W. Grimes 		/* splat the data from one into the other */
589df8bae1dSRodney W. Grimes 		bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len,
590df8bae1dSRodney W. Grimes 		    (u_int)n->m_len);
591df8bae1dSRodney W. Grimes 		m->m_len += n->m_len;
592df8bae1dSRodney W. Grimes 		n = m_free(n);
593df8bae1dSRodney W. Grimes 	}
594df8bae1dSRodney W. Grimes }
595df8bae1dSRodney W. Grimes 
59626f9a767SRodney W. Grimes void
597122a814aSBosko Milekic m_adj(struct mbuf *mp, int req_len)
598df8bae1dSRodney W. Grimes {
599122a814aSBosko Milekic 	int len = req_len;
600122a814aSBosko Milekic 	struct mbuf *m;
601122a814aSBosko Milekic 	int count;
602df8bae1dSRodney W. Grimes 
603df8bae1dSRodney W. Grimes 	if ((m = mp) == NULL)
604df8bae1dSRodney W. Grimes 		return;
605df8bae1dSRodney W. Grimes 	if (len >= 0) {
606df8bae1dSRodney W. Grimes 		/*
607df8bae1dSRodney W. Grimes 		 * Trim from head.
608df8bae1dSRodney W. Grimes 		 */
609df8bae1dSRodney W. Grimes 		while (m != NULL && len > 0) {
610df8bae1dSRodney W. Grimes 			if (m->m_len <= len) {
611df8bae1dSRodney W. Grimes 				len -= m->m_len;
612df8bae1dSRodney W. Grimes 				m->m_len = 0;
613df8bae1dSRodney W. Grimes 				m = m->m_next;
614df8bae1dSRodney W. Grimes 			} else {
615df8bae1dSRodney W. Grimes 				m->m_len -= len;
616df8bae1dSRodney W. Grimes 				m->m_data += len;
617df8bae1dSRodney W. Grimes 				len = 0;
618df8bae1dSRodney W. Grimes 			}
619df8bae1dSRodney W. Grimes 		}
620df8bae1dSRodney W. Grimes 		m = mp;
621df8bae1dSRodney W. Grimes 		if (mp->m_flags & M_PKTHDR)
622df8bae1dSRodney W. Grimes 			m->m_pkthdr.len -= (req_len - len);
623df8bae1dSRodney W. Grimes 	} else {
624df8bae1dSRodney W. Grimes 		/*
625df8bae1dSRodney W. Grimes 		 * Trim from tail.  Scan the mbuf chain,
626df8bae1dSRodney W. Grimes 		 * calculating its length and finding the last mbuf.
627df8bae1dSRodney W. Grimes 		 * If the adjustment only affects this mbuf, then just
628df8bae1dSRodney W. Grimes 		 * adjust and return.  Otherwise, rescan and truncate
629df8bae1dSRodney W. Grimes 		 * after the remaining size.
630df8bae1dSRodney W. Grimes 		 */
631df8bae1dSRodney W. Grimes 		len = -len;
632df8bae1dSRodney W. Grimes 		count = 0;
633df8bae1dSRodney W. Grimes 		for (;;) {
634df8bae1dSRodney W. Grimes 			count += m->m_len;
635df8bae1dSRodney W. Grimes 			if (m->m_next == (struct mbuf *)0)
636df8bae1dSRodney W. Grimes 				break;
637df8bae1dSRodney W. Grimes 			m = m->m_next;
638df8bae1dSRodney W. Grimes 		}
639df8bae1dSRodney W. Grimes 		if (m->m_len >= len) {
640df8bae1dSRodney W. Grimes 			m->m_len -= len;
641df8bae1dSRodney W. Grimes 			if (mp->m_flags & M_PKTHDR)
642df8bae1dSRodney W. Grimes 				mp->m_pkthdr.len -= len;
643df8bae1dSRodney W. Grimes 			return;
644df8bae1dSRodney W. Grimes 		}
645df8bae1dSRodney W. Grimes 		count -= len;
646df8bae1dSRodney W. Grimes 		if (count < 0)
647df8bae1dSRodney W. Grimes 			count = 0;
648df8bae1dSRodney W. Grimes 		/*
649df8bae1dSRodney W. Grimes 		 * Correct length for chain is "count".
650df8bae1dSRodney W. Grimes 		 * Find the mbuf with last data, adjust its length,
651df8bae1dSRodney W. Grimes 		 * and toss data from remaining mbufs on chain.
652df8bae1dSRodney W. Grimes 		 */
653df8bae1dSRodney W. Grimes 		m = mp;
654df8bae1dSRodney W. Grimes 		if (m->m_flags & M_PKTHDR)
655df8bae1dSRodney W. Grimes 			m->m_pkthdr.len = count;
656df8bae1dSRodney W. Grimes 		for (; m; m = m->m_next) {
657df8bae1dSRodney W. Grimes 			if (m->m_len >= count) {
658df8bae1dSRodney W. Grimes 				m->m_len = count;
659df8bae1dSRodney W. Grimes 				break;
660df8bae1dSRodney W. Grimes 			}
661df8bae1dSRodney W. Grimes 			count -= m->m_len;
662df8bae1dSRodney W. Grimes 		}
663797f2d22SPoul-Henning Kamp 		while (m->m_next)
664797f2d22SPoul-Henning Kamp 			(m = m->m_next) ->m_len = 0;
665df8bae1dSRodney W. Grimes 	}
666df8bae1dSRodney W. Grimes }
667df8bae1dSRodney W. Grimes 
668df8bae1dSRodney W. Grimes /*
669df8bae1dSRodney W. Grimes  * Rearange an mbuf chain so that len bytes are contiguous
670df8bae1dSRodney W. Grimes  * and in the data area of an mbuf (so that mtod and dtom
671df8bae1dSRodney W. Grimes  * will work for a structure of size len).  Returns the resulting
672df8bae1dSRodney W. Grimes  * mbuf chain on success, frees it and returns null on failure.
673df8bae1dSRodney W. Grimes  * If there is room, it will add up to max_protohdr-len extra bytes to the
674df8bae1dSRodney W. Grimes  * contiguous region in an attempt to avoid being called next time.
675df8bae1dSRodney W. Grimes  */
676df8bae1dSRodney W. Grimes struct mbuf *
677122a814aSBosko Milekic m_pullup(struct mbuf *n, int len)
678df8bae1dSRodney W. Grimes {
679122a814aSBosko Milekic 	struct mbuf *m;
680122a814aSBosko Milekic 	int count;
681df8bae1dSRodney W. Grimes 	int space;
682df8bae1dSRodney W. Grimes 
683df8bae1dSRodney W. Grimes 	/*
684df8bae1dSRodney W. Grimes 	 * If first mbuf has no cluster, and has room for len bytes
685df8bae1dSRodney W. Grimes 	 * without shifting current data, pullup into it,
686df8bae1dSRodney W. Grimes 	 * otherwise allocate a new mbuf to prepend to the chain.
687df8bae1dSRodney W. Grimes 	 */
688df8bae1dSRodney W. Grimes 	if ((n->m_flags & M_EXT) == 0 &&
689df8bae1dSRodney W. Grimes 	    n->m_data + len < &n->m_dat[MLEN] && n->m_next) {
690df8bae1dSRodney W. Grimes 		if (n->m_len >= len)
691df8bae1dSRodney W. Grimes 			return (n);
692df8bae1dSRodney W. Grimes 		m = n;
693df8bae1dSRodney W. Grimes 		n = n->m_next;
694df8bae1dSRodney W. Grimes 		len -= m->m_len;
695df8bae1dSRodney W. Grimes 	} else {
696df8bae1dSRodney W. Grimes 		if (len > MHLEN)
697df8bae1dSRodney W. Grimes 			goto bad;
698a163d034SWarner Losh 		MGET(m, M_DONTWAIT, n->m_type);
699122a814aSBosko Milekic 		if (m == NULL)
700df8bae1dSRodney W. Grimes 			goto bad;
701df8bae1dSRodney W. Grimes 		m->m_len = 0;
7029967cafcSSam Leffler 		if (n->m_flags & M_PKTHDR)
7039967cafcSSam Leffler 			M_MOVE_PKTHDR(m, n);
704df8bae1dSRodney W. Grimes 	}
705df8bae1dSRodney W. Grimes 	space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
706df8bae1dSRodney W. Grimes 	do {
707df8bae1dSRodney W. Grimes 		count = min(min(max(len, max_protohdr), space), n->m_len);
708df8bae1dSRodney W. Grimes 		bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len,
709bd395ae8SBosko Milekic 		  (u_int)count);
710df8bae1dSRodney W. Grimes 		len -= count;
711df8bae1dSRodney W. Grimes 		m->m_len += count;
712df8bae1dSRodney W. Grimes 		n->m_len -= count;
713df8bae1dSRodney W. Grimes 		space -= count;
714df8bae1dSRodney W. Grimes 		if (n->m_len)
715df8bae1dSRodney W. Grimes 			n->m_data += count;
716df8bae1dSRodney W. Grimes 		else
717df8bae1dSRodney W. Grimes 			n = m_free(n);
718df8bae1dSRodney W. Grimes 	} while (len > 0 && n);
719df8bae1dSRodney W. Grimes 	if (len > 0) {
720df8bae1dSRodney W. Grimes 		(void) m_free(m);
721df8bae1dSRodney W. Grimes 		goto bad;
722df8bae1dSRodney W. Grimes 	}
723df8bae1dSRodney W. Grimes 	m->m_next = n;
724df8bae1dSRodney W. Grimes 	return (m);
725df8bae1dSRodney W. Grimes bad:
726df8bae1dSRodney W. Grimes 	m_freem(n);
72708442f8aSBosko Milekic 	mbstat.m_mpfail++;	/* XXX: No consistency. */
728122a814aSBosko Milekic 	return (NULL);
729df8bae1dSRodney W. Grimes }
730df8bae1dSRodney W. Grimes 
731df8bae1dSRodney W. Grimes /*
732df8bae1dSRodney W. Grimes  * Partition an mbuf chain in two pieces, returning the tail --
733df8bae1dSRodney W. Grimes  * all but the first len0 bytes.  In case of failure, it returns NULL and
734df8bae1dSRodney W. Grimes  * attempts to restore the chain to its original state.
73548d183faSArchie Cobbs  *
73648d183faSArchie Cobbs  * Note that the resulting mbufs might be read-only, because the new
73748d183faSArchie Cobbs  * mbuf can end up sharing an mbuf cluster with the original mbuf if
73848d183faSArchie Cobbs  * the "breaking point" happens to lie within a cluster mbuf. Use the
73948d183faSArchie Cobbs  * M_WRITABLE() macro to check for this case.
740df8bae1dSRodney W. Grimes  */
741df8bae1dSRodney W. Grimes struct mbuf *
742122a814aSBosko Milekic m_split(struct mbuf *m0, int len0, int wait)
743df8bae1dSRodney W. Grimes {
744122a814aSBosko Milekic 	struct mbuf *m, *n;
745bd395ae8SBosko Milekic 	u_int len = len0, remain;
746df8bae1dSRodney W. Grimes 
747df8bae1dSRodney W. Grimes 	for (m = m0; m && len > m->m_len; m = m->m_next)
748df8bae1dSRodney W. Grimes 		len -= m->m_len;
749122a814aSBosko Milekic 	if (m == NULL)
750122a814aSBosko Milekic 		return (NULL);
751df8bae1dSRodney W. Grimes 	remain = m->m_len - len;
752df8bae1dSRodney W. Grimes 	if (m0->m_flags & M_PKTHDR) {
753df8bae1dSRodney W. Grimes 		MGETHDR(n, wait, m0->m_type);
754122a814aSBosko Milekic 		if (n == NULL)
755122a814aSBosko Milekic 			return (NULL);
756df8bae1dSRodney W. Grimes 		n->m_pkthdr.rcvif = m0->m_pkthdr.rcvif;
757df8bae1dSRodney W. Grimes 		n->m_pkthdr.len = m0->m_pkthdr.len - len0;
758df8bae1dSRodney W. Grimes 		m0->m_pkthdr.len = len0;
759df8bae1dSRodney W. Grimes 		if (m->m_flags & M_EXT)
760df8bae1dSRodney W. Grimes 			goto extpacket;
761df8bae1dSRodney W. Grimes 		if (remain > MHLEN) {
762df8bae1dSRodney W. Grimes 			/* m can't be the lead packet */
763df8bae1dSRodney W. Grimes 			MH_ALIGN(n, 0);
764df8bae1dSRodney W. Grimes 			n->m_next = m_split(m, len, wait);
765122a814aSBosko Milekic 			if (n->m_next == NULL) {
766df8bae1dSRodney W. Grimes 				(void) m_free(n);
767122a814aSBosko Milekic 				return (NULL);
76840376987SJeffrey Hsu 			} else {
76940376987SJeffrey Hsu 				n->m_len = 0;
770df8bae1dSRodney W. Grimes 				return (n);
77140376987SJeffrey Hsu 			}
772df8bae1dSRodney W. Grimes 		} else
773df8bae1dSRodney W. Grimes 			MH_ALIGN(n, remain);
774df8bae1dSRodney W. Grimes 	} else if (remain == 0) {
775df8bae1dSRodney W. Grimes 		n = m->m_next;
776122a814aSBosko Milekic 		m->m_next = NULL;
777df8bae1dSRodney W. Grimes 		return (n);
778df8bae1dSRodney W. Grimes 	} else {
779df8bae1dSRodney W. Grimes 		MGET(n, wait, m->m_type);
780122a814aSBosko Milekic 		if (n == NULL)
781122a814aSBosko Milekic 			return (NULL);
782df8bae1dSRodney W. Grimes 		M_ALIGN(n, remain);
783df8bae1dSRodney W. Grimes 	}
784df8bae1dSRodney W. Grimes extpacket:
785df8bae1dSRodney W. Grimes 	if (m->m_flags & M_EXT) {
786df8bae1dSRodney W. Grimes 		n->m_flags |= M_EXT;
787df8bae1dSRodney W. Grimes 		n->m_ext = m->m_ext;
788a5c4836dSDavid Malone 		MEXT_ADD_REF(m);
789df8bae1dSRodney W. Grimes 		n->m_data = m->m_data + len;
790df8bae1dSRodney W. Grimes 	} else {
791df8bae1dSRodney W. Grimes 		bcopy(mtod(m, caddr_t) + len, mtod(n, caddr_t), remain);
792df8bae1dSRodney W. Grimes 	}
793df8bae1dSRodney W. Grimes 	n->m_len = remain;
794df8bae1dSRodney W. Grimes 	m->m_len = len;
795df8bae1dSRodney W. Grimes 	n->m_next = m->m_next;
796122a814aSBosko Milekic 	m->m_next = NULL;
797df8bae1dSRodney W. Grimes 	return (n);
798df8bae1dSRodney W. Grimes }
799df8bae1dSRodney W. Grimes /*
800df8bae1dSRodney W. Grimes  * Routine to copy from device local memory into mbufs.
801f5eece3fSBosko Milekic  * Note that `off' argument is offset into first mbuf of target chain from
802f5eece3fSBosko Milekic  * which to begin copying the data to.
803df8bae1dSRodney W. Grimes  */
804df8bae1dSRodney W. Grimes struct mbuf *
805f5eece3fSBosko Milekic m_devget(char *buf, int totlen, int off, struct ifnet *ifp,
806122a814aSBosko Milekic 	 void (*copy)(char *from, caddr_t to, u_int len))
807df8bae1dSRodney W. Grimes {
808122a814aSBosko Milekic 	struct mbuf *m;
809099a0e58SBosko Milekic 	struct mbuf *top = NULL, **mp = &top;
810f5eece3fSBosko Milekic 	int len;
811df8bae1dSRodney W. Grimes 
812f5eece3fSBosko Milekic 	if (off < 0 || off > MHLEN)
813f5eece3fSBosko Milekic 		return (NULL);
814f5eece3fSBosko Milekic 
815df8bae1dSRodney W. Grimes 	while (totlen > 0) {
816099a0e58SBosko Milekic 		if (top == NULL) {	/* First one, must be PKTHDR */
817f5eece3fSBosko Milekic 			if (totlen + off >= MINCLSIZE) {
818099a0e58SBosko Milekic 				m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
819f5eece3fSBosko Milekic 				len = MCLBYTES;
820df8bae1dSRodney W. Grimes 			} else {
821099a0e58SBosko Milekic 				m = m_gethdr(M_DONTWAIT, MT_DATA);
822099a0e58SBosko Milekic 				len = MHLEN;
823099a0e58SBosko Milekic 
824099a0e58SBosko Milekic 				/* Place initial small packet/header at end of mbuf */
825099a0e58SBosko Milekic 				if (m && totlen + off + max_linkhdr <= MLEN) {
826df8bae1dSRodney W. Grimes 					m->m_data += max_linkhdr;
827f5eece3fSBosko Milekic 					len -= max_linkhdr;
828df8bae1dSRodney W. Grimes 				}
829f5eece3fSBosko Milekic 			}
830099a0e58SBosko Milekic 			if (m == NULL)
831099a0e58SBosko Milekic 				return NULL;
832099a0e58SBosko Milekic 			m->m_pkthdr.rcvif = ifp;
833099a0e58SBosko Milekic 			m->m_pkthdr.len = totlen;
834099a0e58SBosko Milekic 		} else {
835099a0e58SBosko Milekic 			if (totlen + off >= MINCLSIZE) {
836099a0e58SBosko Milekic 				m = m_getcl(M_DONTWAIT, MT_DATA, 0);
837099a0e58SBosko Milekic 				len = MCLBYTES;
838099a0e58SBosko Milekic 			} else {
839099a0e58SBosko Milekic 				m = m_get(M_DONTWAIT, MT_DATA);
840099a0e58SBosko Milekic 				len = MLEN;
841099a0e58SBosko Milekic 			}
842099a0e58SBosko Milekic 			if (m == NULL) {
843099a0e58SBosko Milekic 				m_freem(top);
844099a0e58SBosko Milekic 				return NULL;
845099a0e58SBosko Milekic 			}
846099a0e58SBosko Milekic 		}
847f5eece3fSBosko Milekic 		if (off) {
848f5eece3fSBosko Milekic 			m->m_data += off;
849f5eece3fSBosko Milekic 			len -= off;
850f5eece3fSBosko Milekic 			off = 0;
851f5eece3fSBosko Milekic 		}
852f5eece3fSBosko Milekic 		m->m_len = len = min(totlen, len);
853df8bae1dSRodney W. Grimes 		if (copy)
854bd395ae8SBosko Milekic 			copy(buf, mtod(m, caddr_t), (u_int)len);
855df8bae1dSRodney W. Grimes 		else
856bd395ae8SBosko Milekic 			bcopy(buf, mtod(m, caddr_t), (u_int)len);
857f5eece3fSBosko Milekic 		buf += len;
858df8bae1dSRodney W. Grimes 		*mp = m;
859df8bae1dSRodney W. Grimes 		mp = &m->m_next;
860df8bae1dSRodney W. Grimes 		totlen -= len;
861df8bae1dSRodney W. Grimes 	}
862df8bae1dSRodney W. Grimes 	return (top);
863df8bae1dSRodney W. Grimes }
864c5789ba3SPoul-Henning Kamp 
865c5789ba3SPoul-Henning Kamp /*
866c5789ba3SPoul-Henning Kamp  * Copy data from a buffer back into the indicated mbuf chain,
867c5789ba3SPoul-Henning Kamp  * starting "off" bytes from the beginning, extending the mbuf
868c5789ba3SPoul-Henning Kamp  * chain if necessary.
869c5789ba3SPoul-Henning Kamp  */
870c5789ba3SPoul-Henning Kamp void
87124665342SLuigi Rizzo m_copyback(struct mbuf *m0, int off, int len, c_caddr_t cp)
872c5789ba3SPoul-Henning Kamp {
873122a814aSBosko Milekic 	int mlen;
874122a814aSBosko Milekic 	struct mbuf *m = m0, *n;
875c5789ba3SPoul-Henning Kamp 	int totlen = 0;
876c5789ba3SPoul-Henning Kamp 
877122a814aSBosko Milekic 	if (m0 == NULL)
878c5789ba3SPoul-Henning Kamp 		return;
879c5789ba3SPoul-Henning Kamp 	while (off > (mlen = m->m_len)) {
880c5789ba3SPoul-Henning Kamp 		off -= mlen;
881c5789ba3SPoul-Henning Kamp 		totlen += mlen;
882122a814aSBosko Milekic 		if (m->m_next == NULL) {
883099a0e58SBosko Milekic 			n = m_get(M_DONTWAIT, m->m_type);
884122a814aSBosko Milekic 			if (n == NULL)
885c5789ba3SPoul-Henning Kamp 				goto out;
886099a0e58SBosko Milekic 			bzero(mtod(n, caddr_t), MLEN);
887c5789ba3SPoul-Henning Kamp 			n->m_len = min(MLEN, len + off);
888c5789ba3SPoul-Henning Kamp 			m->m_next = n;
889c5789ba3SPoul-Henning Kamp 		}
890c5789ba3SPoul-Henning Kamp 		m = m->m_next;
891c5789ba3SPoul-Henning Kamp 	}
892c5789ba3SPoul-Henning Kamp 	while (len > 0) {
893c5789ba3SPoul-Henning Kamp 		mlen = min (m->m_len - off, len);
894bd395ae8SBosko Milekic 		bcopy(cp, off + mtod(m, caddr_t), (u_int)mlen);
895c5789ba3SPoul-Henning Kamp 		cp += mlen;
896c5789ba3SPoul-Henning Kamp 		len -= mlen;
897c5789ba3SPoul-Henning Kamp 		mlen += off;
898c5789ba3SPoul-Henning Kamp 		off = 0;
899c5789ba3SPoul-Henning Kamp 		totlen += mlen;
900c5789ba3SPoul-Henning Kamp 		if (len == 0)
901c5789ba3SPoul-Henning Kamp 			break;
902122a814aSBosko Milekic 		if (m->m_next == NULL) {
903a163d034SWarner Losh 			n = m_get(M_DONTWAIT, m->m_type);
904122a814aSBosko Milekic 			if (n == NULL)
905c5789ba3SPoul-Henning Kamp 				break;
906c5789ba3SPoul-Henning Kamp 			n->m_len = min(MLEN, len);
907c5789ba3SPoul-Henning Kamp 			m->m_next = n;
908c5789ba3SPoul-Henning Kamp 		}
909c5789ba3SPoul-Henning Kamp 		m = m->m_next;
910c5789ba3SPoul-Henning Kamp 	}
911c5789ba3SPoul-Henning Kamp out:	if (((m = m0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen))
912c5789ba3SPoul-Henning Kamp 		m->m_pkthdr.len = totlen;
913c5789ba3SPoul-Henning Kamp }
914ce4a64f7SPoul-Henning Kamp 
91537621fd5SBruce M Simpson /*
91637621fd5SBruce M Simpson  * Apply function f to the data in an mbuf chain starting "off" bytes from
91737621fd5SBruce M Simpson  * the beginning, continuing for "len" bytes.
91837621fd5SBruce M Simpson  */
91937621fd5SBruce M Simpson int
92037621fd5SBruce M Simpson m_apply(struct mbuf *m, int off, int len,
92154065297SBruce M Simpson     int (*f)(void *, void *, u_int), void *arg)
92237621fd5SBruce M Simpson {
92354065297SBruce M Simpson 	u_int count;
92437621fd5SBruce M Simpson 	int rval;
92537621fd5SBruce M Simpson 
92637621fd5SBruce M Simpson 	KASSERT(off >= 0, ("m_apply, negative off %d", off));
92737621fd5SBruce M Simpson 	KASSERT(len >= 0, ("m_apply, negative len %d", len));
92837621fd5SBruce M Simpson 	while (off > 0) {
92937621fd5SBruce M Simpson 		KASSERT(m != NULL, ("m_apply, offset > size of mbuf chain"));
93037621fd5SBruce M Simpson 		if (off < m->m_len)
93137621fd5SBruce M Simpson 			break;
93237621fd5SBruce M Simpson 		off -= m->m_len;
93337621fd5SBruce M Simpson 		m = m->m_next;
93437621fd5SBruce M Simpson 	}
93537621fd5SBruce M Simpson 	while (len > 0) {
93637621fd5SBruce M Simpson 		KASSERT(m != NULL, ("m_apply, offset > size of mbuf chain"));
93737621fd5SBruce M Simpson 		count = min(m->m_len - off, len);
93837621fd5SBruce M Simpson 		rval = (*f)(arg, mtod(m, caddr_t) + off, count);
93937621fd5SBruce M Simpson 		if (rval)
94037621fd5SBruce M Simpson 			return (rval);
94137621fd5SBruce M Simpson 		len -= count;
94237621fd5SBruce M Simpson 		off = 0;
94337621fd5SBruce M Simpson 		m = m->m_next;
94437621fd5SBruce M Simpson 	}
94537621fd5SBruce M Simpson 	return (0);
94637621fd5SBruce M Simpson }
94737621fd5SBruce M Simpson 
94837621fd5SBruce M Simpson /*
94937621fd5SBruce M Simpson  * Return a pointer to mbuf/offset of location in mbuf chain.
95037621fd5SBruce M Simpson  */
95137621fd5SBruce M Simpson struct mbuf *
95237621fd5SBruce M Simpson m_getptr(struct mbuf *m, int loc, int *off)
95337621fd5SBruce M Simpson {
95437621fd5SBruce M Simpson 
95537621fd5SBruce M Simpson 	while (loc >= 0) {
95654065297SBruce M Simpson 		/* Normal end of search. */
95737621fd5SBruce M Simpson 		if (m->m_len > loc) {
95837621fd5SBruce M Simpson 			*off = loc;
95937621fd5SBruce M Simpson 			return (m);
96037621fd5SBruce M Simpson 		} else {
96137621fd5SBruce M Simpson 			loc -= m->m_len;
96237621fd5SBruce M Simpson 			if (m->m_next == NULL) {
96337621fd5SBruce M Simpson 				if (loc == 0) {
96454065297SBruce M Simpson 					/* Point at the end of valid data. */
96537621fd5SBruce M Simpson 					*off = m->m_len;
96637621fd5SBruce M Simpson 					return (m);
96754065297SBruce M Simpson 				}
96837621fd5SBruce M Simpson 				return (NULL);
96954065297SBruce M Simpson 			}
97037621fd5SBruce M Simpson 			m = m->m_next;
97137621fd5SBruce M Simpson 		}
97237621fd5SBruce M Simpson 	}
97337621fd5SBruce M Simpson 	return (NULL);
97437621fd5SBruce M Simpson }
97537621fd5SBruce M Simpson 
976ce4a64f7SPoul-Henning Kamp void
977ce4a64f7SPoul-Henning Kamp m_print(const struct mbuf *m)
978ce4a64f7SPoul-Henning Kamp {
979ce4a64f7SPoul-Henning Kamp 	int len;
9806357e7b5SEivind Eklund 	const struct mbuf *m2;
981ce4a64f7SPoul-Henning Kamp 
982ce4a64f7SPoul-Henning Kamp 	len = m->m_pkthdr.len;
983ce4a64f7SPoul-Henning Kamp 	m2 = m;
984ce4a64f7SPoul-Henning Kamp 	while (len) {
985ce4a64f7SPoul-Henning Kamp 		printf("%p %*D\n", m2, m2->m_len, (u_char *)m2->m_data, "-");
986ce4a64f7SPoul-Henning Kamp 		len -= m2->m_len;
987ce4a64f7SPoul-Henning Kamp 		m2 = m2->m_next;
988ce4a64f7SPoul-Henning Kamp 	}
989ce4a64f7SPoul-Henning Kamp 	return;
990ce4a64f7SPoul-Henning Kamp }
9913f2e06c5SPoul-Henning Kamp 
992bd395ae8SBosko Milekic u_int
9933f2e06c5SPoul-Henning Kamp m_fixhdr(struct mbuf *m0)
9943f2e06c5SPoul-Henning Kamp {
995bd395ae8SBosko Milekic 	u_int len;
9963f2e06c5SPoul-Henning Kamp 
997ac6e585dSPoul-Henning Kamp 	len = m_length(m0, NULL);
9983f2e06c5SPoul-Henning Kamp 	m0->m_pkthdr.len = len;
999ac6e585dSPoul-Henning Kamp 	return (len);
1000ac6e585dSPoul-Henning Kamp }
1001ac6e585dSPoul-Henning Kamp 
1002bd395ae8SBosko Milekic u_int
1003ac6e585dSPoul-Henning Kamp m_length(struct mbuf *m0, struct mbuf **last)
1004ac6e585dSPoul-Henning Kamp {
1005ac6e585dSPoul-Henning Kamp 	struct mbuf *m;
1006bd395ae8SBosko Milekic 	u_int len;
1007ac6e585dSPoul-Henning Kamp 
1008ac6e585dSPoul-Henning Kamp 	len = 0;
1009ac6e585dSPoul-Henning Kamp 	for (m = m0; m != NULL; m = m->m_next) {
1010ac6e585dSPoul-Henning Kamp 		len += m->m_len;
1011ac6e585dSPoul-Henning Kamp 		if (m->m_next == NULL)
1012ac6e585dSPoul-Henning Kamp 			break;
1013ac6e585dSPoul-Henning Kamp 	}
1014ac6e585dSPoul-Henning Kamp 	if (last != NULL)
1015ac6e585dSPoul-Henning Kamp 		*last = m;
1016ac6e585dSPoul-Henning Kamp 	return (len);
10173f2e06c5SPoul-Henning Kamp }
101855e9f80dSMike Silbersack 
101955e9f80dSMike Silbersack /*
102055e9f80dSMike Silbersack  * Defragment a mbuf chain, returning the shortest possible
102155e9f80dSMike Silbersack  * chain of mbufs and clusters.  If allocation fails and
102255e9f80dSMike Silbersack  * this cannot be completed, NULL will be returned, but
102355e9f80dSMike Silbersack  * the passed in chain will be unchanged.  Upon success,
102455e9f80dSMike Silbersack  * the original chain will be freed, and the new chain
102555e9f80dSMike Silbersack  * will be returned.
102655e9f80dSMike Silbersack  *
102755e9f80dSMike Silbersack  * If a non-packet header is passed in, the original
102855e9f80dSMike Silbersack  * mbuf (chain?) will be returned unharmed.
102955e9f80dSMike Silbersack  */
103055e9f80dSMike Silbersack struct mbuf *
103155e9f80dSMike Silbersack m_defrag(struct mbuf *m0, int how)
103255e9f80dSMike Silbersack {
103355e9f80dSMike Silbersack 	struct mbuf *m_new = NULL, *m_final = NULL;
103455e9f80dSMike Silbersack 	int progress = 0, length;
103555e9f80dSMike Silbersack 
103655e9f80dSMike Silbersack 	if (!(m0->m_flags & M_PKTHDR))
103755e9f80dSMike Silbersack 		return (m0);
103855e9f80dSMike Silbersack 
1039f8bf8e39SMike Silbersack 	m_fixhdr(m0); /* Needed sanity check */
1040f8bf8e39SMike Silbersack 
1041352d050eSMike Silbersack #ifdef MBUF_STRESS_TEST
1042352d050eSMike Silbersack 	if (m_defragrandomfailures) {
1043352d050eSMike Silbersack 		int temp = arc4random() & 0xff;
1044352d050eSMike Silbersack 		if (temp == 0xba)
1045352d050eSMike Silbersack 			goto nospace;
1046352d050eSMike Silbersack 	}
1047352d050eSMike Silbersack #endif
104855e9f80dSMike Silbersack 
104955e9f80dSMike Silbersack 	if (m0->m_pkthdr.len > MHLEN)
105055e9f80dSMike Silbersack 		m_final = m_getcl(how, MT_DATA, M_PKTHDR);
105155e9f80dSMike Silbersack 	else
105255e9f80dSMike Silbersack 		m_final = m_gethdr(how, MT_DATA);
105355e9f80dSMike Silbersack 
105455e9f80dSMike Silbersack 	if (m_final == NULL)
105555e9f80dSMike Silbersack 		goto nospace;
105655e9f80dSMike Silbersack 
1057a89ec05eSPeter Wemm 	if (m_dup_pkthdr(m_final, m0, how) == 0)
105855e9f80dSMike Silbersack 		goto nospace;
105955e9f80dSMike Silbersack 
106055e9f80dSMike Silbersack 	m_new = m_final;
106155e9f80dSMike Silbersack 
106255e9f80dSMike Silbersack 	while (progress < m0->m_pkthdr.len) {
106355e9f80dSMike Silbersack 		length = m0->m_pkthdr.len - progress;
106455e9f80dSMike Silbersack 		if (length > MCLBYTES)
106555e9f80dSMike Silbersack 			length = MCLBYTES;
106655e9f80dSMike Silbersack 
106755e9f80dSMike Silbersack 		if (m_new == NULL) {
106855e9f80dSMike Silbersack 			if (length > MLEN)
106955e9f80dSMike Silbersack 				m_new = m_getcl(how, MT_DATA, 0);
107055e9f80dSMike Silbersack 			else
107155e9f80dSMike Silbersack 				m_new = m_get(how, MT_DATA);
107255e9f80dSMike Silbersack 			if (m_new == NULL)
107355e9f80dSMike Silbersack 				goto nospace;
107455e9f80dSMike Silbersack 		}
107555e9f80dSMike Silbersack 
107655e9f80dSMike Silbersack 		m_copydata(m0, progress, length, mtod(m_new, caddr_t));
107755e9f80dSMike Silbersack 		progress += length;
107855e9f80dSMike Silbersack 		m_new->m_len = length;
107955e9f80dSMike Silbersack 		if (m_new != m_final)
108055e9f80dSMike Silbersack 			m_cat(m_final, m_new);
108155e9f80dSMike Silbersack 		m_new = NULL;
108255e9f80dSMike Silbersack 	}
108351710a45SMike Silbersack #ifdef MBUF_STRESS_TEST
108455e9f80dSMike Silbersack 	if (m0->m_next == NULL)
108555e9f80dSMike Silbersack 		m_defraguseless++;
108651710a45SMike Silbersack #endif
108755e9f80dSMike Silbersack 	m_freem(m0);
108855e9f80dSMike Silbersack 	m0 = m_final;
108951710a45SMike Silbersack #ifdef MBUF_STRESS_TEST
109055e9f80dSMike Silbersack 	m_defragpackets++;
109155e9f80dSMike Silbersack 	m_defragbytes += m0->m_pkthdr.len;
109251710a45SMike Silbersack #endif
109355e9f80dSMike Silbersack 	return (m0);
109455e9f80dSMike Silbersack nospace:
109551710a45SMike Silbersack #ifdef MBUF_STRESS_TEST
109655e9f80dSMike Silbersack 	m_defragfailure++;
109751710a45SMike Silbersack #endif
109855e9f80dSMike Silbersack 	if (m_new)
109955e9f80dSMike Silbersack 		m_free(m_new);
110055e9f80dSMike Silbersack 	if (m_final)
110155e9f80dSMike Silbersack 		m_freem(m_final);
110255e9f80dSMike Silbersack 	return (NULL);
110355e9f80dSMike Silbersack }
11043390d476SMike Silbersack 
11053390d476SMike Silbersack #ifdef MBUF_STRESS_TEST
11063390d476SMike Silbersack 
11073390d476SMike Silbersack /*
11083390d476SMike Silbersack  * Fragment an mbuf chain.  There's no reason you'd ever want to do
11093390d476SMike Silbersack  * this in normal usage, but it's great for stress testing various
11103390d476SMike Silbersack  * mbuf consumers.
11113390d476SMike Silbersack  *
11123390d476SMike Silbersack  * If fragmentation is not possible, the original chain will be
11133390d476SMike Silbersack  * returned.
11143390d476SMike Silbersack  *
11153390d476SMike Silbersack  * Possible length values:
11163390d476SMike Silbersack  * 0	 no fragmentation will occur
11173390d476SMike Silbersack  * > 0	each fragment will be of the specified length
11183390d476SMike Silbersack  * -1	each fragment will be the same random value in length
11193390d476SMike Silbersack  * -2	each fragment's length will be entirely random
11203390d476SMike Silbersack  * (Random values range from 1 to 256)
11213390d476SMike Silbersack  */
11223390d476SMike Silbersack struct mbuf *
11233390d476SMike Silbersack m_fragment(struct mbuf *m0, int how, int length)
11243390d476SMike Silbersack {
11253390d476SMike Silbersack 	struct mbuf *m_new = NULL, *m_final = NULL;
11263390d476SMike Silbersack 	int progress = 0;
11273390d476SMike Silbersack 
11283390d476SMike Silbersack 	if (!(m0->m_flags & M_PKTHDR))
11293390d476SMike Silbersack 		return (m0);
11303390d476SMike Silbersack 
11313390d476SMike Silbersack 	if ((length == 0) || (length < -2))
11323390d476SMike Silbersack 		return (m0);
11333390d476SMike Silbersack 
11343390d476SMike Silbersack 	m_fixhdr(m0); /* Needed sanity check */
11353390d476SMike Silbersack 
11363390d476SMike Silbersack 	m_final = m_getcl(how, MT_DATA, M_PKTHDR);
11373390d476SMike Silbersack 
11383390d476SMike Silbersack 	if (m_final == NULL)
11393390d476SMike Silbersack 		goto nospace;
11403390d476SMike Silbersack 
11418dee2f67SMike Silbersack 	if (m_dup_pkthdr(m_final, m0, how) == 0)
11423390d476SMike Silbersack 		goto nospace;
11433390d476SMike Silbersack 
11443390d476SMike Silbersack 	m_new = m_final;
11453390d476SMike Silbersack 
11463390d476SMike Silbersack 	if (length == -1)
11473390d476SMike Silbersack 		length = 1 + (arc4random() & 255);
11483390d476SMike Silbersack 
11493390d476SMike Silbersack 	while (progress < m0->m_pkthdr.len) {
11503390d476SMike Silbersack 		int fraglen;
11513390d476SMike Silbersack 
11523390d476SMike Silbersack 		if (length > 0)
11533390d476SMike Silbersack 			fraglen = length;
11543390d476SMike Silbersack 		else
11553390d476SMike Silbersack 			fraglen = 1 + (arc4random() & 255);
11563390d476SMike Silbersack 		if (fraglen > m0->m_pkthdr.len - progress)
11573390d476SMike Silbersack 			fraglen = m0->m_pkthdr.len - progress;
11583390d476SMike Silbersack 
11593390d476SMike Silbersack 		if (fraglen > MCLBYTES)
11603390d476SMike Silbersack 			fraglen = MCLBYTES;
11613390d476SMike Silbersack 
11623390d476SMike Silbersack 		if (m_new == NULL) {
11633390d476SMike Silbersack 			m_new = m_getcl(how, MT_DATA, 0);
11643390d476SMike Silbersack 			if (m_new == NULL)
11653390d476SMike Silbersack 				goto nospace;
11663390d476SMike Silbersack 		}
11673390d476SMike Silbersack 
11683390d476SMike Silbersack 		m_copydata(m0, progress, fraglen, mtod(m_new, caddr_t));
11693390d476SMike Silbersack 		progress += fraglen;
11703390d476SMike Silbersack 		m_new->m_len = fraglen;
11713390d476SMike Silbersack 		if (m_new != m_final)
11723390d476SMike Silbersack 			m_cat(m_final, m_new);
11733390d476SMike Silbersack 		m_new = NULL;
11743390d476SMike Silbersack 	}
11753390d476SMike Silbersack 	m_freem(m0);
11763390d476SMike Silbersack 	m0 = m_final;
11773390d476SMike Silbersack 	return (m0);
11783390d476SMike Silbersack nospace:
11793390d476SMike Silbersack 	if (m_new)
11803390d476SMike Silbersack 		m_free(m_new);
11813390d476SMike Silbersack 	if (m_final)
11823390d476SMike Silbersack 		m_freem(m_final);
11833390d476SMike Silbersack 	/* Return the original chain on failure */
11843390d476SMike Silbersack 	return (m0);
11853390d476SMike Silbersack }
11863390d476SMike Silbersack 
11873390d476SMike Silbersack #endif
1188beb699c7SMike Silbersack 
1189beb699c7SMike Silbersack struct mbuf *
1190beb699c7SMike Silbersack m_uiotombuf(struct uio *uio, int how, int len)
1191beb699c7SMike Silbersack {
1192beb699c7SMike Silbersack 	struct mbuf *m_new = NULL, *m_final = NULL;
1193beb699c7SMike Silbersack 	int progress = 0, error = 0, length, total;
1194beb699c7SMike Silbersack 
1195beb699c7SMike Silbersack 	if (len > 0)
1196beb699c7SMike Silbersack 		total = min(uio->uio_resid, len);
1197beb699c7SMike Silbersack 	else
1198beb699c7SMike Silbersack 		total = uio->uio_resid;
1199beb699c7SMike Silbersack 	if (total > MHLEN)
1200beb699c7SMike Silbersack 		m_final = m_getcl(how, MT_DATA, M_PKTHDR);
1201beb699c7SMike Silbersack 	else
1202beb699c7SMike Silbersack 		m_final = m_gethdr(how, MT_DATA);
1203beb699c7SMike Silbersack 	if (m_final == NULL)
1204beb699c7SMike Silbersack 		goto nospace;
1205beb699c7SMike Silbersack 	m_new = m_final;
1206beb699c7SMike Silbersack 	while (progress < total) {
1207beb699c7SMike Silbersack 		length = total - progress;
1208beb699c7SMike Silbersack 		if (length > MCLBYTES)
1209beb699c7SMike Silbersack 			length = MCLBYTES;
1210beb699c7SMike Silbersack 		if (m_new == NULL) {
1211beb699c7SMike Silbersack 			if (length > MLEN)
1212beb699c7SMike Silbersack 				m_new = m_getcl(how, MT_DATA, 0);
1213beb699c7SMike Silbersack 			else
1214beb699c7SMike Silbersack 				m_new = m_get(how, MT_DATA);
1215beb699c7SMike Silbersack 			if (m_new == NULL)
1216beb699c7SMike Silbersack 				goto nospace;
1217beb699c7SMike Silbersack 		}
1218beb699c7SMike Silbersack 		error = uiomove(mtod(m_new, void *), length, uio);
1219beb699c7SMike Silbersack 		if (error)
1220beb699c7SMike Silbersack 			goto nospace;
1221beb699c7SMike Silbersack 		progress += length;
1222beb699c7SMike Silbersack 		m_new->m_len = length;
1223beb699c7SMike Silbersack 		if (m_new != m_final)
1224beb699c7SMike Silbersack 			m_cat(m_final, m_new);
1225beb699c7SMike Silbersack 		m_new = NULL;
1226beb699c7SMike Silbersack 	}
1227beb699c7SMike Silbersack 	m_fixhdr(m_final);
1228beb699c7SMike Silbersack 	return (m_final);
1229beb699c7SMike Silbersack nospace:
1230beb699c7SMike Silbersack 	if (m_new)
1231beb699c7SMike Silbersack 		m_free(m_new);
1232beb699c7SMike Silbersack 	if (m_final)
1233beb699c7SMike Silbersack 		m_freem(m_final);
1234beb699c7SMike Silbersack 	return (NULL);
1235beb699c7SMike Silbersack }
1236