Home | History | Annotate | Line # | Download | only in kern
uipc_mbuf.c revision 1.100.2.15
      1  1.100.2.14      yamt /*	$NetBSD: uipc_mbuf.c,v 1.100.2.15 2007/10/27 11:40:55 yamt Exp $	*/
      2        1.42   thorpej 
      3        1.42   thorpej /*-
      4        1.53   thorpej  * Copyright (c) 1999, 2001 The NetBSD Foundation, Inc.
      5        1.42   thorpej  * All rights reserved.
      6        1.42   thorpej  *
      7        1.42   thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8        1.42   thorpej  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9        1.42   thorpej  * NASA Ames Research Center.
     10        1.42   thorpej  *
     11        1.42   thorpej  * Redistribution and use in source and binary forms, with or without
     12        1.42   thorpej  * modification, are permitted provided that the following conditions
     13        1.42   thorpej  * are met:
     14        1.42   thorpej  * 1. Redistributions of source code must retain the above copyright
     15        1.42   thorpej  *    notice, this list of conditions and the following disclaimer.
     16        1.42   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     17        1.42   thorpej  *    notice, this list of conditions and the following disclaimer in the
     18        1.42   thorpej  *    documentation and/or other materials provided with the distribution.
     19        1.42   thorpej  * 3. All advertising materials mentioning features or use of this software
     20        1.42   thorpej  *    must display the following acknowledgement:
     21        1.42   thorpej  *	This product includes software developed by the NetBSD
     22        1.42   thorpej  *	Foundation, Inc. and its contributors.
     23        1.42   thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24        1.42   thorpej  *    contributors may be used to endorse or promote products derived
     25        1.42   thorpej  *    from this software without specific prior written permission.
     26        1.42   thorpej  *
     27        1.42   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28        1.42   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29        1.42   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30        1.42   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31        1.42   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32        1.42   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33        1.42   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34        1.42   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35        1.42   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36        1.42   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37        1.42   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     38        1.42   thorpej  */
     39        1.10       cgd 
     40         1.1       cgd /*
     41         1.9   mycroft  * Copyright (c) 1982, 1986, 1988, 1991, 1993
     42         1.9   mycroft  *	The Regents of the University of California.  All rights reserved.
     43         1.1       cgd  *
     44         1.1       cgd  * Redistribution and use in source and binary forms, with or without
     45         1.1       cgd  * modification, are permitted provided that the following conditions
     46         1.1       cgd  * are met:
     47         1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     48         1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     49         1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     50         1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     51         1.1       cgd  *    documentation and/or other materials provided with the distribution.
     52        1.70       agc  * 3. Neither the name of the University nor the names of its contributors
     53         1.1       cgd  *    may be used to endorse or promote products derived from this software
     54         1.1       cgd  *    without specific prior written permission.
     55         1.1       cgd  *
     56         1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     57         1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     58         1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     59         1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     60         1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     61         1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     62         1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     63         1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     64         1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     65         1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     66         1.1       cgd  * SUCH DAMAGE.
     67         1.1       cgd  *
     68        1.26      fvdl  *	@(#)uipc_mbuf.c	8.4 (Berkeley) 2/14/95
     69         1.1       cgd  */
     70        1.56     lukem 
     71        1.56     lukem #include <sys/cdefs.h>
     72  1.100.2.14      yamt __KERNEL_RCSID(0, "$NetBSD: uipc_mbuf.c,v 1.100.2.15 2007/10/27 11:40:55 yamt Exp $");
     73        1.69    martin 
     74        1.69    martin #include "opt_mbuftrace.h"
     75   1.100.2.6      yamt #include "opt_ddb.h"
     76        1.24       mrg 
     77         1.6   mycroft #include <sys/param.h>
     78         1.6   mycroft #include <sys/systm.h>
     79         1.6   mycroft #include <sys/proc.h>
     80         1.6   mycroft #include <sys/malloc.h>
     81         1.1       cgd #define MBTYPES
     82         1.6   mycroft #include <sys/mbuf.h>
     83         1.6   mycroft #include <sys/kernel.h>
     84         1.6   mycroft #include <sys/syslog.h>
     85         1.6   mycroft #include <sys/domain.h>
     86         1.6   mycroft #include <sys/protosw.h>
     87        1.28   thorpej #include <sys/pool.h>
     88        1.27      matt #include <sys/socket.h>
     89        1.55    simonb #include <sys/sysctl.h>
     90        1.55    simonb 
     91        1.27      matt #include <net/if.h>
     92        1.14  christos 
     93        1.65   thorpej #include <uvm/uvm.h>
     94        1.23       mrg 
     95        1.42   thorpej 
     96        1.28   thorpej struct	pool mbpool;		/* mbuf pool */
     97        1.28   thorpej struct	pool mclpool;		/* mbuf cluster pool */
     98        1.28   thorpej 
     99        1.53   thorpej struct pool_cache mbpool_cache;
    100        1.53   thorpej struct pool_cache mclpool_cache;
    101        1.53   thorpej 
    102        1.18   thorpej struct mbstat mbstat;
    103        1.18   thorpej int	max_linkhdr;
    104        1.18   thorpej int	max_protohdr;
    105        1.18   thorpej int	max_hdr;
    106        1.18   thorpej int	max_datalen;
    107        1.18   thorpej 
    108        1.65   thorpej static int mb_ctor(void *, void *, int);
    109        1.65   thorpej 
    110   1.100.2.6      yamt static void	*mclpool_alloc(struct pool *, int);
    111   1.100.2.6      yamt static void	mclpool_release(struct pool *, void *);
    112        1.58   thorpej 
    113   1.100.2.6      yamt static struct pool_allocator mclpool_allocator = {
    114  1.100.2.11      yamt 	.pa_alloc = mclpool_alloc,
    115  1.100.2.11      yamt 	.pa_free = mclpool_release,
    116        1.58   thorpej };
    117        1.58   thorpej 
    118        1.79  junyoung static struct mbuf *m_copym0(struct mbuf *, int, int, int, int);
    119        1.85      yamt static struct mbuf *m_split0(struct mbuf *, int, int, int);
    120        1.86      yamt static int m_copyback0(struct mbuf **, int, int, const void *, int, int);
    121        1.85      yamt 
    122        1.85      yamt /* flags for m_copyback0 */
    123        1.85      yamt #define	M_COPYBACK0_COPYBACK	0x0001	/* copyback from cp */
    124        1.85      yamt #define	M_COPYBACK0_PRESERVE	0x0002	/* preserve original data */
    125        1.85      yamt #define	M_COPYBACK0_COW		0x0004	/* do copy-on-write */
    126        1.85      yamt #define	M_COPYBACK0_EXTEND	0x0008	/* extend chain */
    127        1.28   thorpej 
    128   1.100.2.6      yamt static const char mclpool_warnmsg[] =
    129        1.42   thorpej     "WARNING: mclpool limit reached; increase NMBCLUSTERS";
    130        1.63   thorpej 
    131        1.63   thorpej MALLOC_DEFINE(M_MBUF, "mbuf", "mbuf");
    132        1.42   thorpej 
    133        1.64      matt #ifdef MBUFTRACE
    134        1.64      matt struct mownerhead mowners = LIST_HEAD_INITIALIZER(mowners);
    135        1.64      matt struct mowner unknown_mowners[] = {
    136  1.100.2.11      yamt 	MOWNER_INIT("unknown", "free"),
    137  1.100.2.11      yamt 	MOWNER_INIT("unknown", "data"),
    138  1.100.2.11      yamt 	MOWNER_INIT("unknown", "header"),
    139  1.100.2.11      yamt 	MOWNER_INIT("unknown", "soname"),
    140  1.100.2.11      yamt 	MOWNER_INIT("unknown", "soopts"),
    141  1.100.2.11      yamt 	MOWNER_INIT("unknown", "ftable"),
    142  1.100.2.11      yamt 	MOWNER_INIT("unknown", "control"),
    143  1.100.2.11      yamt 	MOWNER_INIT("unknown", "oobdata"),
    144        1.64      matt };
    145  1.100.2.11      yamt struct mowner revoked_mowner = MOWNER_INIT("revoked", "");
    146        1.64      matt #endif
    147        1.64      matt 
    148  1.100.2.15      yamt #define	MEXT_LOCK(m)	mutex_enter(&(m)->m_ext.ext_lock)
    149  1.100.2.15      yamt #define	MEXT_UNLOCK(m)	mutex_exit(&(m)->m_ext.ext_lock)
    150   1.100.2.7      yamt #define	MEXT_ISEMBEDDED(m) ((m)->m_ext_ref == (m))
    151   1.100.2.7      yamt 
    152   1.100.2.8      yamt static inline void
    153   1.100.2.8      yamt mcl_inc_reference(struct mbuf *m)
    154   1.100.2.8      yamt {
    155   1.100.2.8      yamt 
    156  1.100.2.10      yamt 	MEXT_LOCK(m);
    157  1.100.2.10      yamt 	m->m_ext.ext_refcnt++;
    158  1.100.2.10      yamt 	MEXT_UNLOCK(m);
    159   1.100.2.8      yamt }
    160   1.100.2.8      yamt 
    161  1.100.2.12      yamt static inline bool
    162   1.100.2.8      yamt mcl_dec_and_test_reference(struct mbuf *m)
    163   1.100.2.8      yamt {
    164  1.100.2.12      yamt 	bool gotzero;
    165   1.100.2.8      yamt 
    166   1.100.2.8      yamt 	MEXT_LOCK(m);
    167  1.100.2.10      yamt 	KASSERT(m->m_ext.ext_refcnt > 0);
    168   1.100.2.8      yamt 	m->m_ext.ext_refcnt--;
    169  1.100.2.10      yamt 	gotzero = (m->m_ext.ext_refcnt == 0);
    170   1.100.2.8      yamt 	MEXT_UNLOCK(m);
    171   1.100.2.8      yamt 
    172  1.100.2.10      yamt 	return gotzero;
    173   1.100.2.8      yamt }
    174   1.100.2.7      yamt 
    175  1.100.2.10      yamt #define	MCLADDREFERENCE(o, n)						\
    176   1.100.2.7      yamt do {									\
    177   1.100.2.7      yamt 	KASSERT(((o)->m_flags & M_EXT) != 0);				\
    178   1.100.2.7      yamt 	KASSERT(((n)->m_flags & M_EXT) == 0);				\
    179   1.100.2.7      yamt 	KASSERT((o)->m_ext.ext_refcnt >= 1);				\
    180   1.100.2.7      yamt 	(n)->m_flags |= ((o)->m_flags & M_EXTCOPYFLAGS);		\
    181   1.100.2.8      yamt 	mcl_inc_reference((o));						\
    182   1.100.2.7      yamt 	(n)->m_ext_ref = (o)->m_ext_ref;				\
    183   1.100.2.7      yamt 	_MOWNERREF((n), (n)->m_flags);					\
    184   1.100.2.7      yamt 	MCLREFDEBUGN((n), __FILE__, __LINE__);				\
    185   1.100.2.7      yamt } while (/* CONSTCOND */ 0)
    186   1.100.2.7      yamt 
    187        1.28   thorpej /*
    188        1.68    simonb  * Initialize the mbuf allocator.
    189        1.28   thorpej  */
    190         1.4       jtc void
    191        1.62   thorpej mbinit(void)
    192         1.1       cgd {
    193        1.65   thorpej 
    194        1.65   thorpej 	KASSERT(sizeof(struct _m_ext) <= MHLEN);
    195        1.67    simonb 	KASSERT(sizeof(struct mbuf) == MSIZE);
    196        1.65   thorpej 
    197   1.100.2.6      yamt 	mclpool_allocator.pa_backingmap = mb_map;
    198  1.100.2.13      yamt 	pool_init(&mbpool, msize, 0, 0, 0, "mbpl", NULL, IPL_VM);
    199  1.100.2.13      yamt 	pool_init(&mclpool, mclbytes, 0, 0, 0, "mclpl", &mclpool_allocator,
    200  1.100.2.13      yamt 	    IPL_VM);
    201        1.53   thorpej 
    202        1.59   thorpej 	pool_set_drain_hook(&mbpool, m_reclaim, NULL);
    203        1.59   thorpej 	pool_set_drain_hook(&mclpool, m_reclaim, NULL);
    204        1.59   thorpej 
    205        1.65   thorpej 	pool_cache_init(&mbpool_cache, &mbpool, mb_ctor, NULL, NULL);
    206        1.53   thorpej 	pool_cache_init(&mclpool_cache, &mclpool, NULL, NULL, NULL);
    207        1.37   thorpej 
    208        1.37   thorpej 	/*
    209        1.39   thorpej 	 * Set the hard limit on the mclpool to the number of
    210        1.39   thorpej 	 * mbuf clusters the kernel is to support.  Log the limit
    211        1.39   thorpej 	 * reached message max once a minute.
    212        1.39   thorpej 	 */
    213        1.42   thorpej 	pool_sethardlimit(&mclpool, nmbclusters, mclpool_warnmsg, 60);
    214        1.42   thorpej 
    215        1.39   thorpej 	/*
    216        1.42   thorpej 	 * Set a low water mark for both mbufs and clusters.  This should
    217        1.42   thorpej 	 * help ensure that they can be allocated in a memory starvation
    218        1.42   thorpej 	 * situation.  This is important for e.g. diskless systems which
    219        1.42   thorpej 	 * must allocate mbufs in order for the pagedaemon to clean pages.
    220        1.37   thorpej 	 */
    221        1.42   thorpej 	pool_setlowat(&mbpool, mblowat);
    222        1.42   thorpej 	pool_setlowat(&mclpool, mcllowat);
    223        1.64      matt 
    224        1.64      matt #ifdef MBUFTRACE
    225        1.64      matt 	{
    226        1.64      matt 		/*
    227        1.64      matt 		 * Attach the unknown mowners.
    228        1.64      matt 		 */
    229        1.64      matt 		int i;
    230        1.64      matt 		MOWNER_ATTACH(&revoked_mowner);
    231        1.64      matt 		for (i = sizeof(unknown_mowners)/sizeof(unknown_mowners[0]);
    232        1.64      matt 		     i-- > 0; )
    233        1.64      matt 			MOWNER_ATTACH(&unknown_mowners[i]);
    234        1.64      matt 	}
    235        1.64      matt #endif
    236        1.42   thorpej }
    237        1.42   thorpej 
    238        1.75    atatat /*
    239        1.75    atatat  * sysctl helper routine for the kern.mbuf subtree.  nmbclusters may
    240        1.75    atatat  * or may not be writable, and mblowat and mcllowat need range
    241        1.75    atatat  * checking and pool tweaking after being reset.
    242        1.75    atatat  */
    243        1.75    atatat static int
    244        1.75    atatat sysctl_kern_mbuf(SYSCTLFN_ARGS)
    245        1.42   thorpej {
    246        1.42   thorpej 	int error, newval;
    247        1.75    atatat 	struct sysctlnode node;
    248        1.42   thorpej 
    249        1.75    atatat 	node = *rnode;
    250        1.75    atatat 	node.sysctl_data = &newval;
    251        1.75    atatat 	switch (rnode->sysctl_num) {
    252        1.42   thorpej 	case MBUF_NMBCLUSTERS:
    253        1.76    atatat 		if (mb_map != NULL) {
    254        1.80    atatat 			node.sysctl_flags &= ~CTLFLAG_READWRITE;
    255        1.80    atatat 			node.sysctl_flags |= CTLFLAG_READONLY;
    256        1.75    atatat 		}
    257        1.75    atatat 		/* FALLTHROUGH */
    258        1.42   thorpej 	case MBUF_MBLOWAT:
    259        1.42   thorpej 	case MBUF_MCLLOWAT:
    260        1.75    atatat 		newval = *(int*)rnode->sysctl_data;
    261        1.75    atatat 		break;
    262        1.75    atatat 	default:
    263        1.75    atatat 		return (EOPNOTSUPP);
    264        1.75    atatat 	}
    265        1.75    atatat 
    266        1.75    atatat 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    267        1.75    atatat 	if (error || newp == NULL)
    268        1.42   thorpej 		return (error);
    269        1.75    atatat 	if (newval < 0)
    270        1.75    atatat 		return (EINVAL);
    271        1.75    atatat 
    272        1.75    atatat 	switch (node.sysctl_num) {
    273        1.75    atatat 	case MBUF_NMBCLUSTERS:
    274        1.75    atatat 		if (newval < nmbclusters)
    275        1.75    atatat 			return (EINVAL);
    276        1.75    atatat 		nmbclusters = newval;
    277        1.75    atatat 		pool_sethardlimit(&mclpool, nmbclusters, mclpool_warnmsg, 60);
    278        1.75    atatat 		break;
    279        1.75    atatat 	case MBUF_MBLOWAT:
    280        1.75    atatat 		mblowat = newval;
    281        1.76    atatat 		pool_setlowat(&mbpool, mblowat);
    282        1.75    atatat 		break;
    283        1.75    atatat 	case MBUF_MCLLOWAT:
    284        1.76    atatat 		mcllowat = newval;
    285        1.76    atatat 		pool_setlowat(&mclpool, mcllowat);
    286        1.75    atatat 		break;
    287        1.75    atatat 	}
    288        1.75    atatat 
    289        1.75    atatat 	return (0);
    290        1.75    atatat }
    291        1.75    atatat 
    292        1.64      matt #ifdef MBUFTRACE
    293        1.75    atatat static int
    294        1.75    atatat sysctl_kern_mbuf_mowners(SYSCTLFN_ARGS)
    295        1.75    atatat {
    296        1.75    atatat 	struct mowner *mo;
    297        1.75    atatat 	size_t len = 0;
    298        1.75    atatat 	int error = 0;
    299        1.75    atatat 
    300        1.75    atatat 	if (namelen != 0)
    301        1.75    atatat 		return (EINVAL);
    302        1.75    atatat 	if (newp != NULL)
    303        1.75    atatat 		return (EPERM);
    304        1.75    atatat 
    305        1.75    atatat 	LIST_FOREACH(mo, &mowners, mo_link) {
    306        1.75    atatat 		if (oldp != NULL) {
    307        1.75    atatat 			if (*oldlenp - len < sizeof(*mo)) {
    308        1.75    atatat 				error = ENOMEM;
    309        1.75    atatat 				break;
    310        1.75    atatat 			}
    311  1.100.2.13      yamt 			error = copyout(mo, (char *)oldp + len, sizeof(*mo));
    312        1.75    atatat 			if (error)
    313        1.75    atatat 				break;
    314        1.64      matt 		}
    315        1.75    atatat 		len += sizeof(*mo);
    316        1.75    atatat 	}
    317        1.75    atatat 
    318        1.75    atatat 	if (error == 0)
    319        1.64      matt 		*oldlenp = len;
    320        1.75    atatat 
    321        1.75    atatat 	return (error);
    322        1.75    atatat }
    323        1.75    atatat #endif /* MBUFTRACE */
    324        1.75    atatat 
    325        1.75    atatat SYSCTL_SETUP(sysctl_kern_mbuf_setup, "sysctl kern.mbuf subtree setup")
    326        1.75    atatat {
    327        1.75    atatat 
    328        1.80    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    329        1.80    atatat 		       CTLFLAG_PERMANENT,
    330        1.75    atatat 		       CTLTYPE_NODE, "kern", NULL,
    331        1.75    atatat 		       NULL, 0, NULL, 0,
    332        1.75    atatat 		       CTL_KERN, CTL_EOL);
    333        1.80    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    334        1.80    atatat 		       CTLFLAG_PERMANENT,
    335        1.82    atatat 		       CTLTYPE_NODE, "mbuf",
    336        1.82    atatat 		       SYSCTL_DESCR("mbuf control variables"),
    337        1.75    atatat 		       NULL, 0, NULL, 0,
    338        1.75    atatat 		       CTL_KERN, KERN_MBUF, CTL_EOL);
    339        1.75    atatat 
    340        1.80    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    341        1.80    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    342        1.82    atatat 		       CTLTYPE_INT, "msize",
    343        1.82    atatat 		       SYSCTL_DESCR("mbuf base size"),
    344        1.75    atatat 		       NULL, msize, NULL, 0,
    345        1.75    atatat 		       CTL_KERN, KERN_MBUF, MBUF_MSIZE, CTL_EOL);
    346        1.80    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    347        1.80    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    348        1.82    atatat 		       CTLTYPE_INT, "mclbytes",
    349        1.82    atatat 		       SYSCTL_DESCR("mbuf cluster size"),
    350        1.75    atatat 		       NULL, mclbytes, NULL, 0,
    351        1.75    atatat 		       CTL_KERN, KERN_MBUF, MBUF_MCLBYTES, CTL_EOL);
    352        1.80    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    353        1.80    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    354        1.82    atatat 		       CTLTYPE_INT, "nmbclusters",
    355        1.82    atatat 		       SYSCTL_DESCR("Limit on the number of mbuf clusters"),
    356        1.75    atatat 		       sysctl_kern_mbuf, 0, &nmbclusters, 0,
    357        1.75    atatat 		       CTL_KERN, KERN_MBUF, MBUF_NMBCLUSTERS, CTL_EOL);
    358        1.80    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    359        1.80    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    360        1.82    atatat 		       CTLTYPE_INT, "mblowat",
    361        1.82    atatat 		       SYSCTL_DESCR("mbuf low water mark"),
    362        1.75    atatat 		       sysctl_kern_mbuf, 0, &mblowat, 0,
    363        1.75    atatat 		       CTL_KERN, KERN_MBUF, MBUF_MBLOWAT, CTL_EOL);
    364        1.80    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    365        1.80    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    366        1.82    atatat 		       CTLTYPE_INT, "mcllowat",
    367        1.82    atatat 		       SYSCTL_DESCR("mbuf cluster low water mark"),
    368        1.75    atatat 		       sysctl_kern_mbuf, 0, &mcllowat, 0,
    369        1.75    atatat 		       CTL_KERN, KERN_MBUF, MBUF_MCLLOWAT, CTL_EOL);
    370        1.80    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    371        1.80    atatat 		       CTLFLAG_PERMANENT,
    372        1.82    atatat 		       CTLTYPE_STRUCT, "stats",
    373        1.82    atatat 		       SYSCTL_DESCR("mbuf allocation statistics"),
    374        1.75    atatat 		       NULL, 0, &mbstat, sizeof(mbstat),
    375        1.75    atatat 		       CTL_KERN, KERN_MBUF, MBUF_STATS, CTL_EOL);
    376        1.75    atatat #ifdef MBUFTRACE
    377        1.80    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    378        1.80    atatat 		       CTLFLAG_PERMANENT,
    379        1.82    atatat 		       CTLTYPE_STRUCT, "mowners",
    380        1.82    atatat 		       SYSCTL_DESCR("Information about mbuf owners"),
    381        1.75    atatat 		       sysctl_kern_mbuf_mowners, 0, NULL, 0,
    382        1.75    atatat 		       CTL_KERN, KERN_MBUF, MBUF_MOWNERS, CTL_EOL);
    383        1.75    atatat #endif /* MBUFTRACE */
    384        1.28   thorpej }
    385        1.28   thorpej 
    386   1.100.2.6      yamt static void *
    387        1.62   thorpej mclpool_alloc(struct pool *pp, int flags)
    388        1.28   thorpej {
    389  1.100.2.12      yamt 	bool waitok = (flags & PR_WAITOK) ? true : false;
    390        1.28   thorpej 
    391        1.93      yamt 	return ((void *)uvm_km_alloc_poolpage(mb_map, waitok));
    392         1.1       cgd }
    393         1.1       cgd 
    394   1.100.2.6      yamt static void
    395        1.62   thorpej mclpool_release(struct pool *pp, void *v)
    396         1.1       cgd {
    397         1.1       cgd 
    398        1.93      yamt 	uvm_km_free_poolpage(mb_map, (vaddr_t)v);
    399        1.65   thorpej }
    400        1.65   thorpej 
    401        1.65   thorpej /*ARGSUSED*/
    402        1.65   thorpej static int
    403        1.65   thorpej mb_ctor(void *arg, void *object, int flags)
    404        1.65   thorpej {
    405        1.65   thorpej 	struct mbuf *m = object;
    406        1.65   thorpej 
    407        1.65   thorpej #ifdef POOL_VTOPHYS
    408        1.65   thorpej 	m->m_paddr = POOL_VTOPHYS(m);
    409        1.65   thorpej #else
    410        1.65   thorpej 	m->m_paddr = M_PADDR_INVALID;
    411        1.65   thorpej #endif
    412        1.65   thorpej 	return (0);
    413         1.1       cgd }
    414         1.1       cgd 
    415        1.14  christos void
    416        1.59   thorpej m_reclaim(void *arg, int flags)
    417         1.1       cgd {
    418        1.27      matt 	struct domain *dp;
    419        1.81      matt 	const struct protosw *pr;
    420        1.27      matt 	struct ifnet *ifp;
    421        1.52   thorpej 	int s = splvm();
    422         1.1       cgd 
    423        1.91      matt 	DOMAIN_FOREACH(dp) {
    424        1.33   thorpej 		for (pr = dp->dom_protosw;
    425        1.33   thorpej 		     pr < dp->dom_protoswNPROTOSW; pr++)
    426        1.33   thorpej 			if (pr->pr_drain)
    427        1.33   thorpej 				(*pr->pr_drain)();
    428        1.91      matt 	}
    429        1.92      matt 	IFNET_FOREACH(ifp) {
    430        1.27      matt 		if (ifp->if_drain)
    431        1.27      matt 			(*ifp->if_drain)(ifp);
    432        1.92      matt 	}
    433         1.1       cgd 	splx(s);
    434         1.1       cgd 	mbstat.m_drain++;
    435         1.1       cgd }
    436         1.1       cgd 
    437         1.1       cgd /*
    438         1.1       cgd  * Space allocation routines.
    439         1.1       cgd  * These are also available as macros
    440         1.1       cgd  * for critical paths.
    441         1.1       cgd  */
    442         1.1       cgd struct mbuf *
    443        1.62   thorpej m_get(int nowait, int type)
    444         1.1       cgd {
    445        1.27      matt 	struct mbuf *m;
    446         1.1       cgd 
    447         1.5       cgd 	MGET(m, nowait, type);
    448         1.1       cgd 	return (m);
    449         1.1       cgd }
    450         1.1       cgd 
    451         1.1       cgd struct mbuf *
    452        1.62   thorpej m_gethdr(int nowait, int type)
    453         1.1       cgd {
    454        1.27      matt 	struct mbuf *m;
    455         1.1       cgd 
    456         1.5       cgd 	MGETHDR(m, nowait, type);
    457         1.1       cgd 	return (m);
    458         1.1       cgd }
    459         1.1       cgd 
    460         1.1       cgd struct mbuf *
    461        1.62   thorpej m_getclr(int nowait, int type)
    462         1.1       cgd {
    463        1.27      matt 	struct mbuf *m;
    464         1.1       cgd 
    465         1.5       cgd 	MGET(m, nowait, type);
    466         1.1       cgd 	if (m == 0)
    467        1.71    simonb 		return (NULL);
    468  1.100.2.13      yamt 	memset(mtod(m, void *), 0, MLEN);
    469         1.1       cgd 	return (m);
    470         1.1       cgd }
    471         1.1       cgd 
    472        1.64      matt void
    473        1.64      matt m_clget(struct mbuf *m, int nowait)
    474        1.64      matt {
    475        1.71    simonb 
    476        1.64      matt 	MCLGET(m, nowait);
    477        1.64      matt }
    478        1.64      matt 
    479         1.1       cgd struct mbuf *
    480        1.62   thorpej m_free(struct mbuf *m)
    481         1.1       cgd {
    482        1.27      matt 	struct mbuf *n;
    483         1.1       cgd 
    484         1.1       cgd 	MFREE(m, n);
    485         1.1       cgd 	return (n);
    486         1.1       cgd }
    487         1.1       cgd 
    488         1.9   mycroft void
    489        1.62   thorpej m_freem(struct mbuf *m)
    490         1.1       cgd {
    491        1.27      matt 	struct mbuf *n;
    492         1.1       cgd 
    493         1.1       cgd 	if (m == NULL)
    494         1.1       cgd 		return;
    495         1.1       cgd 	do {
    496         1.1       cgd 		MFREE(m, n);
    497        1.18   thorpej 		m = n;
    498        1.18   thorpej 	} while (m);
    499         1.1       cgd }
    500         1.1       cgd 
    501        1.64      matt #ifdef MBUFTRACE
    502        1.83  jonathan /*
    503        1.83  jonathan  * Walk a chain of mbufs, claiming ownership of each mbuf in the chain.
    504        1.83  jonathan  */
    505        1.64      matt void
    506        1.83  jonathan m_claimm(struct mbuf *m, struct mowner *mo)
    507        1.64      matt {
    508        1.71    simonb 
    509        1.64      matt 	for (; m != NULL; m = m->m_next)
    510        1.64      matt 		MCLAIM(m, mo);
    511        1.64      matt }
    512        1.64      matt #endif
    513        1.64      matt 
    514         1.1       cgd /*
    515         1.1       cgd  * Mbuffer utility routines.
    516         1.1       cgd  */
    517         1.1       cgd 
    518         1.1       cgd /*
    519         1.1       cgd  * Lesser-used path for M_PREPEND:
    520         1.1       cgd  * allocate new mbuf to prepend to chain,
    521         1.1       cgd  * copy junk along.
    522         1.1       cgd  */
    523         1.1       cgd struct mbuf *
    524        1.62   thorpej m_prepend(struct mbuf *m, int len, int how)
    525         1.1       cgd {
    526         1.1       cgd 	struct mbuf *mn;
    527         1.1       cgd 
    528         1.9   mycroft 	MGET(mn, how, m->m_type);
    529         1.1       cgd 	if (mn == (struct mbuf *)NULL) {
    530         1.1       cgd 		m_freem(m);
    531         1.1       cgd 		return ((struct mbuf *)NULL);
    532         1.1       cgd 	}
    533         1.1       cgd 	if (m->m_flags & M_PKTHDR) {
    534   1.100.2.6      yamt 		M_MOVE_PKTHDR(mn, m);
    535        1.64      matt 	} else {
    536        1.64      matt 		MCLAIM(mn, m->m_owner);
    537         1.1       cgd 	}
    538         1.1       cgd 	mn->m_next = m;
    539         1.1       cgd 	m = mn;
    540         1.1       cgd 	if (len < MHLEN)
    541         1.1       cgd 		MH_ALIGN(m, len);
    542         1.1       cgd 	m->m_len = len;
    543         1.1       cgd 	return (m);
    544         1.1       cgd }
    545         1.1       cgd 
    546         1.1       cgd /*
    547         1.1       cgd  * Make a copy of an mbuf chain starting "off0" bytes from the beginning,
    548         1.1       cgd  * continuing for "len" bytes.  If len is M_COPYALL, copy to end of mbuf.
    549         1.1       cgd  * The wait parameter is a choice of M_WAIT/M_DONTWAIT from caller.
    550         1.1       cgd  */
    551         1.1       cgd int MCFail;
    552         1.1       cgd 
    553         1.1       cgd struct mbuf *
    554        1.62   thorpej m_copym(struct mbuf *m, int off0, int len, int wait)
    555         1.1       cgd {
    556        1.71    simonb 
    557        1.44    itojun 	return m_copym0(m, off0, len, wait, 0);	/* shallow copy on M_EXT */
    558        1.44    itojun }
    559        1.44    itojun 
    560        1.44    itojun struct mbuf *
    561        1.62   thorpej m_dup(struct mbuf *m, int off0, int len, int wait)
    562        1.44    itojun {
    563        1.71    simonb 
    564        1.44    itojun 	return m_copym0(m, off0, len, wait, 1);	/* deep copy */
    565        1.44    itojun }
    566        1.44    itojun 
    567        1.44    itojun static struct mbuf *
    568        1.62   thorpej m_copym0(struct mbuf *m, int off0, int len, int wait, int deep)
    569        1.44    itojun {
    570        1.27      matt 	struct mbuf *n, **np;
    571        1.27      matt 	int off = off0;
    572         1.1       cgd 	struct mbuf *top;
    573         1.1       cgd 	int copyhdr = 0;
    574         1.1       cgd 
    575         1.1       cgd 	if (off < 0 || len < 0)
    576        1.43   thorpej 		panic("m_copym: off %d, len %d", off, len);
    577         1.1       cgd 	if (off == 0 && m->m_flags & M_PKTHDR)
    578         1.1       cgd 		copyhdr = 1;
    579         1.1       cgd 	while (off > 0) {
    580         1.1       cgd 		if (m == 0)
    581        1.90      matt 			panic("m_copym: m == 0, off %d", off);
    582         1.1       cgd 		if (off < m->m_len)
    583         1.1       cgd 			break;
    584         1.1       cgd 		off -= m->m_len;
    585         1.1       cgd 		m = m->m_next;
    586         1.1       cgd 	}
    587         1.1       cgd 	np = &top;
    588         1.1       cgd 	top = 0;
    589         1.1       cgd 	while (len > 0) {
    590         1.1       cgd 		if (m == 0) {
    591         1.1       cgd 			if (len != M_COPYALL)
    592        1.90      matt 				panic("m_copym: m == 0, len %d [!COPYALL]",
    593        1.90      matt 				    len);
    594         1.1       cgd 			break;
    595         1.1       cgd 		}
    596         1.1       cgd 		MGET(n, wait, m->m_type);
    597         1.1       cgd 		*np = n;
    598         1.1       cgd 		if (n == 0)
    599         1.1       cgd 			goto nospace;
    600        1.64      matt 		MCLAIM(n, m->m_owner);
    601         1.1       cgd 		if (copyhdr) {
    602         1.1       cgd 			M_COPY_PKTHDR(n, m);
    603         1.1       cgd 			if (len == M_COPYALL)
    604         1.1       cgd 				n->m_pkthdr.len -= off0;
    605         1.1       cgd 			else
    606         1.1       cgd 				n->m_pkthdr.len = len;
    607         1.1       cgd 			copyhdr = 0;
    608         1.1       cgd 		}
    609         1.9   mycroft 		n->m_len = min(len, m->m_len - off);
    610         1.1       cgd 		if (m->m_flags & M_EXT) {
    611        1.44    itojun 			if (!deep) {
    612        1.44    itojun 				n->m_data = m->m_data + off;
    613        1.44    itojun 				MCLADDREFERENCE(m, n);
    614        1.44    itojun 			} else {
    615        1.48    itojun 				/*
    616        1.50    itojun 				 * we are unsure about the way m was allocated.
    617        1.50    itojun 				 * copy into multiple MCLBYTES cluster mbufs.
    618        1.48    itojun 				 */
    619        1.44    itojun 				MCLGET(n, wait);
    620        1.50    itojun 				n->m_len = 0;
    621        1.50    itojun 				n->m_len = M_TRAILINGSPACE(n);
    622        1.50    itojun 				n->m_len = min(n->m_len, len);
    623        1.50    itojun 				n->m_len = min(n->m_len, m->m_len - off);
    624  1.100.2.13      yamt 				memcpy(mtod(n, void *), mtod(m, char *) + off,
    625        1.44    itojun 				    (unsigned)n->m_len);
    626        1.44    itojun 			}
    627         1.1       cgd 		} else
    628  1.100.2.13      yamt 			memcpy(mtod(n, void *), mtod(m, char *) + off,
    629         1.1       cgd 			    (unsigned)n->m_len);
    630         1.1       cgd 		if (len != M_COPYALL)
    631         1.1       cgd 			len -= n->m_len;
    632        1.50    itojun 		off += n->m_len;
    633        1.50    itojun #ifdef DIAGNOSTIC
    634        1.50    itojun 		if (off > m->m_len)
    635        1.50    itojun 			panic("m_copym0 overrun");
    636        1.50    itojun #endif
    637        1.50    itojun 		if (off == m->m_len) {
    638        1.50    itojun 			m = m->m_next;
    639        1.50    itojun 			off = 0;
    640        1.50    itojun 		}
    641         1.1       cgd 		np = &n->m_next;
    642         1.1       cgd 	}
    643         1.1       cgd 	if (top == 0)
    644         1.1       cgd 		MCFail++;
    645         1.1       cgd 	return (top);
    646         1.1       cgd nospace:
    647         1.1       cgd 	m_freem(top);
    648         1.1       cgd 	MCFail++;
    649        1.71    simonb 	return (NULL);
    650         1.1       cgd }
    651         1.1       cgd 
    652         1.1       cgd /*
    653        1.18   thorpej  * Copy an entire packet, including header (which must be present).
    654        1.18   thorpej  * An optimization of the common case `m_copym(m, 0, M_COPYALL, how)'.
    655        1.18   thorpej  */
    656        1.18   thorpej struct mbuf *
    657        1.62   thorpej m_copypacket(struct mbuf *m, int how)
    658        1.18   thorpej {
    659        1.18   thorpej 	struct mbuf *top, *n, *o;
    660        1.18   thorpej 
    661        1.18   thorpej 	MGET(n, how, m->m_type);
    662        1.18   thorpej 	top = n;
    663        1.18   thorpej 	if (!n)
    664        1.18   thorpej 		goto nospace;
    665        1.18   thorpej 
    666        1.64      matt 	MCLAIM(n, m->m_owner);
    667        1.18   thorpej 	M_COPY_PKTHDR(n, m);
    668        1.18   thorpej 	n->m_len = m->m_len;
    669        1.18   thorpej 	if (m->m_flags & M_EXT) {
    670        1.18   thorpej 		n->m_data = m->m_data;
    671        1.18   thorpej 		MCLADDREFERENCE(m, n);
    672        1.18   thorpej 	} else {
    673        1.30     perry 		memcpy(mtod(n, char *), mtod(m, char *), n->m_len);
    674        1.18   thorpej 	}
    675        1.18   thorpej 
    676        1.18   thorpej 	m = m->m_next;
    677        1.18   thorpej 	while (m) {
    678        1.18   thorpej 		MGET(o, how, m->m_type);
    679        1.18   thorpej 		if (!o)
    680        1.18   thorpej 			goto nospace;
    681        1.18   thorpej 
    682        1.64      matt 		MCLAIM(o, m->m_owner);
    683        1.18   thorpej 		n->m_next = o;
    684        1.18   thorpej 		n = n->m_next;
    685        1.18   thorpej 
    686        1.18   thorpej 		n->m_len = m->m_len;
    687        1.18   thorpej 		if (m->m_flags & M_EXT) {
    688        1.18   thorpej 			n->m_data = m->m_data;
    689        1.18   thorpej 			MCLADDREFERENCE(m, n);
    690        1.18   thorpej 		} else {
    691        1.30     perry 			memcpy(mtod(n, char *), mtod(m, char *), n->m_len);
    692        1.18   thorpej 		}
    693        1.18   thorpej 
    694        1.18   thorpej 		m = m->m_next;
    695        1.18   thorpej 	}
    696        1.18   thorpej 	return top;
    697        1.18   thorpej nospace:
    698        1.18   thorpej 	m_freem(top);
    699        1.18   thorpej 	MCFail++;
    700        1.71    simonb 	return NULL;
    701        1.18   thorpej }
    702        1.18   thorpej 
    703        1.18   thorpej /*
    704         1.1       cgd  * Copy data from an mbuf chain starting "off" bytes from the beginning,
    705         1.1       cgd  * continuing for "len" bytes, into the indicated buffer.
    706         1.1       cgd  */
    707        1.14  christos void
    708       1.100    martin m_copydata(struct mbuf *m, int off, int len, void *vp)
    709         1.1       cgd {
    710        1.94      tron 	unsigned	count;
    711  1.100.2.13      yamt 	void *		cp = vp;
    712         1.1       cgd 
    713         1.1       cgd 	if (off < 0 || len < 0)
    714        1.90      matt 		panic("m_copydata: off %d, len %d", off, len);
    715         1.1       cgd 	while (off > 0) {
    716        1.94      tron 		if (m == NULL)
    717        1.94      tron 			panic("m_copydata: m == NULL, off %d", off);
    718         1.1       cgd 		if (off < m->m_len)
    719         1.1       cgd 			break;
    720         1.1       cgd 		off -= m->m_len;
    721         1.1       cgd 		m = m->m_next;
    722         1.1       cgd 	}
    723         1.1       cgd 	while (len > 0) {
    724        1.94      tron 		if (m == NULL)
    725        1.94      tron 			panic("m_copydata: m == NULL, len %d", len);
    726         1.9   mycroft 		count = min(m->m_len - off, len);
    727  1.100.2.13      yamt 		memcpy(cp, mtod(m, char *) + off, count);
    728         1.1       cgd 		len -= count;
    729  1.100.2.13      yamt 		cp = (char *)cp + count;
    730         1.1       cgd 		off = 0;
    731         1.1       cgd 		m = m->m_next;
    732         1.1       cgd 	}
    733         1.1       cgd }
    734         1.1       cgd 
    735         1.1       cgd /*
    736         1.1       cgd  * Concatenate mbuf chain n to m.
    737        1.72    itojun  * n might be copied into m (when n->m_len is small), therefore data portion of
    738        1.72    itojun  * n could be copied into an mbuf of different mbuf type.
    739         1.1       cgd  * Any m_pkthdr is not updated.
    740         1.1       cgd  */
    741        1.14  christos void
    742        1.62   thorpej m_cat(struct mbuf *m, struct mbuf *n)
    743         1.1       cgd {
    744        1.73      yamt 
    745         1.1       cgd 	while (m->m_next)
    746         1.1       cgd 		m = m->m_next;
    747         1.1       cgd 	while (n) {
    748        1.77    itojun 		if (M_READONLY(m) || n->m_len > M_TRAILINGSPACE(m)) {
    749         1.1       cgd 			/* just join the two chains */
    750         1.1       cgd 			m->m_next = n;
    751         1.1       cgd 			return;
    752         1.1       cgd 		}
    753         1.1       cgd 		/* splat the data from one into the other */
    754  1.100.2.13      yamt 		memcpy(mtod(m, char *) + m->m_len, mtod(n, void *),
    755         1.1       cgd 		    (u_int)n->m_len);
    756         1.1       cgd 		m->m_len += n->m_len;
    757         1.1       cgd 		n = m_free(n);
    758         1.1       cgd 	}
    759         1.1       cgd }
    760         1.1       cgd 
    761        1.11   mycroft void
    762        1.62   thorpej m_adj(struct mbuf *mp, int req_len)
    763         1.1       cgd {
    764        1.27      matt 	int len = req_len;
    765        1.27      matt 	struct mbuf *m;
    766        1.27      matt 	int count;
    767         1.1       cgd 
    768         1.1       cgd 	if ((m = mp) == NULL)
    769         1.1       cgd 		return;
    770         1.1       cgd 	if (len >= 0) {
    771         1.1       cgd 		/*
    772         1.1       cgd 		 * Trim from head.
    773         1.1       cgd 		 */
    774         1.1       cgd 		while (m != NULL && len > 0) {
    775         1.1       cgd 			if (m->m_len <= len) {
    776         1.1       cgd 				len -= m->m_len;
    777         1.1       cgd 				m->m_len = 0;
    778         1.1       cgd 				m = m->m_next;
    779         1.1       cgd 			} else {
    780         1.1       cgd 				m->m_len -= len;
    781         1.1       cgd 				m->m_data += len;
    782         1.1       cgd 				len = 0;
    783         1.1       cgd 			}
    784         1.1       cgd 		}
    785         1.1       cgd 		m = mp;
    786         1.1       cgd 		if (mp->m_flags & M_PKTHDR)
    787         1.1       cgd 			m->m_pkthdr.len -= (req_len - len);
    788         1.1       cgd 	} else {
    789         1.1       cgd 		/*
    790         1.1       cgd 		 * Trim from tail.  Scan the mbuf chain,
    791         1.1       cgd 		 * calculating its length and finding the last mbuf.
    792         1.1       cgd 		 * If the adjustment only affects this mbuf, then just
    793         1.1       cgd 		 * adjust and return.  Otherwise, rescan and truncate
    794         1.1       cgd 		 * after the remaining size.
    795         1.1       cgd 		 */
    796         1.1       cgd 		len = -len;
    797         1.1       cgd 		count = 0;
    798         1.1       cgd 		for (;;) {
    799         1.1       cgd 			count += m->m_len;
    800         1.1       cgd 			if (m->m_next == (struct mbuf *)0)
    801         1.1       cgd 				break;
    802         1.1       cgd 			m = m->m_next;
    803         1.1       cgd 		}
    804         1.1       cgd 		if (m->m_len >= len) {
    805         1.1       cgd 			m->m_len -= len;
    806         1.8   deraadt 			if (mp->m_flags & M_PKTHDR)
    807         1.8   deraadt 				mp->m_pkthdr.len -= len;
    808         1.1       cgd 			return;
    809         1.1       cgd 		}
    810         1.1       cgd 		count -= len;
    811         1.1       cgd 		if (count < 0)
    812         1.1       cgd 			count = 0;
    813         1.1       cgd 		/*
    814         1.1       cgd 		 * Correct length for chain is "count".
    815         1.1       cgd 		 * Find the mbuf with last data, adjust its length,
    816         1.1       cgd 		 * and toss data from remaining mbufs on chain.
    817         1.1       cgd 		 */
    818         1.1       cgd 		m = mp;
    819         1.1       cgd 		if (m->m_flags & M_PKTHDR)
    820         1.1       cgd 			m->m_pkthdr.len = count;
    821         1.1       cgd 		for (; m; m = m->m_next) {
    822         1.1       cgd 			if (m->m_len >= count) {
    823         1.1       cgd 				m->m_len = count;
    824         1.1       cgd 				break;
    825         1.1       cgd 			}
    826         1.1       cgd 			count -= m->m_len;
    827         1.1       cgd 		}
    828   1.100.2.6      yamt 		if (m)
    829   1.100.2.6      yamt 			while (m->m_next)
    830   1.100.2.6      yamt 				(m = m->m_next)->m_len = 0;
    831         1.1       cgd 	}
    832         1.1       cgd }
    833         1.1       cgd 
    834         1.1       cgd /*
    835   1.100.2.6      yamt  * Rearrange an mbuf chain so that len bytes are contiguous
    836         1.1       cgd  * and in the data area of an mbuf (so that mtod and dtom
    837         1.1       cgd  * will work for a structure of size len).  Returns the resulting
    838         1.1       cgd  * mbuf chain on success, frees it and returns null on failure.
    839         1.1       cgd  * If there is room, it will add up to max_protohdr-len extra bytes to the
    840         1.1       cgd  * contiguous region in an attempt to avoid being called next time.
    841         1.1       cgd  */
    842         1.1       cgd int MPFail;
    843         1.1       cgd 
    844         1.1       cgd struct mbuf *
    845        1.62   thorpej m_pullup(struct mbuf *n, int len)
    846         1.1       cgd {
    847        1.27      matt 	struct mbuf *m;
    848        1.27      matt 	int count;
    849         1.1       cgd 	int space;
    850         1.1       cgd 
    851         1.1       cgd 	/*
    852         1.1       cgd 	 * If first mbuf has no cluster, and has room for len bytes
    853         1.1       cgd 	 * without shifting current data, pullup into it,
    854         1.1       cgd 	 * otherwise allocate a new mbuf to prepend to the chain.
    855         1.1       cgd 	 */
    856         1.1       cgd 	if ((n->m_flags & M_EXT) == 0 &&
    857         1.1       cgd 	    n->m_data + len < &n->m_dat[MLEN] && n->m_next) {
    858         1.1       cgd 		if (n->m_len >= len)
    859         1.1       cgd 			return (n);
    860         1.1       cgd 		m = n;
    861         1.1       cgd 		n = n->m_next;
    862         1.1       cgd 		len -= m->m_len;
    863         1.1       cgd 	} else {
    864         1.1       cgd 		if (len > MHLEN)
    865         1.1       cgd 			goto bad;
    866         1.1       cgd 		MGET(m, M_DONTWAIT, n->m_type);
    867         1.1       cgd 		if (m == 0)
    868         1.1       cgd 			goto bad;
    869        1.64      matt 		MCLAIM(m, n->m_owner);
    870         1.1       cgd 		m->m_len = 0;
    871         1.1       cgd 		if (n->m_flags & M_PKTHDR) {
    872   1.100.2.6      yamt 			M_MOVE_PKTHDR(m, n);
    873         1.1       cgd 		}
    874         1.1       cgd 	}
    875         1.1       cgd 	space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
    876         1.1       cgd 	do {
    877         1.1       cgd 		count = min(min(max(len, max_protohdr), space), n->m_len);
    878  1.100.2.13      yamt 		memcpy(mtod(m, char *) + m->m_len, mtod(n, void *),
    879         1.1       cgd 		  (unsigned)count);
    880         1.1       cgd 		len -= count;
    881         1.1       cgd 		m->m_len += count;
    882         1.1       cgd 		n->m_len -= count;
    883         1.1       cgd 		space -= count;
    884         1.1       cgd 		if (n->m_len)
    885         1.1       cgd 			n->m_data += count;
    886         1.1       cgd 		else
    887         1.1       cgd 			n = m_free(n);
    888         1.1       cgd 	} while (len > 0 && n);
    889         1.1       cgd 	if (len > 0) {
    890         1.1       cgd 		(void) m_free(m);
    891         1.1       cgd 		goto bad;
    892         1.1       cgd 	}
    893         1.1       cgd 	m->m_next = n;
    894         1.1       cgd 	return (m);
    895         1.1       cgd bad:
    896         1.1       cgd 	m_freem(n);
    897         1.1       cgd 	MPFail++;
    898        1.71    simonb 	return (NULL);
    899        1.60   thorpej }
    900        1.60   thorpej 
    901        1.60   thorpej /*
    902        1.60   thorpej  * Like m_pullup(), except a new mbuf is always allocated, and we allow
    903        1.60   thorpej  * the amount of empty space before the data in the new mbuf to be specified
    904        1.60   thorpej  * (in the event that the caller expects to prepend later).
    905        1.60   thorpej  */
    906        1.60   thorpej int MSFail;
    907        1.60   thorpej 
    908        1.60   thorpej struct mbuf *
    909        1.60   thorpej m_copyup(struct mbuf *n, int len, int dstoff)
    910        1.60   thorpej {
    911        1.60   thorpej 	struct mbuf *m;
    912        1.60   thorpej 	int count, space;
    913        1.60   thorpej 
    914        1.60   thorpej 	if (len > (MHLEN - dstoff))
    915        1.60   thorpej 		goto bad;
    916        1.60   thorpej 	MGET(m, M_DONTWAIT, n->m_type);
    917        1.60   thorpej 	if (m == NULL)
    918        1.60   thorpej 		goto bad;
    919        1.64      matt 	MCLAIM(m, n->m_owner);
    920        1.60   thorpej 	m->m_len = 0;
    921        1.60   thorpej 	if (n->m_flags & M_PKTHDR) {
    922   1.100.2.6      yamt 		M_MOVE_PKTHDR(m, n);
    923        1.60   thorpej 	}
    924        1.60   thorpej 	m->m_data += dstoff;
    925        1.60   thorpej 	space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
    926        1.60   thorpej 	do {
    927        1.60   thorpej 		count = min(min(max(len, max_protohdr), space), n->m_len);
    928  1.100.2.13      yamt 		memcpy(mtod(m, char *) + m->m_len, mtod(n, void *),
    929        1.60   thorpej 		    (unsigned)count);
    930        1.60   thorpej 		len -= count;
    931        1.60   thorpej 		m->m_len += count;
    932        1.60   thorpej 		n->m_len -= count;
    933        1.60   thorpej 		space -= count;
    934        1.60   thorpej 		if (n->m_len)
    935        1.60   thorpej 			n->m_data += count;
    936        1.60   thorpej 		else
    937        1.60   thorpej 			n = m_free(n);
    938        1.60   thorpej 	} while (len > 0 && n);
    939        1.60   thorpej 	if (len > 0) {
    940        1.60   thorpej 		(void) m_free(m);
    941        1.60   thorpej 		goto bad;
    942        1.60   thorpej 	}
    943        1.60   thorpej 	m->m_next = n;
    944        1.60   thorpej 	return (m);
    945        1.60   thorpej  bad:
    946        1.60   thorpej 	m_freem(n);
    947        1.60   thorpej 	MSFail++;
    948        1.60   thorpej 	return (NULL);
    949         1.9   mycroft }
    950         1.9   mycroft 
    951         1.9   mycroft /*
    952         1.9   mycroft  * Partition an mbuf chain in two pieces, returning the tail --
    953         1.9   mycroft  * all but the first len0 bytes.  In case of failure, it returns NULL and
    954         1.9   mycroft  * attempts to restore the chain to its original state.
    955         1.9   mycroft  */
    956         1.9   mycroft struct mbuf *
    957        1.62   thorpej m_split(struct mbuf *m0, int len0, int wait)
    958         1.9   mycroft {
    959        1.85      yamt 
    960        1.87      yamt 	return m_split0(m0, len0, wait, 1);
    961        1.85      yamt }
    962        1.85      yamt 
    963        1.85      yamt static struct mbuf *
    964        1.85      yamt m_split0(struct mbuf *m0, int len0, int wait, int copyhdr)
    965        1.85      yamt {
    966        1.27      matt 	struct mbuf *m, *n;
    967        1.22   thorpej 	unsigned len = len0, remain, len_save;
    968         1.9   mycroft 
    969         1.9   mycroft 	for (m = m0; m && len > m->m_len; m = m->m_next)
    970         1.9   mycroft 		len -= m->m_len;
    971         1.9   mycroft 	if (m == 0)
    972        1.71    simonb 		return (NULL);
    973         1.9   mycroft 	remain = m->m_len - len;
    974        1.85      yamt 	if (copyhdr && (m0->m_flags & M_PKTHDR)) {
    975         1.9   mycroft 		MGETHDR(n, wait, m0->m_type);
    976         1.9   mycroft 		if (n == 0)
    977        1.71    simonb 			return (NULL);
    978  1.100.2.11      yamt 		MCLAIM(n, m0->m_owner);
    979         1.9   mycroft 		n->m_pkthdr.rcvif = m0->m_pkthdr.rcvif;
    980         1.9   mycroft 		n->m_pkthdr.len = m0->m_pkthdr.len - len0;
    981        1.22   thorpej 		len_save = m0->m_pkthdr.len;
    982         1.9   mycroft 		m0->m_pkthdr.len = len0;
    983         1.9   mycroft 		if (m->m_flags & M_EXT)
    984         1.9   mycroft 			goto extpacket;
    985         1.9   mycroft 		if (remain > MHLEN) {
    986         1.9   mycroft 			/* m can't be the lead packet */
    987         1.9   mycroft 			MH_ALIGN(n, 0);
    988         1.9   mycroft 			n->m_next = m_split(m, len, wait);
    989         1.9   mycroft 			if (n->m_next == 0) {
    990         1.9   mycroft 				(void) m_free(n);
    991        1.22   thorpej 				m0->m_pkthdr.len = len_save;
    992        1.71    simonb 				return (NULL);
    993         1.9   mycroft 			} else
    994         1.9   mycroft 				return (n);
    995         1.9   mycroft 		} else
    996         1.9   mycroft 			MH_ALIGN(n, remain);
    997         1.9   mycroft 	} else if (remain == 0) {
    998         1.9   mycroft 		n = m->m_next;
    999         1.9   mycroft 		m->m_next = 0;
   1000         1.9   mycroft 		return (n);
   1001         1.9   mycroft 	} else {
   1002         1.9   mycroft 		MGET(n, wait, m->m_type);
   1003         1.9   mycroft 		if (n == 0)
   1004        1.71    simonb 			return (NULL);
   1005        1.64      matt 		MCLAIM(n, m->m_owner);
   1006         1.9   mycroft 		M_ALIGN(n, remain);
   1007         1.9   mycroft 	}
   1008         1.9   mycroft extpacket:
   1009         1.9   mycroft 	if (m->m_flags & M_EXT) {
   1010         1.9   mycroft 		n->m_data = m->m_data + len;
   1011   1.100.2.1      yamt 		MCLADDREFERENCE(m, n);
   1012         1.9   mycroft 	} else {
   1013  1.100.2.13      yamt 		memcpy(mtod(n, void *), mtod(m, char *) + len, remain);
   1014         1.9   mycroft 	}
   1015         1.9   mycroft 	n->m_len = remain;
   1016         1.9   mycroft 	m->m_len = len;
   1017         1.9   mycroft 	n->m_next = m->m_next;
   1018         1.9   mycroft 	m->m_next = 0;
   1019         1.9   mycroft 	return (n);
   1020         1.9   mycroft }
   1021         1.9   mycroft /*
   1022         1.9   mycroft  * Routine to copy from device local memory into mbufs.
   1023         1.9   mycroft  */
   1024         1.9   mycroft struct mbuf *
   1025        1.62   thorpej m_devget(char *buf, int totlen, int off0, struct ifnet *ifp,
   1026        1.62   thorpej     void (*copy)(const void *from, void *to, size_t len))
   1027         1.9   mycroft {
   1028        1.27      matt 	struct mbuf *m;
   1029         1.9   mycroft 	struct mbuf *top = 0, **mp = &top;
   1030        1.27      matt 	int off = off0, len;
   1031        1.27      matt 	char *cp;
   1032         1.9   mycroft 	char *epkt;
   1033         1.9   mycroft 
   1034         1.9   mycroft 	cp = buf;
   1035         1.9   mycroft 	epkt = cp + totlen;
   1036         1.9   mycroft 	if (off) {
   1037        1.13       cgd 		/*
   1038        1.13       cgd 		 * If 'off' is non-zero, packet is trailer-encapsulated,
   1039        1.13       cgd 		 * so we have to skip the type and length fields.
   1040        1.13       cgd 		 */
   1041   1.100.2.6      yamt 		cp += off + 2 * sizeof(uint16_t);
   1042   1.100.2.6      yamt 		totlen -= 2 * sizeof(uint16_t);
   1043         1.9   mycroft 	}
   1044         1.9   mycroft 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   1045         1.9   mycroft 	if (m == 0)
   1046        1.71    simonb 		return (NULL);
   1047         1.9   mycroft 	m->m_pkthdr.rcvif = ifp;
   1048         1.9   mycroft 	m->m_pkthdr.len = totlen;
   1049         1.9   mycroft 	m->m_len = MHLEN;
   1050         1.9   mycroft 
   1051         1.9   mycroft 	while (totlen > 0) {
   1052         1.9   mycroft 		if (top) {
   1053         1.9   mycroft 			MGET(m, M_DONTWAIT, MT_DATA);
   1054         1.9   mycroft 			if (m == 0) {
   1055         1.9   mycroft 				m_freem(top);
   1056        1.71    simonb 				return (NULL);
   1057         1.9   mycroft 			}
   1058         1.9   mycroft 			m->m_len = MLEN;
   1059         1.9   mycroft 		}
   1060         1.9   mycroft 		len = min(totlen, epkt - cp);
   1061         1.9   mycroft 		if (len >= MINCLSIZE) {
   1062         1.9   mycroft 			MCLGET(m, M_DONTWAIT);
   1063        1.19   mycroft 			if ((m->m_flags & M_EXT) == 0) {
   1064        1.20   mycroft 				m_free(m);
   1065        1.19   mycroft 				m_freem(top);
   1066        1.71    simonb 				return (NULL);
   1067        1.19   mycroft 			}
   1068        1.19   mycroft 			m->m_len = len = min(len, MCLBYTES);
   1069         1.9   mycroft 		} else {
   1070         1.9   mycroft 			/*
   1071         1.9   mycroft 			 * Place initial small packet/header at end of mbuf.
   1072         1.9   mycroft 			 */
   1073         1.9   mycroft 			if (len < m->m_len) {
   1074         1.9   mycroft 				if (top == 0 && len + max_linkhdr <= m->m_len)
   1075         1.9   mycroft 					m->m_data += max_linkhdr;
   1076         1.9   mycroft 				m->m_len = len;
   1077         1.9   mycroft 			} else
   1078         1.9   mycroft 				len = m->m_len;
   1079         1.9   mycroft 		}
   1080         1.9   mycroft 		if (copy)
   1081  1.100.2.13      yamt 			copy(cp, mtod(m, void *), (size_t)len);
   1082         1.9   mycroft 		else
   1083  1.100.2.13      yamt 			memcpy(mtod(m, void *), cp, (size_t)len);
   1084         1.9   mycroft 		cp += len;
   1085         1.9   mycroft 		*mp = m;
   1086         1.9   mycroft 		mp = &m->m_next;
   1087         1.9   mycroft 		totlen -= len;
   1088         1.9   mycroft 		if (cp == epkt)
   1089         1.9   mycroft 			cp = buf;
   1090         1.9   mycroft 	}
   1091         1.9   mycroft 	return (top);
   1092        1.18   thorpej }
   1093        1.18   thorpej 
   1094        1.18   thorpej /*
   1095        1.18   thorpej  * Copy data from a buffer back into the indicated mbuf chain,
   1096        1.18   thorpej  * starting "off" bytes from the beginning, extending the mbuf
   1097        1.18   thorpej  * chain if necessary.
   1098        1.18   thorpej  */
   1099        1.18   thorpej void
   1100        1.86      yamt m_copyback(struct mbuf *m0, int off, int len, const void *cp)
   1101        1.18   thorpej {
   1102        1.85      yamt #if defined(DEBUG)
   1103        1.85      yamt 	struct mbuf *origm = m0;
   1104        1.85      yamt 	int error;
   1105        1.85      yamt #endif /* defined(DEBUG) */
   1106        1.85      yamt 
   1107        1.85      yamt 	if (m0 == NULL)
   1108        1.85      yamt 		return;
   1109        1.85      yamt 
   1110        1.85      yamt #if defined(DEBUG)
   1111        1.85      yamt 	error =
   1112        1.85      yamt #endif /* defined(DEBUG) */
   1113        1.85      yamt 	m_copyback0(&m0, off, len, cp,
   1114        1.85      yamt 	    M_COPYBACK0_COPYBACK|M_COPYBACK0_EXTEND, M_DONTWAIT);
   1115        1.85      yamt 
   1116        1.85      yamt #if defined(DEBUG)
   1117        1.85      yamt 	if (error != 0 || (m0 != NULL && origm != m0))
   1118        1.85      yamt 		panic("m_copyback");
   1119        1.85      yamt #endif /* defined(DEBUG) */
   1120        1.85      yamt }
   1121        1.85      yamt 
   1122        1.85      yamt struct mbuf *
   1123        1.86      yamt m_copyback_cow(struct mbuf *m0, int off, int len, const void *cp, int how)
   1124        1.85      yamt {
   1125        1.85      yamt 	int error;
   1126        1.85      yamt 
   1127        1.85      yamt 	/* don't support chain expansion */
   1128        1.85      yamt 	KDASSERT(off + len <= m_length(m0));
   1129        1.85      yamt 
   1130        1.85      yamt 	error = m_copyback0(&m0, off, len, cp,
   1131        1.85      yamt 	    M_COPYBACK0_COPYBACK|M_COPYBACK0_COW, how);
   1132        1.85      yamt 	if (error) {
   1133        1.85      yamt 		/*
   1134        1.85      yamt 		 * no way to recover from partial success.
   1135        1.85      yamt 		 * just free the chain.
   1136        1.85      yamt 		 */
   1137        1.85      yamt 		m_freem(m0);
   1138        1.85      yamt 		return NULL;
   1139        1.85      yamt 	}
   1140        1.85      yamt 	return m0;
   1141        1.85      yamt }
   1142        1.85      yamt 
   1143        1.85      yamt /*
   1144        1.85      yamt  * m_makewritable: ensure the specified range writable.
   1145        1.85      yamt  */
   1146        1.85      yamt int
   1147        1.85      yamt m_makewritable(struct mbuf **mp, int off, int len, int how)
   1148        1.85      yamt {
   1149        1.85      yamt 	int error;
   1150        1.85      yamt #if defined(DEBUG)
   1151        1.85      yamt 	struct mbuf *n;
   1152        1.85      yamt 	int origlen, reslen;
   1153        1.85      yamt 
   1154        1.85      yamt 	origlen = m_length(*mp);
   1155        1.85      yamt #endif /* defined(DEBUG) */
   1156        1.85      yamt 
   1157        1.85      yamt #if 0 /* M_COPYALL is large enough */
   1158        1.85      yamt 	if (len == M_COPYALL)
   1159        1.85      yamt 		len = m_length(*mp) - off; /* XXX */
   1160        1.85      yamt #endif
   1161        1.85      yamt 
   1162        1.85      yamt 	error = m_copyback0(mp, off, len, NULL,
   1163        1.85      yamt 	    M_COPYBACK0_PRESERVE|M_COPYBACK0_COW, how);
   1164        1.85      yamt 
   1165        1.85      yamt #if defined(DEBUG)
   1166        1.85      yamt 	reslen = 0;
   1167        1.85      yamt 	for (n = *mp; n; n = n->m_next)
   1168        1.85      yamt 		reslen += n->m_len;
   1169        1.85      yamt 	if (origlen != reslen)
   1170        1.85      yamt 		panic("m_makewritable: length changed");
   1171        1.85      yamt 	if (((*mp)->m_flags & M_PKTHDR) != 0 && reslen != (*mp)->m_pkthdr.len)
   1172        1.85      yamt 		panic("m_makewritable: inconsist");
   1173        1.85      yamt #endif /* defined(DEBUG) */
   1174        1.85      yamt 
   1175        1.85      yamt 	return error;
   1176        1.85      yamt }
   1177        1.85      yamt 
   1178        1.85      yamt int
   1179        1.86      yamt m_copyback0(struct mbuf **mp0, int off, int len, const void *vp, int flags,
   1180        1.86      yamt     int how)
   1181        1.85      yamt {
   1182        1.27      matt 	int mlen;
   1183        1.85      yamt 	struct mbuf *m, *n;
   1184        1.85      yamt 	struct mbuf **mp;
   1185        1.18   thorpej 	int totlen = 0;
   1186        1.86      yamt 	const char *cp = vp;
   1187        1.18   thorpej 
   1188        1.85      yamt 	KASSERT(mp0 != NULL);
   1189        1.85      yamt 	KASSERT(*mp0 != NULL);
   1190        1.85      yamt 	KASSERT((flags & M_COPYBACK0_PRESERVE) == 0 || cp == NULL);
   1191        1.85      yamt 	KASSERT((flags & M_COPYBACK0_COPYBACK) == 0 || cp != NULL);
   1192        1.85      yamt 
   1193   1.100.2.6      yamt 	/*
   1194   1.100.2.6      yamt 	 * we don't bother to update "totlen" in the case of M_COPYBACK0_COW,
   1195   1.100.2.6      yamt 	 * assuming that M_COPYBACK0_EXTEND and M_COPYBACK0_COW are exclusive.
   1196   1.100.2.6      yamt 	 */
   1197   1.100.2.6      yamt 
   1198   1.100.2.6      yamt 	KASSERT((~flags & (M_COPYBACK0_EXTEND|M_COPYBACK0_COW)) != 0);
   1199   1.100.2.6      yamt 
   1200        1.85      yamt 	mp = mp0;
   1201        1.85      yamt 	m = *mp;
   1202        1.18   thorpej 	while (off > (mlen = m->m_len)) {
   1203        1.18   thorpej 		off -= mlen;
   1204        1.18   thorpej 		totlen += mlen;
   1205   1.100.2.6      yamt 		if (m->m_next == NULL) {
   1206   1.100.2.6      yamt 			int tspace;
   1207   1.100.2.6      yamt extend:
   1208        1.85      yamt 			if ((flags & M_COPYBACK0_EXTEND) == 0)
   1209        1.85      yamt 				goto out;
   1210   1.100.2.6      yamt 
   1211   1.100.2.6      yamt 			/*
   1212   1.100.2.6      yamt 			 * try to make some space at the end of "m".
   1213   1.100.2.6      yamt 			 */
   1214   1.100.2.6      yamt 
   1215   1.100.2.6      yamt 			mlen = m->m_len;
   1216   1.100.2.6      yamt 			if (off + len >= MINCLSIZE &&
   1217   1.100.2.6      yamt 			    (m->m_flags & M_EXT) == 0 && m->m_len == 0) {
   1218   1.100.2.6      yamt 				MCLGET(m, how);
   1219   1.100.2.6      yamt 			}
   1220   1.100.2.6      yamt 			tspace = M_TRAILINGSPACE(m);
   1221   1.100.2.6      yamt 			if (tspace > 0) {
   1222   1.100.2.6      yamt 				tspace = min(tspace, off + len);
   1223   1.100.2.6      yamt 				KASSERT(tspace > 0);
   1224   1.100.2.6      yamt 				memset(mtod(m, char *) + m->m_len, 0,
   1225   1.100.2.6      yamt 				    min(off, tspace));
   1226   1.100.2.6      yamt 				m->m_len += tspace;
   1227   1.100.2.6      yamt 				off += mlen;
   1228   1.100.2.6      yamt 				totlen -= mlen;
   1229   1.100.2.6      yamt 				continue;
   1230   1.100.2.6      yamt 			}
   1231   1.100.2.6      yamt 
   1232   1.100.2.6      yamt 			/*
   1233   1.100.2.6      yamt 			 * need to allocate an mbuf.
   1234   1.100.2.6      yamt 			 */
   1235   1.100.2.6      yamt 
   1236   1.100.2.6      yamt 			if (off + len >= MINCLSIZE) {
   1237   1.100.2.6      yamt 				n = m_getcl(how, m->m_type, 0);
   1238   1.100.2.6      yamt 			} else {
   1239   1.100.2.6      yamt 				n = m_get(how, m->m_type);
   1240   1.100.2.6      yamt 			}
   1241   1.100.2.6      yamt 			if (n == NULL) {
   1242        1.18   thorpej 				goto out;
   1243   1.100.2.6      yamt 			}
   1244   1.100.2.6      yamt 			n->m_len = 0;
   1245   1.100.2.6      yamt 			n->m_len = min(M_TRAILINGSPACE(n), off + len);
   1246   1.100.2.6      yamt 			memset(mtod(n, char *), 0, min(n->m_len, off));
   1247        1.18   thorpej 			m->m_next = n;
   1248        1.18   thorpej 		}
   1249        1.85      yamt 		mp = &m->m_next;
   1250        1.18   thorpej 		m = m->m_next;
   1251        1.18   thorpej 	}
   1252        1.18   thorpej 	while (len > 0) {
   1253        1.85      yamt 		mlen = m->m_len - off;
   1254        1.85      yamt 		if (mlen != 0 && M_READONLY(m)) {
   1255        1.85      yamt 			char *datap;
   1256        1.85      yamt 			int eatlen;
   1257        1.85      yamt 
   1258        1.85      yamt 			/*
   1259        1.85      yamt 			 * this mbuf is read-only.
   1260        1.85      yamt 			 * allocate a new writable mbuf and try again.
   1261        1.85      yamt 			 */
   1262        1.85      yamt 
   1263        1.85      yamt #if defined(DIAGNOSTIC)
   1264        1.85      yamt 			if ((flags & M_COPYBACK0_COW) == 0)
   1265        1.85      yamt 				panic("m_copyback0: read-only");
   1266        1.85      yamt #endif /* defined(DIAGNOSTIC) */
   1267        1.85      yamt 
   1268        1.85      yamt 			/*
   1269        1.85      yamt 			 * if we're going to write into the middle of
   1270        1.85      yamt 			 * a mbuf, split it first.
   1271        1.85      yamt 			 */
   1272        1.85      yamt 			if (off > 0 && len < mlen) {
   1273        1.85      yamt 				n = m_split0(m, off, how, 0);
   1274        1.85      yamt 				if (n == NULL)
   1275        1.85      yamt 					goto enobufs;
   1276        1.85      yamt 				m->m_next = n;
   1277        1.85      yamt 				mp = &m->m_next;
   1278        1.85      yamt 				m = n;
   1279        1.85      yamt 				off = 0;
   1280        1.85      yamt 				continue;
   1281        1.85      yamt 			}
   1282        1.85      yamt 
   1283        1.85      yamt 			/*
   1284        1.85      yamt 			 * XXX TODO coalesce into the trailingspace of
   1285        1.85      yamt 			 * the previous mbuf when possible.
   1286        1.85      yamt 			 */
   1287        1.85      yamt 
   1288        1.85      yamt 			/*
   1289        1.85      yamt 			 * allocate a new mbuf.  copy packet header if needed.
   1290        1.85      yamt 			 */
   1291        1.85      yamt 			MGET(n, how, m->m_type);
   1292        1.85      yamt 			if (n == NULL)
   1293        1.85      yamt 				goto enobufs;
   1294        1.85      yamt 			MCLAIM(n, m->m_owner);
   1295        1.85      yamt 			if (off == 0 && (m->m_flags & M_PKTHDR) != 0) {
   1296   1.100.2.6      yamt 				M_MOVE_PKTHDR(n, m);
   1297        1.85      yamt 				n->m_len = MHLEN;
   1298        1.85      yamt 			} else {
   1299        1.85      yamt 				if (len >= MINCLSIZE)
   1300        1.85      yamt 					MCLGET(n, M_DONTWAIT);
   1301        1.85      yamt 				n->m_len =
   1302        1.85      yamt 				    (n->m_flags & M_EXT) ? MCLBYTES : MLEN;
   1303        1.85      yamt 			}
   1304        1.85      yamt 			if (n->m_len > len)
   1305        1.85      yamt 				n->m_len = len;
   1306        1.85      yamt 
   1307        1.85      yamt 			/*
   1308        1.85      yamt 			 * free the region which has been overwritten.
   1309        1.85      yamt 			 * copying data from old mbufs if requested.
   1310        1.85      yamt 			 */
   1311        1.85      yamt 			if (flags & M_COPYBACK0_PRESERVE)
   1312        1.85      yamt 				datap = mtod(n, char *);
   1313        1.85      yamt 			else
   1314        1.85      yamt 				datap = NULL;
   1315        1.85      yamt 			eatlen = n->m_len;
   1316        1.85      yamt 			KDASSERT(off == 0 || eatlen >= mlen);
   1317        1.85      yamt 			if (off > 0) {
   1318        1.85      yamt 				KDASSERT(len >= mlen);
   1319        1.85      yamt 				m->m_len = off;
   1320        1.85      yamt 				m->m_next = n;
   1321        1.85      yamt 				if (datap) {
   1322        1.85      yamt 					m_copydata(m, off, mlen, datap);
   1323        1.85      yamt 					datap += mlen;
   1324        1.85      yamt 				}
   1325        1.85      yamt 				eatlen -= mlen;
   1326        1.85      yamt 				mp = &m->m_next;
   1327        1.85      yamt 				m = m->m_next;
   1328        1.85      yamt 			}
   1329        1.85      yamt 			while (m != NULL && M_READONLY(m) &&
   1330        1.85      yamt 			    n->m_type == m->m_type && eatlen > 0) {
   1331        1.85      yamt 				mlen = min(eatlen, m->m_len);
   1332        1.85      yamt 				if (datap) {
   1333        1.85      yamt 					m_copydata(m, 0, mlen, datap);
   1334        1.85      yamt 					datap += mlen;
   1335        1.85      yamt 				}
   1336        1.85      yamt 				m->m_data += mlen;
   1337        1.85      yamt 				m->m_len -= mlen;
   1338        1.85      yamt 				eatlen -= mlen;
   1339        1.85      yamt 				if (m->m_len == 0)
   1340        1.85      yamt 					*mp = m = m_free(m);
   1341        1.85      yamt 			}
   1342        1.85      yamt 			if (eatlen > 0)
   1343        1.85      yamt 				n->m_len -= eatlen;
   1344        1.85      yamt 			n->m_next = m;
   1345        1.85      yamt 			*mp = m = n;
   1346        1.85      yamt 			continue;
   1347        1.85      yamt 		}
   1348        1.85      yamt 		mlen = min(mlen, len);
   1349        1.85      yamt 		if (flags & M_COPYBACK0_COPYBACK) {
   1350  1.100.2.13      yamt 			memcpy(mtod(m, char *) + off, cp, (unsigned)mlen);
   1351        1.85      yamt 			cp += mlen;
   1352        1.85      yamt 		}
   1353        1.18   thorpej 		len -= mlen;
   1354        1.18   thorpej 		mlen += off;
   1355        1.18   thorpej 		off = 0;
   1356        1.18   thorpej 		totlen += mlen;
   1357        1.18   thorpej 		if (len == 0)
   1358        1.18   thorpej 			break;
   1359   1.100.2.6      yamt 		if (m->m_next == NULL) {
   1360   1.100.2.6      yamt 			goto extend;
   1361        1.18   thorpej 		}
   1362        1.85      yamt 		mp = &m->m_next;
   1363        1.18   thorpej 		m = m->m_next;
   1364        1.18   thorpej 	}
   1365   1.100.2.6      yamt out:	if (((m = *mp0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen)) {
   1366   1.100.2.6      yamt 		KASSERT((flags & M_COPYBACK0_EXTEND) != 0);
   1367        1.18   thorpej 		m->m_pkthdr.len = totlen;
   1368   1.100.2.6      yamt 	}
   1369        1.85      yamt 
   1370        1.85      yamt 	return 0;
   1371        1.85      yamt 
   1372        1.85      yamt enobufs:
   1373        1.85      yamt 	return ENOBUFS;
   1374        1.66   thorpej }
   1375        1.66   thorpej 
   1376   1.100.2.6      yamt void
   1377   1.100.2.6      yamt m_move_pkthdr(struct mbuf *to, struct mbuf *from)
   1378   1.100.2.6      yamt {
   1379   1.100.2.6      yamt 
   1380   1.100.2.6      yamt 	KASSERT((to->m_flags & M_EXT) == 0);
   1381   1.100.2.6      yamt 	KASSERT((to->m_flags & M_PKTHDR) == 0 || m_tag_first(to) == NULL);
   1382   1.100.2.6      yamt 	KASSERT((from->m_flags & M_PKTHDR) != 0);
   1383   1.100.2.6      yamt 
   1384   1.100.2.6      yamt 	to->m_pkthdr = from->m_pkthdr;
   1385   1.100.2.6      yamt 	to->m_flags = from->m_flags & M_COPYFLAGS;
   1386   1.100.2.6      yamt 	to->m_data = to->m_pktdat;
   1387   1.100.2.6      yamt 
   1388   1.100.2.6      yamt 	from->m_flags &= ~M_PKTHDR;
   1389   1.100.2.6      yamt }
   1390   1.100.2.6      yamt 
   1391        1.66   thorpej /*
   1392        1.66   thorpej  * Apply function f to the data in an mbuf chain starting "off" bytes from the
   1393        1.66   thorpej  * beginning, continuing for "len" bytes.
   1394        1.66   thorpej  */
   1395        1.66   thorpej int
   1396        1.66   thorpej m_apply(struct mbuf *m, int off, int len,
   1397  1.100.2.13      yamt     int (*f)(void *, void *, unsigned int), void *arg)
   1398        1.66   thorpej {
   1399        1.66   thorpej 	unsigned int count;
   1400        1.66   thorpej 	int rval;
   1401        1.66   thorpej 
   1402        1.66   thorpej 	KASSERT(len >= 0);
   1403        1.66   thorpej 	KASSERT(off >= 0);
   1404        1.66   thorpej 
   1405        1.66   thorpej 	while (off > 0) {
   1406        1.66   thorpej 		KASSERT(m != NULL);
   1407        1.66   thorpej 		if (off < m->m_len)
   1408        1.66   thorpej 			break;
   1409        1.66   thorpej 		off -= m->m_len;
   1410        1.66   thorpej 		m = m->m_next;
   1411        1.66   thorpej 	}
   1412        1.66   thorpej 	while (len > 0) {
   1413        1.66   thorpej 		KASSERT(m != NULL);
   1414        1.66   thorpej 		count = min(m->m_len - off, len);
   1415        1.66   thorpej 
   1416  1.100.2.13      yamt 		rval = (*f)(arg, mtod(m, char *) + off, count);
   1417        1.66   thorpej 		if (rval)
   1418        1.66   thorpej 			return (rval);
   1419        1.66   thorpej 
   1420        1.66   thorpej 		len -= count;
   1421        1.66   thorpej 		off = 0;
   1422        1.66   thorpej 		m = m->m_next;
   1423        1.66   thorpej 	}
   1424        1.66   thorpej 
   1425        1.66   thorpej 	return (0);
   1426        1.66   thorpej }
   1427        1.66   thorpej 
   1428        1.66   thorpej /*
   1429        1.66   thorpej  * Return a pointer to mbuf/offset of location in mbuf chain.
   1430        1.66   thorpej  */
   1431        1.66   thorpej struct mbuf *
   1432        1.66   thorpej m_getptr(struct mbuf *m, int loc, int *off)
   1433        1.66   thorpej {
   1434        1.66   thorpej 
   1435        1.66   thorpej 	while (loc >= 0) {
   1436        1.66   thorpej 		/* Normal end of search */
   1437        1.66   thorpej 		if (m->m_len > loc) {
   1438        1.66   thorpej 	    		*off = loc;
   1439        1.66   thorpej 	    		return (m);
   1440        1.66   thorpej 		} else {
   1441        1.66   thorpej 	    		loc -= m->m_len;
   1442        1.66   thorpej 
   1443        1.66   thorpej 	    		if (m->m_next == NULL) {
   1444        1.66   thorpej 				if (loc == 0) {
   1445        1.66   thorpej  					/* Point at the end of valid data */
   1446        1.66   thorpej 		    			*off = m->m_len;
   1447        1.66   thorpej 		    			return (m);
   1448        1.66   thorpej 				} else
   1449        1.66   thorpej 		  			return (NULL);
   1450        1.66   thorpej 	    		} else
   1451        1.66   thorpej 	      			m = m->m_next;
   1452        1.66   thorpej 		}
   1453        1.66   thorpej     	}
   1454        1.66   thorpej 
   1455        1.66   thorpej 	return (NULL);
   1456         1.1       cgd }
   1457   1.100.2.2      yamt 
   1458   1.100.2.2      yamt /*
   1459   1.100.2.2      yamt  * m_ext_free: release a reference to the mbuf external storage.
   1460   1.100.2.2      yamt  *
   1461   1.100.2.2      yamt  * => free the mbuf m itsself as well.
   1462   1.100.2.2      yamt  * => called at splvm.
   1463   1.100.2.2      yamt  */
   1464   1.100.2.2      yamt 
   1465   1.100.2.2      yamt void
   1466   1.100.2.2      yamt m_ext_free(struct mbuf *m)
   1467   1.100.2.2      yamt {
   1468  1.100.2.12      yamt 	bool embedded = MEXT_ISEMBEDDED(m);
   1469  1.100.2.12      yamt 	bool dofree = true;
   1470   1.100.2.2      yamt 
   1471   1.100.2.2      yamt 	KASSERT((m->m_flags & M_EXT) != 0);
   1472   1.100.2.2      yamt 	KASSERT(MEXT_ISEMBEDDED(m->m_ext_ref));
   1473   1.100.2.2      yamt 	KASSERT((m->m_ext_ref->m_flags & M_EXT) != 0);
   1474   1.100.2.2      yamt 	KASSERT((m->m_flags & M_EXT_CLUSTER) ==
   1475   1.100.2.2      yamt 	    (m->m_ext_ref->m_flags & M_EXT_CLUSTER));
   1476   1.100.2.2      yamt 
   1477  1.100.2.10      yamt 	if (!mcl_dec_and_test_reference(m)) {
   1478   1.100.2.2      yamt 		if (embedded) {
   1479  1.100.2.15      yamt 			/*
   1480  1.100.2.15      yamt 			 * other mbuf's m_ext_ref still points to us.
   1481  1.100.2.15      yamt 			 */
   1482  1.100.2.12      yamt 			dofree = false;
   1483   1.100.2.2      yamt 		} else {
   1484   1.100.2.2      yamt 			m->m_ext_ref = m;
   1485   1.100.2.2      yamt 		}
   1486   1.100.2.2      yamt 	} else {
   1487   1.100.2.2      yamt 		/*
   1488   1.100.2.2      yamt 		 * dropping the last reference
   1489   1.100.2.2      yamt 		 */
   1490   1.100.2.2      yamt 		if (!embedded) {
   1491  1.100.2.10      yamt 			mcl_inc_reference(m); /* XXX */
   1492   1.100.2.2      yamt 			m_ext_free(m->m_ext_ref);
   1493   1.100.2.2      yamt 			m->m_ext_ref = m;
   1494   1.100.2.2      yamt 		} else if ((m->m_flags & M_EXT_CLUSTER) != 0) {
   1495   1.100.2.6      yamt 			pool_cache_put_paddr((struct pool_cache *)
   1496   1.100.2.6      yamt 			    m->m_ext.ext_arg,
   1497   1.100.2.2      yamt 			    m->m_ext.ext_buf, m->m_ext.ext_paddr);
   1498   1.100.2.2      yamt 		} else if (m->m_ext.ext_free) {
   1499  1.100.2.15      yamt 			mutex_destroy(&m->m_ext.ext_lock);
   1500   1.100.2.3      yamt 			(*m->m_ext.ext_free)(m,
   1501   1.100.2.2      yamt 			    m->m_ext.ext_buf, m->m_ext.ext_size,
   1502   1.100.2.2      yamt 			    m->m_ext.ext_arg);
   1503   1.100.2.2      yamt 			/*
   1504   1.100.2.2      yamt 			 * 'm' is already freed by the ext_free callback.
   1505   1.100.2.2      yamt 			 */
   1506  1.100.2.12      yamt 			dofree = false;
   1507   1.100.2.2      yamt 		} else {
   1508   1.100.2.2      yamt 			free(m->m_ext.ext_buf, m->m_ext.ext_type);
   1509   1.100.2.2      yamt 		}
   1510   1.100.2.2      yamt 	}
   1511   1.100.2.2      yamt 	if (dofree) {
   1512  1.100.2.15      yamt 		if (embedded) {
   1513  1.100.2.15      yamt 			mutex_destroy(&m->m_ext.ext_lock);
   1514  1.100.2.15      yamt 		}
   1515   1.100.2.2      yamt 		pool_cache_put(&mbpool_cache, m);
   1516   1.100.2.2      yamt 	}
   1517   1.100.2.2      yamt }
   1518   1.100.2.3      yamt 
   1519   1.100.2.4      yamt #if defined(__HAVE_LAZY_MBUF) || defined(DEBUG)
   1520  1.100.2.14      yamt char *
   1521   1.100.2.3      yamt m_mapin(struct mbuf *m)
   1522   1.100.2.3      yamt {
   1523   1.100.2.5      yamt 
   1524  1.100.2.15      yamt #if defined(__HAVE_LAZY_MBUF)
   1525   1.100.2.3      yamt 	KASSERT((~m->m_flags & (M_EXT|M_EXT_PAGES|M_EXT_LAZY)) == 0);
   1526   1.100.2.3      yamt 
   1527   1.100.2.3      yamt 	MEXT_LOCK(m);
   1528   1.100.2.3      yamt 	if (m->m_ext.ext_flags & M_EXT_LAZY) {
   1529   1.100.2.3      yamt 		vaddr_t buf = (vaddr_t)m->m_ext.ext_buf;
   1530   1.100.2.3      yamt 		vsize_t size = (vsize_t)m->m_ext.ext_size;
   1531   1.100.2.3      yamt 		vaddr_t va, sva, eva;
   1532   1.100.2.3      yamt 		int i;
   1533   1.100.2.3      yamt 
   1534   1.100.2.3      yamt 		sva = trunc_page(buf);
   1535   1.100.2.3      yamt 		eva = round_page(buf + size);
   1536   1.100.2.3      yamt 
   1537   1.100.2.3      yamt 		for (i = 0, va = sva; va < eva; i++, va += PAGE_SIZE) {
   1538   1.100.2.3      yamt 			pmap_kenter_pa(va, VM_PAGE_TO_PHYS(m->m_ext.ext_pgs[i]),
   1539   1.100.2.3      yamt 			    VM_PROT_READ);
   1540   1.100.2.3      yamt 		}
   1541   1.100.2.3      yamt 		pmap_update(pmap_kernel());
   1542   1.100.2.3      yamt 		m->m_ext.ext_flags &= ~M_EXT_LAZY;
   1543   1.100.2.3      yamt 	}
   1544   1.100.2.3      yamt 	MEXT_UNLOCK(m);
   1545   1.100.2.3      yamt 
   1546   1.100.2.3      yamt 	m->m_flags &= ~M_EXT_LAZY;
   1547   1.100.2.3      yamt 	return m->m_data;
   1548   1.100.2.4      yamt #else /* defined(__HAVE_LAZY_MBUF) */
   1549   1.100.2.4      yamt 	panic("m_mapin");
   1550   1.100.2.4      yamt #endif /* defined(__HAVE_LAZY_MBUF) */
   1551   1.100.2.3      yamt }
   1552   1.100.2.4      yamt #endif /* defined(__HAVE_LAZY_MBUF) || defined(DEBUG) */
   1553   1.100.2.6      yamt 
   1554   1.100.2.6      yamt #if defined(DDB)
   1555   1.100.2.6      yamt void
   1556   1.100.2.6      yamt m_print(const struct mbuf *m, const char *modif, void (*pr)(const char *, ...))
   1557   1.100.2.6      yamt {
   1558   1.100.2.6      yamt 	char ch;
   1559  1.100.2.12      yamt 	bool opt_c = false;
   1560   1.100.2.6      yamt 	char buf[512];
   1561   1.100.2.6      yamt 
   1562   1.100.2.6      yamt 	while ((ch = *(modif++)) != '\0') {
   1563   1.100.2.6      yamt 		switch (ch) {
   1564   1.100.2.6      yamt 		case 'c':
   1565  1.100.2.12      yamt 			opt_c = true;
   1566   1.100.2.6      yamt 			break;
   1567   1.100.2.6      yamt 		}
   1568   1.100.2.6      yamt 	}
   1569   1.100.2.6      yamt 
   1570   1.100.2.6      yamt nextchain:
   1571   1.100.2.6      yamt 	(*pr)("MBUF %p\n", m);
   1572   1.100.2.6      yamt 	bitmask_snprintf(m->m_flags, M_FLAGS_BITS, buf, sizeof(buf));
   1573   1.100.2.6      yamt 	(*pr)("  data=%p, len=%d, type=%d, flags=0x%s\n",
   1574   1.100.2.6      yamt 	    m->m_data, m->m_len, m->m_type, buf);
   1575   1.100.2.6      yamt 	(*pr)("  owner=%p, next=%p, nextpkt=%p\n", m->m_owner, m->m_next,
   1576   1.100.2.6      yamt 	    m->m_nextpkt);
   1577   1.100.2.6      yamt 	(*pr)("  leadingspace=%u, trailingspace=%u, readonly=%u\n",
   1578   1.100.2.6      yamt 	    (int)M_LEADINGSPACE(m), (int)M_TRAILINGSPACE(m),
   1579   1.100.2.6      yamt 	    (int)M_READONLY(m));
   1580   1.100.2.6      yamt 	if ((m->m_flags & M_PKTHDR) != 0) {
   1581   1.100.2.6      yamt 		bitmask_snprintf(m->m_pkthdr.csum_flags, M_CSUM_BITS, buf,
   1582   1.100.2.6      yamt 		    sizeof(buf));
   1583   1.100.2.6      yamt 		(*pr)("  pktlen=%d, rcvif=%p, csum_flags=0x%s, csum_data=0x%"
   1584   1.100.2.6      yamt 		    PRIx32 ", segsz=%u\n",
   1585   1.100.2.6      yamt 		    m->m_pkthdr.len, m->m_pkthdr.rcvif,
   1586   1.100.2.6      yamt 		    buf, m->m_pkthdr.csum_data, m->m_pkthdr.segsz);
   1587   1.100.2.6      yamt 	}
   1588   1.100.2.6      yamt 	if ((m->m_flags & M_EXT)) {
   1589   1.100.2.9      yamt 		(*pr)("  ext_refcnt=%u, ext_buf=%p, ext_size=%zd, "
   1590   1.100.2.6      yamt 		    "ext_free=%p, ext_arg=%p\n",
   1591   1.100.2.9      yamt 		    m->m_ext.ext_refcnt,
   1592   1.100.2.6      yamt 		    m->m_ext.ext_buf, m->m_ext.ext_size,
   1593   1.100.2.6      yamt 		    m->m_ext.ext_free, m->m_ext.ext_arg);
   1594   1.100.2.6      yamt 	}
   1595   1.100.2.6      yamt 	if ((~m->m_flags & (M_EXT|M_EXT_PAGES)) == 0) {
   1596   1.100.2.6      yamt 		vaddr_t sva = (vaddr_t)m->m_ext.ext_buf;
   1597   1.100.2.6      yamt 		vaddr_t eva = sva + m->m_ext.ext_size;
   1598   1.100.2.6      yamt 		int n = (round_page(eva) - trunc_page(sva)) >> PAGE_SHIFT;
   1599   1.100.2.6      yamt 		int i;
   1600   1.100.2.6      yamt 
   1601   1.100.2.6      yamt 		(*pr)("  pages:");
   1602   1.100.2.6      yamt 		for (i = 0; i < n; i ++) {
   1603   1.100.2.6      yamt 			(*pr)(" %p", m->m_ext.ext_pgs[i]);
   1604   1.100.2.6      yamt 		}
   1605   1.100.2.6      yamt 		(*pr)("\n");
   1606   1.100.2.6      yamt 	}
   1607   1.100.2.6      yamt 
   1608   1.100.2.6      yamt 	if (opt_c) {
   1609   1.100.2.6      yamt 		m = m->m_next;
   1610   1.100.2.6      yamt 		if (m != NULL) {
   1611   1.100.2.6      yamt 			goto nextchain;
   1612   1.100.2.6      yamt 		}
   1613   1.100.2.6      yamt 	}
   1614   1.100.2.6      yamt }
   1615   1.100.2.6      yamt #endif /* defined(DDB) */
   1616