Home | History | Annotate | Line # | Download | only in kern
uipc_mbuf.c revision 1.69.2.6
      1  1.69.2.6     skrll /*	$NetBSD: uipc_mbuf.c,v 1.69.2.6 2005/01/24 08:35:36 skrll 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.69.2.1     skrll  * 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.69.2.6     skrll __KERNEL_RCSID(0, "$NetBSD: uipc_mbuf.c,v 1.69.2.6 2005/01/24 08:35:36 skrll Exp $");
     73      1.69    martin 
     74      1.69    martin #include "opt_mbuftrace.h"
     75      1.24       mrg 
     76       1.6   mycroft #include <sys/param.h>
     77       1.6   mycroft #include <sys/systm.h>
     78       1.6   mycroft #include <sys/proc.h>
     79       1.6   mycroft #include <sys/malloc.h>
     80       1.1       cgd #define MBTYPES
     81       1.6   mycroft #include <sys/mbuf.h>
     82       1.6   mycroft #include <sys/kernel.h>
     83       1.6   mycroft #include <sys/syslog.h>
     84       1.6   mycroft #include <sys/domain.h>
     85       1.6   mycroft #include <sys/protosw.h>
     86      1.28   thorpej #include <sys/pool.h>
     87      1.27      matt #include <sys/socket.h>
     88      1.55    simonb #include <sys/sysctl.h>
     89      1.55    simonb 
     90      1.27      matt #include <net/if.h>
     91      1.14  christos 
     92      1.65   thorpej #include <uvm/uvm.h>
     93      1.23       mrg 
     94      1.42   thorpej 
     95      1.28   thorpej struct	pool mbpool;		/* mbuf pool */
     96      1.28   thorpej struct	pool mclpool;		/* mbuf cluster pool */
     97      1.28   thorpej 
     98      1.53   thorpej struct pool_cache mbpool_cache;
     99      1.53   thorpej struct pool_cache mclpool_cache;
    100      1.53   thorpej 
    101      1.18   thorpej struct mbstat mbstat;
    102      1.18   thorpej int	max_linkhdr;
    103      1.18   thorpej int	max_protohdr;
    104      1.18   thorpej int	max_hdr;
    105      1.18   thorpej int	max_datalen;
    106      1.18   thorpej 
    107      1.65   thorpej static int mb_ctor(void *, void *, int);
    108      1.65   thorpej 
    109      1.62   thorpej void	*mclpool_alloc(struct pool *, int);
    110      1.62   thorpej void	mclpool_release(struct pool *, void *);
    111      1.58   thorpej 
    112      1.58   thorpej struct pool_allocator mclpool_allocator = {
    113      1.58   thorpej 	mclpool_alloc, mclpool_release, 0,
    114      1.58   thorpej };
    115      1.58   thorpej 
    116  1.69.2.1     skrll static struct mbuf *m_copym0(struct mbuf *, int, int, int, int);
    117  1.69.2.2     skrll static struct mbuf *m_split0(struct mbuf *, int, int, int);
    118  1.69.2.2     skrll static int m_copyback0(struct mbuf **, int, int, const void *, int, int);
    119  1.69.2.2     skrll 
    120  1.69.2.2     skrll /* flags for m_copyback0 */
    121  1.69.2.2     skrll #define	M_COPYBACK0_COPYBACK	0x0001	/* copyback from cp */
    122  1.69.2.2     skrll #define	M_COPYBACK0_PRESERVE	0x0002	/* preserve original data */
    123  1.69.2.2     skrll #define	M_COPYBACK0_COW		0x0004	/* do copy-on-write */
    124  1.69.2.2     skrll #define	M_COPYBACK0_EXTEND	0x0008	/* extend chain */
    125      1.28   thorpej 
    126      1.57   thorpej const char mclpool_warnmsg[] =
    127      1.42   thorpej     "WARNING: mclpool limit reached; increase NMBCLUSTERS";
    128      1.63   thorpej 
    129      1.63   thorpej MALLOC_DEFINE(M_MBUF, "mbuf", "mbuf");
    130      1.42   thorpej 
    131      1.64      matt #ifdef MBUFTRACE
    132      1.64      matt struct mownerhead mowners = LIST_HEAD_INITIALIZER(mowners);
    133      1.64      matt struct mowner unknown_mowners[] = {
    134      1.64      matt 	{ "unknown", "free" },
    135      1.64      matt 	{ "unknown", "data" },
    136      1.64      matt 	{ "unknown", "header" },
    137      1.64      matt 	{ "unknown", "soname" },
    138      1.64      matt 	{ "unknown", "soopts" },
    139      1.64      matt 	{ "unknown", "ftable" },
    140      1.64      matt 	{ "unknown", "control" },
    141      1.64      matt 	{ "unknown", "oobdata" },
    142      1.64      matt };
    143      1.64      matt struct mowner revoked_mowner = { "revoked", "" };
    144      1.64      matt #endif
    145      1.64      matt 
    146      1.28   thorpej /*
    147      1.68    simonb  * Initialize the mbuf allocator.
    148      1.28   thorpej  */
    149       1.4       jtc void
    150      1.62   thorpej mbinit(void)
    151       1.1       cgd {
    152      1.65   thorpej 
    153      1.65   thorpej 	KASSERT(sizeof(struct _m_ext) <= MHLEN);
    154      1.67    simonb 	KASSERT(sizeof(struct mbuf) == MSIZE);
    155      1.65   thorpej 
    156      1.58   thorpej 	pool_init(&mbpool, msize, 0, 0, 0, "mbpl", NULL);
    157      1.58   thorpej 	pool_init(&mclpool, mclbytes, 0, 0, 0, "mclpl", &mclpool_allocator);
    158      1.53   thorpej 
    159      1.59   thorpej 	pool_set_drain_hook(&mbpool, m_reclaim, NULL);
    160      1.59   thorpej 	pool_set_drain_hook(&mclpool, m_reclaim, NULL);
    161      1.59   thorpej 
    162      1.65   thorpej 	pool_cache_init(&mbpool_cache, &mbpool, mb_ctor, NULL, NULL);
    163      1.53   thorpej 	pool_cache_init(&mclpool_cache, &mclpool, NULL, NULL, NULL);
    164      1.37   thorpej 
    165      1.37   thorpej 	/*
    166      1.39   thorpej 	 * Set the hard limit on the mclpool to the number of
    167      1.39   thorpej 	 * mbuf clusters the kernel is to support.  Log the limit
    168      1.39   thorpej 	 * reached message max once a minute.
    169      1.39   thorpej 	 */
    170      1.42   thorpej 	pool_sethardlimit(&mclpool, nmbclusters, mclpool_warnmsg, 60);
    171      1.42   thorpej 
    172      1.39   thorpej 	/*
    173      1.42   thorpej 	 * Set a low water mark for both mbufs and clusters.  This should
    174      1.42   thorpej 	 * help ensure that they can be allocated in a memory starvation
    175      1.42   thorpej 	 * situation.  This is important for e.g. diskless systems which
    176      1.42   thorpej 	 * must allocate mbufs in order for the pagedaemon to clean pages.
    177      1.37   thorpej 	 */
    178      1.42   thorpej 	pool_setlowat(&mbpool, mblowat);
    179      1.42   thorpej 	pool_setlowat(&mclpool, mcllowat);
    180      1.64      matt 
    181      1.64      matt #ifdef MBUFTRACE
    182      1.64      matt 	{
    183      1.64      matt 		/*
    184      1.64      matt 		 * Attach the unknown mowners.
    185      1.64      matt 		 */
    186      1.64      matt 		int i;
    187      1.64      matt 		MOWNER_ATTACH(&revoked_mowner);
    188      1.64      matt 		for (i = sizeof(unknown_mowners)/sizeof(unknown_mowners[0]);
    189      1.64      matt 		     i-- > 0; )
    190      1.64      matt 			MOWNER_ATTACH(&unknown_mowners[i]);
    191      1.64      matt 	}
    192      1.64      matt #endif
    193      1.42   thorpej }
    194      1.42   thorpej 
    195  1.69.2.1     skrll /*
    196  1.69.2.1     skrll  * sysctl helper routine for the kern.mbuf subtree.  nmbclusters may
    197  1.69.2.1     skrll  * or may not be writable, and mblowat and mcllowat need range
    198  1.69.2.1     skrll  * checking and pool tweaking after being reset.
    199  1.69.2.1     skrll  */
    200  1.69.2.1     skrll static int
    201  1.69.2.1     skrll sysctl_kern_mbuf(SYSCTLFN_ARGS)
    202      1.42   thorpej {
    203      1.42   thorpej 	int error, newval;
    204  1.69.2.1     skrll 	struct sysctlnode node;
    205      1.42   thorpej 
    206  1.69.2.1     skrll 	node = *rnode;
    207  1.69.2.1     skrll 	node.sysctl_data = &newval;
    208  1.69.2.1     skrll 	switch (rnode->sysctl_num) {
    209      1.42   thorpej 	case MBUF_NMBCLUSTERS:
    210  1.69.2.1     skrll 		if (mb_map != NULL) {
    211  1.69.2.1     skrll 			node.sysctl_flags &= ~CTLFLAG_READWRITE;
    212  1.69.2.1     skrll 			node.sysctl_flags |= CTLFLAG_READONLY;
    213  1.69.2.1     skrll 		}
    214  1.69.2.1     skrll 		/* FALLTHROUGH */
    215      1.42   thorpej 	case MBUF_MBLOWAT:
    216      1.42   thorpej 	case MBUF_MCLLOWAT:
    217  1.69.2.1     skrll 		newval = *(int*)rnode->sysctl_data;
    218  1.69.2.1     skrll 		break;
    219  1.69.2.1     skrll 	default:
    220  1.69.2.1     skrll 		return (EOPNOTSUPP);
    221  1.69.2.1     skrll 	}
    222  1.69.2.1     skrll 
    223  1.69.2.1     skrll 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    224  1.69.2.1     skrll 	if (error || newp == NULL)
    225      1.42   thorpej 		return (error);
    226  1.69.2.1     skrll 	if (newval < 0)
    227  1.69.2.1     skrll 		return (EINVAL);
    228  1.69.2.1     skrll 
    229  1.69.2.1     skrll 	switch (node.sysctl_num) {
    230  1.69.2.1     skrll 	case MBUF_NMBCLUSTERS:
    231  1.69.2.1     skrll 		if (newval < nmbclusters)
    232  1.69.2.1     skrll 			return (EINVAL);
    233  1.69.2.1     skrll 		nmbclusters = newval;
    234  1.69.2.1     skrll 		pool_sethardlimit(&mclpool, nmbclusters, mclpool_warnmsg, 60);
    235  1.69.2.1     skrll 		break;
    236  1.69.2.1     skrll 	case MBUF_MBLOWAT:
    237  1.69.2.1     skrll 		mblowat = newval;
    238  1.69.2.1     skrll 		pool_setlowat(&mbpool, mblowat);
    239  1.69.2.1     skrll 		break;
    240  1.69.2.1     skrll 	case MBUF_MCLLOWAT:
    241  1.69.2.1     skrll 		mcllowat = newval;
    242  1.69.2.1     skrll 		pool_setlowat(&mclpool, mcllowat);
    243  1.69.2.1     skrll 		break;
    244  1.69.2.1     skrll 	}
    245  1.69.2.1     skrll 
    246  1.69.2.1     skrll 	return (0);
    247  1.69.2.1     skrll }
    248  1.69.2.1     skrll 
    249      1.64      matt #ifdef MBUFTRACE
    250  1.69.2.1     skrll static int
    251  1.69.2.1     skrll sysctl_kern_mbuf_mowners(SYSCTLFN_ARGS)
    252  1.69.2.1     skrll {
    253  1.69.2.1     skrll 	struct mowner *mo;
    254  1.69.2.1     skrll 	size_t len = 0;
    255  1.69.2.1     skrll 	int error = 0;
    256  1.69.2.1     skrll 
    257  1.69.2.1     skrll 	if (namelen != 0)
    258  1.69.2.1     skrll 		return (EINVAL);
    259  1.69.2.1     skrll 	if (newp != NULL)
    260  1.69.2.1     skrll 		return (EPERM);
    261  1.69.2.1     skrll 
    262  1.69.2.1     skrll 	LIST_FOREACH(mo, &mowners, mo_link) {
    263  1.69.2.1     skrll 		if (oldp != NULL) {
    264  1.69.2.1     skrll 			if (*oldlenp - len < sizeof(*mo)) {
    265  1.69.2.1     skrll 				error = ENOMEM;
    266  1.69.2.1     skrll 				break;
    267      1.64      matt 			}
    268  1.69.2.1     skrll 			error = copyout(mo, (caddr_t) oldp + len,
    269  1.69.2.1     skrll 					sizeof(*mo));
    270  1.69.2.1     skrll 			if (error)
    271  1.69.2.1     skrll 				break;
    272      1.64      matt 		}
    273  1.69.2.1     skrll 		len += sizeof(*mo);
    274      1.64      matt 	}
    275  1.69.2.1     skrll 
    276  1.69.2.1     skrll 	if (error == 0)
    277  1.69.2.1     skrll 		*oldlenp = len;
    278  1.69.2.1     skrll 
    279  1.69.2.1     skrll 	return (error);
    280  1.69.2.1     skrll }
    281  1.69.2.1     skrll #endif /* MBUFTRACE */
    282  1.69.2.1     skrll 
    283  1.69.2.1     skrll SYSCTL_SETUP(sysctl_kern_mbuf_setup, "sysctl kern.mbuf subtree setup")
    284  1.69.2.1     skrll {
    285  1.69.2.1     skrll 
    286  1.69.2.1     skrll 	sysctl_createv(clog, 0, NULL, NULL,
    287  1.69.2.1     skrll 		       CTLFLAG_PERMANENT,
    288  1.69.2.1     skrll 		       CTLTYPE_NODE, "kern", NULL,
    289  1.69.2.1     skrll 		       NULL, 0, NULL, 0,
    290  1.69.2.1     skrll 		       CTL_KERN, CTL_EOL);
    291  1.69.2.1     skrll 	sysctl_createv(clog, 0, NULL, NULL,
    292  1.69.2.1     skrll 		       CTLFLAG_PERMANENT,
    293  1.69.2.1     skrll 		       CTLTYPE_NODE, "mbuf",
    294  1.69.2.1     skrll 		       SYSCTL_DESCR("mbuf control variables"),
    295  1.69.2.1     skrll 		       NULL, 0, NULL, 0,
    296  1.69.2.1     skrll 		       CTL_KERN, KERN_MBUF, CTL_EOL);
    297  1.69.2.1     skrll 
    298  1.69.2.1     skrll 	sysctl_createv(clog, 0, NULL, NULL,
    299  1.69.2.1     skrll 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    300  1.69.2.1     skrll 		       CTLTYPE_INT, "msize",
    301  1.69.2.1     skrll 		       SYSCTL_DESCR("mbuf base size"),
    302  1.69.2.1     skrll 		       NULL, msize, NULL, 0,
    303  1.69.2.1     skrll 		       CTL_KERN, KERN_MBUF, MBUF_MSIZE, CTL_EOL);
    304  1.69.2.1     skrll 	sysctl_createv(clog, 0, NULL, NULL,
    305  1.69.2.1     skrll 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    306  1.69.2.1     skrll 		       CTLTYPE_INT, "mclbytes",
    307  1.69.2.1     skrll 		       SYSCTL_DESCR("mbuf cluster size"),
    308  1.69.2.1     skrll 		       NULL, mclbytes, NULL, 0,
    309  1.69.2.1     skrll 		       CTL_KERN, KERN_MBUF, MBUF_MCLBYTES, CTL_EOL);
    310  1.69.2.1     skrll 	sysctl_createv(clog, 0, NULL, NULL,
    311  1.69.2.1     skrll 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    312  1.69.2.1     skrll 		       CTLTYPE_INT, "nmbclusters",
    313  1.69.2.1     skrll 		       SYSCTL_DESCR("Limit on the number of mbuf clusters"),
    314  1.69.2.1     skrll 		       sysctl_kern_mbuf, 0, &nmbclusters, 0,
    315  1.69.2.1     skrll 		       CTL_KERN, KERN_MBUF, MBUF_NMBCLUSTERS, CTL_EOL);
    316  1.69.2.1     skrll 	sysctl_createv(clog, 0, NULL, NULL,
    317  1.69.2.1     skrll 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    318  1.69.2.1     skrll 		       CTLTYPE_INT, "mblowat",
    319  1.69.2.1     skrll 		       SYSCTL_DESCR("mbuf low water mark"),
    320  1.69.2.1     skrll 		       sysctl_kern_mbuf, 0, &mblowat, 0,
    321  1.69.2.1     skrll 		       CTL_KERN, KERN_MBUF, MBUF_MBLOWAT, CTL_EOL);
    322  1.69.2.1     skrll 	sysctl_createv(clog, 0, NULL, NULL,
    323  1.69.2.1     skrll 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    324  1.69.2.1     skrll 		       CTLTYPE_INT, "mcllowat",
    325  1.69.2.1     skrll 		       SYSCTL_DESCR("mbuf cluster low water mark"),
    326  1.69.2.1     skrll 		       sysctl_kern_mbuf, 0, &mcllowat, 0,
    327  1.69.2.1     skrll 		       CTL_KERN, KERN_MBUF, MBUF_MCLLOWAT, CTL_EOL);
    328  1.69.2.1     skrll 	sysctl_createv(clog, 0, NULL, NULL,
    329  1.69.2.1     skrll 		       CTLFLAG_PERMANENT,
    330  1.69.2.1     skrll 		       CTLTYPE_STRUCT, "stats",
    331  1.69.2.1     skrll 		       SYSCTL_DESCR("mbuf allocation statistics"),
    332  1.69.2.1     skrll 		       NULL, 0, &mbstat, sizeof(mbstat),
    333  1.69.2.1     skrll 		       CTL_KERN, KERN_MBUF, MBUF_STATS, CTL_EOL);
    334  1.69.2.1     skrll #ifdef MBUFTRACE
    335  1.69.2.1     skrll 	sysctl_createv(clog, 0, NULL, NULL,
    336  1.69.2.1     skrll 		       CTLFLAG_PERMANENT,
    337  1.69.2.1     skrll 		       CTLTYPE_STRUCT, "mowners",
    338  1.69.2.1     skrll 		       SYSCTL_DESCR("Information about mbuf owners"),
    339  1.69.2.1     skrll 		       sysctl_kern_mbuf_mowners, 0, NULL, 0,
    340  1.69.2.1     skrll 		       CTL_KERN, KERN_MBUF, MBUF_MOWNERS, CTL_EOL);
    341  1.69.2.1     skrll #endif /* MBUFTRACE */
    342      1.28   thorpej }
    343      1.28   thorpej 
    344      1.28   thorpej void *
    345      1.62   thorpej mclpool_alloc(struct pool *pp, int flags)
    346      1.28   thorpej {
    347      1.32   thorpej 	boolean_t waitok = (flags & PR_WAITOK) ? TRUE : FALSE;
    348      1.28   thorpej 
    349      1.54       chs 	return ((void *)uvm_km_alloc_poolpage1(mb_map, NULL, waitok));
    350       1.1       cgd }
    351       1.1       cgd 
    352      1.28   thorpej void
    353      1.62   thorpej mclpool_release(struct pool *pp, void *v)
    354       1.1       cgd {
    355       1.1       cgd 
    356      1.31   thorpej 	uvm_km_free_poolpage1(mb_map, (vaddr_t)v);
    357      1.65   thorpej }
    358      1.65   thorpej 
    359      1.65   thorpej /*ARGSUSED*/
    360      1.65   thorpej static int
    361      1.65   thorpej mb_ctor(void *arg, void *object, int flags)
    362      1.65   thorpej {
    363      1.65   thorpej 	struct mbuf *m = object;
    364      1.65   thorpej 
    365      1.65   thorpej #ifdef POOL_VTOPHYS
    366      1.65   thorpej 	m->m_paddr = POOL_VTOPHYS(m);
    367      1.65   thorpej #else
    368      1.65   thorpej 	m->m_paddr = M_PADDR_INVALID;
    369      1.65   thorpej #endif
    370      1.65   thorpej 	return (0);
    371       1.1       cgd }
    372       1.1       cgd 
    373      1.14  christos void
    374      1.59   thorpej m_reclaim(void *arg, int flags)
    375       1.1       cgd {
    376      1.27      matt 	struct domain *dp;
    377  1.69.2.1     skrll 	const struct protosw *pr;
    378      1.27      matt 	struct ifnet *ifp;
    379      1.52   thorpej 	int s = splvm();
    380       1.1       cgd 
    381  1.69.2.6     skrll 	DOMAIN_FOREACH(dp) {
    382      1.33   thorpej 		for (pr = dp->dom_protosw;
    383      1.33   thorpej 		     pr < dp->dom_protoswNPROTOSW; pr++)
    384      1.33   thorpej 			if (pr->pr_drain)
    385      1.33   thorpej 				(*pr->pr_drain)();
    386  1.69.2.6     skrll 	}
    387  1.69.2.6     skrll 	TAILQ_FOREACH(ifp, &ifnet, if_list)
    388      1.27      matt 		if (ifp->if_drain)
    389      1.27      matt 			(*ifp->if_drain)(ifp);
    390       1.1       cgd 	splx(s);
    391       1.1       cgd 	mbstat.m_drain++;
    392       1.1       cgd }
    393       1.1       cgd 
    394       1.1       cgd /*
    395       1.1       cgd  * Space allocation routines.
    396       1.1       cgd  * These are also available as macros
    397       1.1       cgd  * for critical paths.
    398       1.1       cgd  */
    399       1.1       cgd struct mbuf *
    400      1.62   thorpej m_get(int nowait, int type)
    401       1.1       cgd {
    402      1.27      matt 	struct mbuf *m;
    403       1.1       cgd 
    404       1.5       cgd 	MGET(m, nowait, type);
    405       1.1       cgd 	return (m);
    406       1.1       cgd }
    407       1.1       cgd 
    408       1.1       cgd struct mbuf *
    409      1.62   thorpej m_gethdr(int nowait, int type)
    410       1.1       cgd {
    411      1.27      matt 	struct mbuf *m;
    412       1.1       cgd 
    413       1.5       cgd 	MGETHDR(m, nowait, type);
    414       1.1       cgd 	return (m);
    415       1.1       cgd }
    416       1.1       cgd 
    417       1.1       cgd struct mbuf *
    418      1.62   thorpej m_getclr(int nowait, int type)
    419       1.1       cgd {
    420      1.27      matt 	struct mbuf *m;
    421       1.1       cgd 
    422       1.5       cgd 	MGET(m, nowait, type);
    423       1.1       cgd 	if (m == 0)
    424  1.69.2.1     skrll 		return (NULL);
    425      1.30     perry 	memset(mtod(m, caddr_t), 0, MLEN);
    426       1.1       cgd 	return (m);
    427       1.1       cgd }
    428       1.1       cgd 
    429      1.64      matt void
    430      1.64      matt m_clget(struct mbuf *m, int nowait)
    431      1.64      matt {
    432  1.69.2.1     skrll 
    433      1.64      matt 	MCLGET(m, nowait);
    434      1.64      matt }
    435      1.64      matt 
    436       1.1       cgd struct mbuf *
    437      1.62   thorpej m_free(struct mbuf *m)
    438       1.1       cgd {
    439      1.27      matt 	struct mbuf *n;
    440       1.1       cgd 
    441       1.1       cgd 	MFREE(m, n);
    442       1.1       cgd 	return (n);
    443       1.1       cgd }
    444       1.1       cgd 
    445       1.9   mycroft void
    446      1.62   thorpej m_freem(struct mbuf *m)
    447       1.1       cgd {
    448      1.27      matt 	struct mbuf *n;
    449       1.1       cgd 
    450       1.1       cgd 	if (m == NULL)
    451       1.1       cgd 		return;
    452       1.1       cgd 	do {
    453       1.1       cgd 		MFREE(m, n);
    454      1.18   thorpej 		m = n;
    455      1.18   thorpej 	} while (m);
    456       1.1       cgd }
    457       1.1       cgd 
    458      1.64      matt #ifdef MBUFTRACE
    459  1.69.2.1     skrll /*
    460  1.69.2.1     skrll  * Walk a chain of mbufs, claiming ownership of each mbuf in the chain.
    461  1.69.2.1     skrll  */
    462      1.64      matt void
    463  1.69.2.1     skrll m_claimm(struct mbuf *m, struct mowner *mo)
    464      1.64      matt {
    465  1.69.2.1     skrll 
    466      1.64      matt 	for (; m != NULL; m = m->m_next)
    467      1.64      matt 		MCLAIM(m, mo);
    468      1.64      matt }
    469      1.64      matt #endif
    470      1.64      matt 
    471       1.1       cgd /*
    472       1.1       cgd  * Mbuffer utility routines.
    473       1.1       cgd  */
    474       1.1       cgd 
    475       1.1       cgd /*
    476       1.1       cgd  * Lesser-used path for M_PREPEND:
    477       1.1       cgd  * allocate new mbuf to prepend to chain,
    478       1.1       cgd  * copy junk along.
    479       1.1       cgd  */
    480       1.1       cgd struct mbuf *
    481      1.62   thorpej m_prepend(struct mbuf *m, int len, int how)
    482       1.1       cgd {
    483       1.1       cgd 	struct mbuf *mn;
    484       1.1       cgd 
    485       1.9   mycroft 	MGET(mn, how, m->m_type);
    486       1.1       cgd 	if (mn == (struct mbuf *)NULL) {
    487       1.1       cgd 		m_freem(m);
    488       1.1       cgd 		return ((struct mbuf *)NULL);
    489       1.1       cgd 	}
    490       1.1       cgd 	if (m->m_flags & M_PKTHDR) {
    491       1.1       cgd 		M_COPY_PKTHDR(mn, m);
    492  1.69.2.1     skrll 		m_tag_delete_chain(m, NULL);
    493       1.1       cgd 		m->m_flags &= ~M_PKTHDR;
    494      1.64      matt 	} else {
    495      1.64      matt 		MCLAIM(mn, m->m_owner);
    496       1.1       cgd 	}
    497       1.1       cgd 	mn->m_next = m;
    498       1.1       cgd 	m = mn;
    499       1.1       cgd 	if (len < MHLEN)
    500       1.1       cgd 		MH_ALIGN(m, len);
    501       1.1       cgd 	m->m_len = len;
    502       1.1       cgd 	return (m);
    503       1.1       cgd }
    504       1.1       cgd 
    505       1.1       cgd /*
    506       1.1       cgd  * Make a copy of an mbuf chain starting "off0" bytes from the beginning,
    507       1.1       cgd  * continuing for "len" bytes.  If len is M_COPYALL, copy to end of mbuf.
    508       1.1       cgd  * The wait parameter is a choice of M_WAIT/M_DONTWAIT from caller.
    509       1.1       cgd  */
    510       1.1       cgd int MCFail;
    511       1.1       cgd 
    512       1.1       cgd struct mbuf *
    513      1.62   thorpej m_copym(struct mbuf *m, int off0, int len, int wait)
    514       1.1       cgd {
    515  1.69.2.1     skrll 
    516      1.44    itojun 	return m_copym0(m, off0, len, wait, 0);	/* shallow copy on M_EXT */
    517      1.44    itojun }
    518      1.44    itojun 
    519      1.44    itojun struct mbuf *
    520      1.62   thorpej m_dup(struct mbuf *m, int off0, int len, int wait)
    521      1.44    itojun {
    522  1.69.2.1     skrll 
    523      1.44    itojun 	return m_copym0(m, off0, len, wait, 1);	/* deep copy */
    524      1.44    itojun }
    525      1.44    itojun 
    526      1.44    itojun static struct mbuf *
    527      1.62   thorpej m_copym0(struct mbuf *m, int off0, int len, int wait, int deep)
    528      1.44    itojun {
    529      1.27      matt 	struct mbuf *n, **np;
    530      1.27      matt 	int off = off0;
    531       1.1       cgd 	struct mbuf *top;
    532       1.1       cgd 	int copyhdr = 0;
    533       1.1       cgd 
    534       1.1       cgd 	if (off < 0 || len < 0)
    535      1.43   thorpej 		panic("m_copym: off %d, len %d", off, len);
    536       1.1       cgd 	if (off == 0 && m->m_flags & M_PKTHDR)
    537       1.1       cgd 		copyhdr = 1;
    538       1.1       cgd 	while (off > 0) {
    539       1.1       cgd 		if (m == 0)
    540  1.69.2.5     skrll 			panic("m_copym: m == 0, off %d", off);
    541       1.1       cgd 		if (off < m->m_len)
    542       1.1       cgd 			break;
    543       1.1       cgd 		off -= m->m_len;
    544       1.1       cgd 		m = m->m_next;
    545       1.1       cgd 	}
    546       1.1       cgd 	np = &top;
    547       1.1       cgd 	top = 0;
    548       1.1       cgd 	while (len > 0) {
    549       1.1       cgd 		if (m == 0) {
    550       1.1       cgd 			if (len != M_COPYALL)
    551  1.69.2.5     skrll 				panic("m_copym: m == 0, len %d [!COPYALL]",
    552  1.69.2.5     skrll 				    len);
    553       1.1       cgd 			break;
    554       1.1       cgd 		}
    555       1.1       cgd 		MGET(n, wait, m->m_type);
    556       1.1       cgd 		*np = n;
    557       1.1       cgd 		if (n == 0)
    558       1.1       cgd 			goto nospace;
    559      1.64      matt 		MCLAIM(n, m->m_owner);
    560       1.1       cgd 		if (copyhdr) {
    561       1.1       cgd 			M_COPY_PKTHDR(n, m);
    562       1.1       cgd 			if (len == M_COPYALL)
    563       1.1       cgd 				n->m_pkthdr.len -= off0;
    564       1.1       cgd 			else
    565       1.1       cgd 				n->m_pkthdr.len = len;
    566       1.1       cgd 			copyhdr = 0;
    567       1.1       cgd 		}
    568       1.9   mycroft 		n->m_len = min(len, m->m_len - off);
    569       1.1       cgd 		if (m->m_flags & M_EXT) {
    570      1.44    itojun 			if (!deep) {
    571      1.44    itojun 				n->m_data = m->m_data + off;
    572      1.44    itojun 				n->m_ext = m->m_ext;
    573      1.44    itojun 				MCLADDREFERENCE(m, n);
    574      1.44    itojun 			} else {
    575      1.48    itojun 				/*
    576      1.50    itojun 				 * we are unsure about the way m was allocated.
    577      1.50    itojun 				 * copy into multiple MCLBYTES cluster mbufs.
    578      1.48    itojun 				 */
    579      1.44    itojun 				MCLGET(n, wait);
    580      1.50    itojun 				n->m_len = 0;
    581      1.50    itojun 				n->m_len = M_TRAILINGSPACE(n);
    582      1.50    itojun 				n->m_len = min(n->m_len, len);
    583      1.50    itojun 				n->m_len = min(n->m_len, m->m_len - off);
    584      1.50    itojun 				memcpy(mtod(n, caddr_t), mtod(m, caddr_t) + off,
    585      1.44    itojun 				    (unsigned)n->m_len);
    586      1.44    itojun 			}
    587       1.1       cgd 		} else
    588      1.30     perry 			memcpy(mtod(n, caddr_t), mtod(m, caddr_t)+off,
    589       1.1       cgd 			    (unsigned)n->m_len);
    590       1.1       cgd 		if (len != M_COPYALL)
    591       1.1       cgd 			len -= n->m_len;
    592      1.50    itojun 		off += n->m_len;
    593      1.50    itojun #ifdef DIAGNOSTIC
    594      1.50    itojun 		if (off > m->m_len)
    595      1.50    itojun 			panic("m_copym0 overrun");
    596      1.50    itojun #endif
    597      1.50    itojun 		if (off == m->m_len) {
    598      1.50    itojun 			m = m->m_next;
    599      1.50    itojun 			off = 0;
    600      1.50    itojun 		}
    601       1.1       cgd 		np = &n->m_next;
    602       1.1       cgd 	}
    603       1.1       cgd 	if (top == 0)
    604       1.1       cgd 		MCFail++;
    605       1.1       cgd 	return (top);
    606       1.1       cgd nospace:
    607       1.1       cgd 	m_freem(top);
    608       1.1       cgd 	MCFail++;
    609  1.69.2.1     skrll 	return (NULL);
    610       1.1       cgd }
    611       1.1       cgd 
    612       1.1       cgd /*
    613      1.18   thorpej  * Copy an entire packet, including header (which must be present).
    614      1.18   thorpej  * An optimization of the common case `m_copym(m, 0, M_COPYALL, how)'.
    615      1.18   thorpej  */
    616      1.18   thorpej struct mbuf *
    617      1.62   thorpej m_copypacket(struct mbuf *m, int how)
    618      1.18   thorpej {
    619      1.18   thorpej 	struct mbuf *top, *n, *o;
    620      1.18   thorpej 
    621      1.18   thorpej 	MGET(n, how, m->m_type);
    622      1.18   thorpej 	top = n;
    623      1.18   thorpej 	if (!n)
    624      1.18   thorpej 		goto nospace;
    625      1.18   thorpej 
    626      1.64      matt 	MCLAIM(n, m->m_owner);
    627      1.18   thorpej 	M_COPY_PKTHDR(n, m);
    628      1.18   thorpej 	n->m_len = m->m_len;
    629      1.18   thorpej 	if (m->m_flags & M_EXT) {
    630      1.18   thorpej 		n->m_data = m->m_data;
    631      1.18   thorpej 		n->m_ext = m->m_ext;
    632      1.18   thorpej 		MCLADDREFERENCE(m, n);
    633      1.18   thorpej 	} else {
    634      1.30     perry 		memcpy(mtod(n, char *), mtod(m, char *), n->m_len);
    635      1.18   thorpej 	}
    636      1.18   thorpej 
    637      1.18   thorpej 	m = m->m_next;
    638      1.18   thorpej 	while (m) {
    639      1.18   thorpej 		MGET(o, how, m->m_type);
    640      1.18   thorpej 		if (!o)
    641      1.18   thorpej 			goto nospace;
    642      1.18   thorpej 
    643      1.64      matt 		MCLAIM(o, m->m_owner);
    644      1.18   thorpej 		n->m_next = o;
    645      1.18   thorpej 		n = n->m_next;
    646      1.18   thorpej 
    647      1.18   thorpej 		n->m_len = m->m_len;
    648      1.18   thorpej 		if (m->m_flags & M_EXT) {
    649      1.18   thorpej 			n->m_data = m->m_data;
    650      1.18   thorpej 			n->m_ext = m->m_ext;
    651      1.18   thorpej 			MCLADDREFERENCE(m, n);
    652      1.18   thorpej 		} else {
    653      1.30     perry 			memcpy(mtod(n, char *), mtod(m, char *), n->m_len);
    654      1.18   thorpej 		}
    655      1.18   thorpej 
    656      1.18   thorpej 		m = m->m_next;
    657      1.18   thorpej 	}
    658      1.18   thorpej 	return top;
    659      1.18   thorpej nospace:
    660      1.18   thorpej 	m_freem(top);
    661      1.18   thorpej 	MCFail++;
    662  1.69.2.1     skrll 	return NULL;
    663      1.18   thorpej }
    664      1.18   thorpej 
    665      1.18   thorpej /*
    666       1.1       cgd  * Copy data from an mbuf chain starting "off" bytes from the beginning,
    667       1.1       cgd  * continuing for "len" bytes, into the indicated buffer.
    668       1.1       cgd  */
    669      1.14  christos void
    670  1.69.2.2     skrll m_copydata(struct mbuf *m, int off, int len, void *vp)
    671       1.1       cgd {
    672      1.27      matt 	unsigned count;
    673  1.69.2.2     skrll 	char *cp = vp;
    674       1.1       cgd 
    675       1.1       cgd 	if (off < 0 || len < 0)
    676  1.69.2.5     skrll 		panic("m_copydata: off %d, len %d", off, len);
    677       1.1       cgd 	while (off > 0) {
    678       1.1       cgd 		if (m == 0)
    679  1.69.2.5     skrll 			panic("m_copydata: m == 0, off %d", off);
    680       1.1       cgd 		if (off < m->m_len)
    681       1.1       cgd 			break;
    682       1.1       cgd 		off -= m->m_len;
    683       1.1       cgd 		m = m->m_next;
    684       1.1       cgd 	}
    685       1.1       cgd 	while (len > 0) {
    686       1.1       cgd 		if (m == 0)
    687  1.69.2.5     skrll 			panic("m_copydata: m == 0, len %d", len);
    688       1.9   mycroft 		count = min(m->m_len - off, len);
    689      1.30     perry 		memcpy(cp, mtod(m, caddr_t) + off, count);
    690       1.1       cgd 		len -= count;
    691       1.1       cgd 		cp += count;
    692       1.1       cgd 		off = 0;
    693       1.1       cgd 		m = m->m_next;
    694       1.1       cgd 	}
    695       1.1       cgd }
    696       1.1       cgd 
    697       1.1       cgd /*
    698       1.1       cgd  * Concatenate mbuf chain n to m.
    699  1.69.2.1     skrll  * n might be copied into m (when n->m_len is small), therefore data portion of
    700  1.69.2.1     skrll  * n could be copied into an mbuf of different mbuf type.
    701       1.1       cgd  * Any m_pkthdr is not updated.
    702       1.1       cgd  */
    703      1.14  christos void
    704      1.62   thorpej m_cat(struct mbuf *m, struct mbuf *n)
    705       1.1       cgd {
    706  1.69.2.1     skrll 
    707       1.1       cgd 	while (m->m_next)
    708       1.1       cgd 		m = m->m_next;
    709       1.1       cgd 	while (n) {
    710  1.69.2.1     skrll 		if (M_READONLY(m) || n->m_len > M_TRAILINGSPACE(m)) {
    711       1.1       cgd 			/* just join the two chains */
    712       1.1       cgd 			m->m_next = n;
    713       1.1       cgd 			return;
    714       1.1       cgd 		}
    715       1.1       cgd 		/* splat the data from one into the other */
    716      1.30     perry 		memcpy(mtod(m, caddr_t) + m->m_len, mtod(n, caddr_t),
    717       1.1       cgd 		    (u_int)n->m_len);
    718       1.1       cgd 		m->m_len += n->m_len;
    719       1.1       cgd 		n = m_free(n);
    720       1.1       cgd 	}
    721       1.1       cgd }
    722       1.1       cgd 
    723      1.11   mycroft void
    724      1.62   thorpej m_adj(struct mbuf *mp, int req_len)
    725       1.1       cgd {
    726      1.27      matt 	int len = req_len;
    727      1.27      matt 	struct mbuf *m;
    728      1.27      matt 	int count;
    729       1.1       cgd 
    730       1.1       cgd 	if ((m = mp) == NULL)
    731       1.1       cgd 		return;
    732       1.1       cgd 	if (len >= 0) {
    733       1.1       cgd 		/*
    734       1.1       cgd 		 * Trim from head.
    735       1.1       cgd 		 */
    736       1.1       cgd 		while (m != NULL && len > 0) {
    737       1.1       cgd 			if (m->m_len <= len) {
    738       1.1       cgd 				len -= m->m_len;
    739       1.1       cgd 				m->m_len = 0;
    740       1.1       cgd 				m = m->m_next;
    741       1.1       cgd 			} else {
    742       1.1       cgd 				m->m_len -= len;
    743       1.1       cgd 				m->m_data += len;
    744       1.1       cgd 				len = 0;
    745       1.1       cgd 			}
    746       1.1       cgd 		}
    747       1.1       cgd 		m = mp;
    748       1.1       cgd 		if (mp->m_flags & M_PKTHDR)
    749       1.1       cgd 			m->m_pkthdr.len -= (req_len - len);
    750       1.1       cgd 	} else {
    751       1.1       cgd 		/*
    752       1.1       cgd 		 * Trim from tail.  Scan the mbuf chain,
    753       1.1       cgd 		 * calculating its length and finding the last mbuf.
    754       1.1       cgd 		 * If the adjustment only affects this mbuf, then just
    755       1.1       cgd 		 * adjust and return.  Otherwise, rescan and truncate
    756       1.1       cgd 		 * after the remaining size.
    757       1.1       cgd 		 */
    758       1.1       cgd 		len = -len;
    759       1.1       cgd 		count = 0;
    760       1.1       cgd 		for (;;) {
    761       1.1       cgd 			count += m->m_len;
    762       1.1       cgd 			if (m->m_next == (struct mbuf *)0)
    763       1.1       cgd 				break;
    764       1.1       cgd 			m = m->m_next;
    765       1.1       cgd 		}
    766       1.1       cgd 		if (m->m_len >= len) {
    767       1.1       cgd 			m->m_len -= len;
    768       1.8   deraadt 			if (mp->m_flags & M_PKTHDR)
    769       1.8   deraadt 				mp->m_pkthdr.len -= len;
    770       1.1       cgd 			return;
    771       1.1       cgd 		}
    772       1.1       cgd 		count -= len;
    773       1.1       cgd 		if (count < 0)
    774       1.1       cgd 			count = 0;
    775       1.1       cgd 		/*
    776       1.1       cgd 		 * Correct length for chain is "count".
    777       1.1       cgd 		 * Find the mbuf with last data, adjust its length,
    778       1.1       cgd 		 * and toss data from remaining mbufs on chain.
    779       1.1       cgd 		 */
    780       1.1       cgd 		m = mp;
    781       1.1       cgd 		if (m->m_flags & M_PKTHDR)
    782       1.1       cgd 			m->m_pkthdr.len = count;
    783       1.1       cgd 		for (; m; m = m->m_next) {
    784       1.1       cgd 			if (m->m_len >= count) {
    785       1.1       cgd 				m->m_len = count;
    786       1.1       cgd 				break;
    787       1.1       cgd 			}
    788       1.1       cgd 			count -= m->m_len;
    789       1.1       cgd 		}
    790      1.18   thorpej 		while (m->m_next)
    791      1.18   thorpej 			(m = m->m_next) ->m_len = 0;
    792       1.1       cgd 	}
    793       1.1       cgd }
    794       1.1       cgd 
    795       1.1       cgd /*
    796       1.1       cgd  * Rearange an mbuf chain so that len bytes are contiguous
    797       1.1       cgd  * and in the data area of an mbuf (so that mtod and dtom
    798       1.1       cgd  * will work for a structure of size len).  Returns the resulting
    799       1.1       cgd  * mbuf chain on success, frees it and returns null on failure.
    800       1.1       cgd  * If there is room, it will add up to max_protohdr-len extra bytes to the
    801       1.1       cgd  * contiguous region in an attempt to avoid being called next time.
    802       1.1       cgd  */
    803       1.1       cgd int MPFail;
    804       1.1       cgd 
    805       1.1       cgd struct mbuf *
    806      1.62   thorpej m_pullup(struct mbuf *n, int len)
    807       1.1       cgd {
    808      1.27      matt 	struct mbuf *m;
    809      1.27      matt 	int count;
    810       1.1       cgd 	int space;
    811       1.1       cgd 
    812       1.1       cgd 	/*
    813       1.1       cgd 	 * If first mbuf has no cluster, and has room for len bytes
    814       1.1       cgd 	 * without shifting current data, pullup into it,
    815       1.1       cgd 	 * otherwise allocate a new mbuf to prepend to the chain.
    816       1.1       cgd 	 */
    817       1.1       cgd 	if ((n->m_flags & M_EXT) == 0 &&
    818       1.1       cgd 	    n->m_data + len < &n->m_dat[MLEN] && n->m_next) {
    819       1.1       cgd 		if (n->m_len >= len)
    820       1.1       cgd 			return (n);
    821       1.1       cgd 		m = n;
    822       1.1       cgd 		n = n->m_next;
    823       1.1       cgd 		len -= m->m_len;
    824       1.1       cgd 	} else {
    825       1.1       cgd 		if (len > MHLEN)
    826       1.1       cgd 			goto bad;
    827       1.1       cgd 		MGET(m, M_DONTWAIT, n->m_type);
    828       1.1       cgd 		if (m == 0)
    829       1.1       cgd 			goto bad;
    830      1.64      matt 		MCLAIM(m, n->m_owner);
    831       1.1       cgd 		m->m_len = 0;
    832       1.1       cgd 		if (n->m_flags & M_PKTHDR) {
    833       1.1       cgd 			M_COPY_PKTHDR(m, n);
    834  1.69.2.1     skrll 			m_tag_delete_chain(n, NULL);
    835       1.1       cgd 			n->m_flags &= ~M_PKTHDR;
    836       1.1       cgd 		}
    837       1.1       cgd 	}
    838       1.1       cgd 	space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
    839       1.1       cgd 	do {
    840       1.1       cgd 		count = min(min(max(len, max_protohdr), space), n->m_len);
    841      1.30     perry 		memcpy(mtod(m, caddr_t) + m->m_len, mtod(n, caddr_t),
    842       1.1       cgd 		  (unsigned)count);
    843       1.1       cgd 		len -= count;
    844       1.1       cgd 		m->m_len += count;
    845       1.1       cgd 		n->m_len -= count;
    846       1.1       cgd 		space -= count;
    847       1.1       cgd 		if (n->m_len)
    848       1.1       cgd 			n->m_data += count;
    849       1.1       cgd 		else
    850       1.1       cgd 			n = m_free(n);
    851       1.1       cgd 	} while (len > 0 && n);
    852       1.1       cgd 	if (len > 0) {
    853       1.1       cgd 		(void) m_free(m);
    854       1.1       cgd 		goto bad;
    855       1.1       cgd 	}
    856       1.1       cgd 	m->m_next = n;
    857       1.1       cgd 	return (m);
    858       1.1       cgd bad:
    859       1.1       cgd 	m_freem(n);
    860       1.1       cgd 	MPFail++;
    861  1.69.2.1     skrll 	return (NULL);
    862      1.60   thorpej }
    863      1.60   thorpej 
    864      1.60   thorpej /*
    865      1.60   thorpej  * Like m_pullup(), except a new mbuf is always allocated, and we allow
    866      1.60   thorpej  * the amount of empty space before the data in the new mbuf to be specified
    867      1.60   thorpej  * (in the event that the caller expects to prepend later).
    868      1.60   thorpej  */
    869      1.60   thorpej int MSFail;
    870      1.60   thorpej 
    871      1.60   thorpej struct mbuf *
    872      1.60   thorpej m_copyup(struct mbuf *n, int len, int dstoff)
    873      1.60   thorpej {
    874      1.60   thorpej 	struct mbuf *m;
    875      1.60   thorpej 	int count, space;
    876      1.60   thorpej 
    877      1.60   thorpej 	if (len > (MHLEN - dstoff))
    878      1.60   thorpej 		goto bad;
    879      1.60   thorpej 	MGET(m, M_DONTWAIT, n->m_type);
    880      1.60   thorpej 	if (m == NULL)
    881      1.60   thorpej 		goto bad;
    882      1.64      matt 	MCLAIM(m, n->m_owner);
    883      1.60   thorpej 	m->m_len = 0;
    884      1.60   thorpej 	if (n->m_flags & M_PKTHDR) {
    885      1.60   thorpej 		M_COPY_PKTHDR(m, n);
    886  1.69.2.2     skrll 		m_tag_delete_chain(n, NULL);
    887      1.60   thorpej 		n->m_flags &= ~M_PKTHDR;
    888      1.60   thorpej 	}
    889      1.60   thorpej 	m->m_data += dstoff;
    890      1.60   thorpej 	space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
    891      1.60   thorpej 	do {
    892      1.60   thorpej 		count = min(min(max(len, max_protohdr), space), n->m_len);
    893      1.60   thorpej 		memcpy(mtod(m, caddr_t) + m->m_len, mtod(n, caddr_t),
    894      1.60   thorpej 		    (unsigned)count);
    895      1.60   thorpej 		len -= count;
    896      1.60   thorpej 		m->m_len += count;
    897      1.60   thorpej 		n->m_len -= count;
    898      1.60   thorpej 		space -= count;
    899      1.60   thorpej 		if (n->m_len)
    900      1.60   thorpej 			n->m_data += count;
    901      1.60   thorpej 		else
    902      1.60   thorpej 			n = m_free(n);
    903      1.60   thorpej 	} while (len > 0 && n);
    904      1.60   thorpej 	if (len > 0) {
    905      1.60   thorpej 		(void) m_free(m);
    906      1.60   thorpej 		goto bad;
    907      1.60   thorpej 	}
    908      1.60   thorpej 	m->m_next = n;
    909      1.60   thorpej 	return (m);
    910      1.60   thorpej  bad:
    911      1.60   thorpej 	m_freem(n);
    912      1.60   thorpej 	MSFail++;
    913      1.60   thorpej 	return (NULL);
    914       1.9   mycroft }
    915       1.9   mycroft 
    916       1.9   mycroft /*
    917       1.9   mycroft  * Partition an mbuf chain in two pieces, returning the tail --
    918       1.9   mycroft  * all but the first len0 bytes.  In case of failure, it returns NULL and
    919       1.9   mycroft  * attempts to restore the chain to its original state.
    920       1.9   mycroft  */
    921       1.9   mycroft struct mbuf *
    922      1.62   thorpej m_split(struct mbuf *m0, int len0, int wait)
    923       1.9   mycroft {
    924  1.69.2.2     skrll 
    925  1.69.2.2     skrll 	return m_split0(m0, len0, wait, 1);
    926  1.69.2.2     skrll }
    927  1.69.2.2     skrll 
    928  1.69.2.2     skrll static struct mbuf *
    929  1.69.2.2     skrll m_split0(struct mbuf *m0, int len0, int wait, int copyhdr)
    930  1.69.2.2     skrll {
    931      1.27      matt 	struct mbuf *m, *n;
    932      1.22   thorpej 	unsigned len = len0, remain, len_save;
    933       1.9   mycroft 
    934       1.9   mycroft 	for (m = m0; m && len > m->m_len; m = m->m_next)
    935       1.9   mycroft 		len -= m->m_len;
    936       1.9   mycroft 	if (m == 0)
    937  1.69.2.1     skrll 		return (NULL);
    938       1.9   mycroft 	remain = m->m_len - len;
    939  1.69.2.2     skrll 	if (copyhdr && (m0->m_flags & M_PKTHDR)) {
    940       1.9   mycroft 		MGETHDR(n, wait, m0->m_type);
    941       1.9   mycroft 		if (n == 0)
    942  1.69.2.1     skrll 			return (NULL);
    943      1.64      matt 		MCLAIM(m, m0->m_owner);
    944       1.9   mycroft 		n->m_pkthdr.rcvif = m0->m_pkthdr.rcvif;
    945       1.9   mycroft 		n->m_pkthdr.len = m0->m_pkthdr.len - len0;
    946      1.22   thorpej 		len_save = m0->m_pkthdr.len;
    947       1.9   mycroft 		m0->m_pkthdr.len = len0;
    948       1.9   mycroft 		if (m->m_flags & M_EXT)
    949       1.9   mycroft 			goto extpacket;
    950       1.9   mycroft 		if (remain > MHLEN) {
    951       1.9   mycroft 			/* m can't be the lead packet */
    952       1.9   mycroft 			MH_ALIGN(n, 0);
    953       1.9   mycroft 			n->m_next = m_split(m, len, wait);
    954       1.9   mycroft 			if (n->m_next == 0) {
    955       1.9   mycroft 				(void) m_free(n);
    956      1.22   thorpej 				m0->m_pkthdr.len = len_save;
    957  1.69.2.1     skrll 				return (NULL);
    958       1.9   mycroft 			} else
    959       1.9   mycroft 				return (n);
    960       1.9   mycroft 		} else
    961       1.9   mycroft 			MH_ALIGN(n, remain);
    962       1.9   mycroft 	} else if (remain == 0) {
    963       1.9   mycroft 		n = m->m_next;
    964       1.9   mycroft 		m->m_next = 0;
    965       1.9   mycroft 		return (n);
    966       1.9   mycroft 	} else {
    967       1.9   mycroft 		MGET(n, wait, m->m_type);
    968       1.9   mycroft 		if (n == 0)
    969  1.69.2.1     skrll 			return (NULL);
    970      1.64      matt 		MCLAIM(n, m->m_owner);
    971       1.9   mycroft 		M_ALIGN(n, remain);
    972       1.9   mycroft 	}
    973       1.9   mycroft extpacket:
    974       1.9   mycroft 	if (m->m_flags & M_EXT) {
    975       1.9   mycroft 		n->m_ext = m->m_ext;
    976      1.18   thorpej 		MCLADDREFERENCE(m, n);
    977       1.9   mycroft 		n->m_data = m->m_data + len;
    978       1.9   mycroft 	} else {
    979      1.30     perry 		memcpy(mtod(n, caddr_t), mtod(m, caddr_t) + len, remain);
    980       1.9   mycroft 	}
    981       1.9   mycroft 	n->m_len = remain;
    982       1.9   mycroft 	m->m_len = len;
    983       1.9   mycroft 	n->m_next = m->m_next;
    984       1.9   mycroft 	m->m_next = 0;
    985       1.9   mycroft 	return (n);
    986       1.9   mycroft }
    987       1.9   mycroft /*
    988       1.9   mycroft  * Routine to copy from device local memory into mbufs.
    989       1.9   mycroft  */
    990       1.9   mycroft struct mbuf *
    991      1.62   thorpej m_devget(char *buf, int totlen, int off0, struct ifnet *ifp,
    992      1.62   thorpej     void (*copy)(const void *from, void *to, size_t len))
    993       1.9   mycroft {
    994      1.27      matt 	struct mbuf *m;
    995       1.9   mycroft 	struct mbuf *top = 0, **mp = &top;
    996      1.27      matt 	int off = off0, len;
    997      1.27      matt 	char *cp;
    998       1.9   mycroft 	char *epkt;
    999       1.9   mycroft 
   1000       1.9   mycroft 	cp = buf;
   1001       1.9   mycroft 	epkt = cp + totlen;
   1002       1.9   mycroft 	if (off) {
   1003      1.13       cgd 		/*
   1004      1.13       cgd 		 * If 'off' is non-zero, packet is trailer-encapsulated,
   1005      1.13       cgd 		 * so we have to skip the type and length fields.
   1006      1.13       cgd 		 */
   1007      1.13       cgd 		cp += off + 2 * sizeof(u_int16_t);
   1008      1.13       cgd 		totlen -= 2 * sizeof(u_int16_t);
   1009       1.9   mycroft 	}
   1010       1.9   mycroft 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   1011       1.9   mycroft 	if (m == 0)
   1012  1.69.2.1     skrll 		return (NULL);
   1013       1.9   mycroft 	m->m_pkthdr.rcvif = ifp;
   1014       1.9   mycroft 	m->m_pkthdr.len = totlen;
   1015       1.9   mycroft 	m->m_len = MHLEN;
   1016       1.9   mycroft 
   1017       1.9   mycroft 	while (totlen > 0) {
   1018       1.9   mycroft 		if (top) {
   1019       1.9   mycroft 			MGET(m, M_DONTWAIT, MT_DATA);
   1020       1.9   mycroft 			if (m == 0) {
   1021       1.9   mycroft 				m_freem(top);
   1022  1.69.2.1     skrll 				return (NULL);
   1023       1.9   mycroft 			}
   1024       1.9   mycroft 			m->m_len = MLEN;
   1025       1.9   mycroft 		}
   1026       1.9   mycroft 		len = min(totlen, epkt - cp);
   1027       1.9   mycroft 		if (len >= MINCLSIZE) {
   1028       1.9   mycroft 			MCLGET(m, M_DONTWAIT);
   1029      1.19   mycroft 			if ((m->m_flags & M_EXT) == 0) {
   1030      1.20   mycroft 				m_free(m);
   1031      1.19   mycroft 				m_freem(top);
   1032  1.69.2.1     skrll 				return (NULL);
   1033      1.19   mycroft 			}
   1034      1.19   mycroft 			m->m_len = len = min(len, MCLBYTES);
   1035       1.9   mycroft 		} else {
   1036       1.9   mycroft 			/*
   1037       1.9   mycroft 			 * Place initial small packet/header at end of mbuf.
   1038       1.9   mycroft 			 */
   1039       1.9   mycroft 			if (len < m->m_len) {
   1040       1.9   mycroft 				if (top == 0 && len + max_linkhdr <= m->m_len)
   1041       1.9   mycroft 					m->m_data += max_linkhdr;
   1042       1.9   mycroft 				m->m_len = len;
   1043       1.9   mycroft 			} else
   1044       1.9   mycroft 				len = m->m_len;
   1045       1.9   mycroft 		}
   1046       1.9   mycroft 		if (copy)
   1047      1.14  christos 			copy(cp, mtod(m, caddr_t), (size_t)len);
   1048       1.9   mycroft 		else
   1049      1.30     perry 			memcpy(mtod(m, caddr_t), cp, (size_t)len);
   1050       1.9   mycroft 		cp += len;
   1051       1.9   mycroft 		*mp = m;
   1052       1.9   mycroft 		mp = &m->m_next;
   1053       1.9   mycroft 		totlen -= len;
   1054       1.9   mycroft 		if (cp == epkt)
   1055       1.9   mycroft 			cp = buf;
   1056       1.9   mycroft 	}
   1057       1.9   mycroft 	return (top);
   1058      1.18   thorpej }
   1059      1.18   thorpej 
   1060      1.18   thorpej /*
   1061      1.18   thorpej  * Copy data from a buffer back into the indicated mbuf chain,
   1062      1.18   thorpej  * starting "off" bytes from the beginning, extending the mbuf
   1063      1.18   thorpej  * chain if necessary.
   1064      1.18   thorpej  */
   1065      1.18   thorpej void
   1066  1.69.2.2     skrll m_copyback(struct mbuf *m0, int off, int len, const void *cp)
   1067  1.69.2.2     skrll {
   1068  1.69.2.2     skrll #if defined(DEBUG)
   1069  1.69.2.2     skrll 	struct mbuf *origm = m0;
   1070  1.69.2.2     skrll 	int error;
   1071  1.69.2.2     skrll #endif /* defined(DEBUG) */
   1072  1.69.2.2     skrll 
   1073  1.69.2.2     skrll 	if (m0 == NULL)
   1074  1.69.2.2     skrll 		return;
   1075  1.69.2.2     skrll 
   1076  1.69.2.2     skrll #if defined(DEBUG)
   1077  1.69.2.2     skrll 	error =
   1078  1.69.2.2     skrll #endif /* defined(DEBUG) */
   1079  1.69.2.2     skrll 	m_copyback0(&m0, off, len, cp,
   1080  1.69.2.2     skrll 	    M_COPYBACK0_COPYBACK|M_COPYBACK0_EXTEND, M_DONTWAIT);
   1081  1.69.2.2     skrll 
   1082  1.69.2.2     skrll #if defined(DEBUG)
   1083  1.69.2.2     skrll 	if (error != 0 || (m0 != NULL && origm != m0))
   1084  1.69.2.2     skrll 		panic("m_copyback");
   1085  1.69.2.2     skrll #endif /* defined(DEBUG) */
   1086  1.69.2.2     skrll }
   1087  1.69.2.2     skrll 
   1088  1.69.2.2     skrll struct mbuf *
   1089  1.69.2.2     skrll m_copyback_cow(struct mbuf *m0, int off, int len, const void *cp, int how)
   1090  1.69.2.2     skrll {
   1091  1.69.2.2     skrll 	int error;
   1092  1.69.2.2     skrll 
   1093  1.69.2.2     skrll 	/* don't support chain expansion */
   1094  1.69.2.2     skrll 	KDASSERT(off + len <= m_length(m0));
   1095  1.69.2.2     skrll 
   1096  1.69.2.2     skrll 	error = m_copyback0(&m0, off, len, cp,
   1097  1.69.2.2     skrll 	    M_COPYBACK0_COPYBACK|M_COPYBACK0_COW, how);
   1098  1.69.2.2     skrll 	if (error) {
   1099  1.69.2.2     skrll 		/*
   1100  1.69.2.2     skrll 		 * no way to recover from partial success.
   1101  1.69.2.2     skrll 		 * just free the chain.
   1102  1.69.2.2     skrll 		 */
   1103  1.69.2.2     skrll 		m_freem(m0);
   1104  1.69.2.2     skrll 		return NULL;
   1105  1.69.2.2     skrll 	}
   1106  1.69.2.2     skrll 	return m0;
   1107  1.69.2.2     skrll }
   1108  1.69.2.2     skrll 
   1109  1.69.2.2     skrll /*
   1110  1.69.2.2     skrll  * m_makewritable: ensure the specified range writable.
   1111  1.69.2.2     skrll  */
   1112  1.69.2.2     skrll int
   1113  1.69.2.2     skrll m_makewritable(struct mbuf **mp, int off, int len, int how)
   1114  1.69.2.2     skrll {
   1115  1.69.2.2     skrll 	int error;
   1116  1.69.2.2     skrll #if defined(DEBUG)
   1117  1.69.2.2     skrll 	struct mbuf *n;
   1118  1.69.2.2     skrll 	int origlen, reslen;
   1119  1.69.2.2     skrll 
   1120  1.69.2.2     skrll 	origlen = m_length(*mp);
   1121  1.69.2.2     skrll #endif /* defined(DEBUG) */
   1122  1.69.2.2     skrll 
   1123  1.69.2.2     skrll #if 0 /* M_COPYALL is large enough */
   1124  1.69.2.2     skrll 	if (len == M_COPYALL)
   1125  1.69.2.2     skrll 		len = m_length(*mp) - off; /* XXX */
   1126  1.69.2.2     skrll #endif
   1127  1.69.2.2     skrll 
   1128  1.69.2.2     skrll 	error = m_copyback0(mp, off, len, NULL,
   1129  1.69.2.2     skrll 	    M_COPYBACK0_PRESERVE|M_COPYBACK0_COW, how);
   1130  1.69.2.2     skrll 
   1131  1.69.2.2     skrll #if defined(DEBUG)
   1132  1.69.2.2     skrll 	reslen = 0;
   1133  1.69.2.2     skrll 	for (n = *mp; n; n = n->m_next)
   1134  1.69.2.2     skrll 		reslen += n->m_len;
   1135  1.69.2.2     skrll 	if (origlen != reslen)
   1136  1.69.2.2     skrll 		panic("m_makewritable: length changed");
   1137  1.69.2.2     skrll 	if (((*mp)->m_flags & M_PKTHDR) != 0 && reslen != (*mp)->m_pkthdr.len)
   1138  1.69.2.2     skrll 		panic("m_makewritable: inconsist");
   1139  1.69.2.2     skrll #endif /* defined(DEBUG) */
   1140  1.69.2.2     skrll 
   1141  1.69.2.2     skrll 	return error;
   1142  1.69.2.2     skrll }
   1143  1.69.2.2     skrll 
   1144  1.69.2.2     skrll int
   1145  1.69.2.2     skrll m_copyback0(struct mbuf **mp0, int off, int len, const void *vp, int flags,
   1146  1.69.2.2     skrll     int how)
   1147      1.18   thorpej {
   1148      1.27      matt 	int mlen;
   1149  1.69.2.2     skrll 	struct mbuf *m, *n;
   1150  1.69.2.2     skrll 	struct mbuf **mp;
   1151      1.18   thorpej 	int totlen = 0;
   1152  1.69.2.2     skrll 	const char *cp = vp;
   1153      1.18   thorpej 
   1154  1.69.2.2     skrll 	KASSERT(mp0 != NULL);
   1155  1.69.2.2     skrll 	KASSERT(*mp0 != NULL);
   1156  1.69.2.2     skrll 	KASSERT((flags & M_COPYBACK0_PRESERVE) == 0 || cp == NULL);
   1157  1.69.2.2     skrll 	KASSERT((flags & M_COPYBACK0_COPYBACK) == 0 || cp != NULL);
   1158  1.69.2.2     skrll 
   1159  1.69.2.2     skrll 	mp = mp0;
   1160  1.69.2.2     skrll 	m = *mp;
   1161      1.18   thorpej 	while (off > (mlen = m->m_len)) {
   1162      1.18   thorpej 		off -= mlen;
   1163      1.18   thorpej 		totlen += mlen;
   1164      1.18   thorpej 		if (m->m_next == 0) {
   1165  1.69.2.2     skrll 			if ((flags & M_COPYBACK0_EXTEND) == 0)
   1166  1.69.2.2     skrll 				goto out;
   1167  1.69.2.2     skrll 			n = m_getclr(how, m->m_type);
   1168      1.18   thorpej 			if (n == 0)
   1169      1.18   thorpej 				goto out;
   1170      1.18   thorpej 			n->m_len = min(MLEN, len + off);
   1171      1.18   thorpej 			m->m_next = n;
   1172      1.18   thorpej 		}
   1173  1.69.2.2     skrll 		mp = &m->m_next;
   1174      1.18   thorpej 		m = m->m_next;
   1175      1.18   thorpej 	}
   1176      1.18   thorpej 	while (len > 0) {
   1177  1.69.2.2     skrll 		mlen = m->m_len - off;
   1178  1.69.2.2     skrll 		if (mlen != 0 && M_READONLY(m)) {
   1179  1.69.2.2     skrll 			char *datap;
   1180  1.69.2.2     skrll 			int eatlen;
   1181  1.69.2.2     skrll 
   1182  1.69.2.2     skrll 			/*
   1183  1.69.2.2     skrll 			 * this mbuf is read-only.
   1184  1.69.2.2     skrll 			 * allocate a new writable mbuf and try again.
   1185  1.69.2.2     skrll 			 */
   1186  1.69.2.2     skrll 
   1187  1.69.2.2     skrll #if defined(DIAGNOSTIC)
   1188  1.69.2.2     skrll 			if ((flags & M_COPYBACK0_COW) == 0)
   1189  1.69.2.2     skrll 				panic("m_copyback0: read-only");
   1190  1.69.2.2     skrll #endif /* defined(DIAGNOSTIC) */
   1191  1.69.2.2     skrll 
   1192  1.69.2.2     skrll 			/*
   1193  1.69.2.2     skrll 			 * if we're going to write into the middle of
   1194  1.69.2.2     skrll 			 * a mbuf, split it first.
   1195  1.69.2.2     skrll 			 */
   1196  1.69.2.2     skrll 			if (off > 0 && len < mlen) {
   1197  1.69.2.2     skrll 				n = m_split0(m, off, how, 0);
   1198  1.69.2.2     skrll 				if (n == NULL)
   1199  1.69.2.2     skrll 					goto enobufs;
   1200  1.69.2.2     skrll 				m->m_next = n;
   1201  1.69.2.2     skrll 				mp = &m->m_next;
   1202  1.69.2.2     skrll 				m = n;
   1203  1.69.2.2     skrll 				off = 0;
   1204  1.69.2.2     skrll 				continue;
   1205  1.69.2.2     skrll 			}
   1206  1.69.2.2     skrll 
   1207  1.69.2.2     skrll 			/*
   1208  1.69.2.2     skrll 			 * XXX TODO coalesce into the trailingspace of
   1209  1.69.2.2     skrll 			 * the previous mbuf when possible.
   1210  1.69.2.2     skrll 			 */
   1211  1.69.2.2     skrll 
   1212  1.69.2.2     skrll 			/*
   1213  1.69.2.2     skrll 			 * allocate a new mbuf.  copy packet header if needed.
   1214  1.69.2.2     skrll 			 */
   1215  1.69.2.2     skrll 			MGET(n, how, m->m_type);
   1216  1.69.2.2     skrll 			if (n == NULL)
   1217  1.69.2.2     skrll 				goto enobufs;
   1218  1.69.2.2     skrll 			MCLAIM(n, m->m_owner);
   1219  1.69.2.2     skrll 			if (off == 0 && (m->m_flags & M_PKTHDR) != 0) {
   1220  1.69.2.2     skrll 				/* XXX M_MOVE_PKTHDR */
   1221  1.69.2.2     skrll 				M_COPY_PKTHDR(n, m);
   1222  1.69.2.2     skrll 				n->m_len = MHLEN;
   1223  1.69.2.2     skrll 			} else {
   1224  1.69.2.2     skrll 				if (len >= MINCLSIZE)
   1225  1.69.2.2     skrll 					MCLGET(n, M_DONTWAIT);
   1226  1.69.2.2     skrll 				n->m_len =
   1227  1.69.2.2     skrll 				    (n->m_flags & M_EXT) ? MCLBYTES : MLEN;
   1228  1.69.2.2     skrll 			}
   1229  1.69.2.2     skrll 			if (n->m_len > len)
   1230  1.69.2.2     skrll 				n->m_len = len;
   1231  1.69.2.2     skrll 
   1232  1.69.2.2     skrll 			/*
   1233  1.69.2.2     skrll 			 * free the region which has been overwritten.
   1234  1.69.2.2     skrll 			 * copying data from old mbufs if requested.
   1235  1.69.2.2     skrll 			 */
   1236  1.69.2.2     skrll 			if (flags & M_COPYBACK0_PRESERVE)
   1237  1.69.2.2     skrll 				datap = mtod(n, char *);
   1238  1.69.2.2     skrll 			else
   1239  1.69.2.2     skrll 				datap = NULL;
   1240  1.69.2.2     skrll 			eatlen = n->m_len;
   1241  1.69.2.2     skrll 			KDASSERT(off == 0 || eatlen >= mlen);
   1242  1.69.2.2     skrll 			if (off > 0) {
   1243  1.69.2.2     skrll 				KDASSERT(len >= mlen);
   1244  1.69.2.2     skrll 				m->m_len = off;
   1245  1.69.2.2     skrll 				m->m_next = n;
   1246  1.69.2.2     skrll 				if (datap) {
   1247  1.69.2.2     skrll 					m_copydata(m, off, mlen, datap);
   1248  1.69.2.2     skrll 					datap += mlen;
   1249  1.69.2.2     skrll 				}
   1250  1.69.2.2     skrll 				eatlen -= mlen;
   1251  1.69.2.2     skrll 				mp = &m->m_next;
   1252  1.69.2.2     skrll 				m = m->m_next;
   1253  1.69.2.2     skrll 			}
   1254  1.69.2.2     skrll 			while (m != NULL && M_READONLY(m) &&
   1255  1.69.2.2     skrll 			    n->m_type == m->m_type && eatlen > 0) {
   1256  1.69.2.2     skrll 				mlen = min(eatlen, m->m_len);
   1257  1.69.2.2     skrll 				if (datap) {
   1258  1.69.2.2     skrll 					m_copydata(m, 0, mlen, datap);
   1259  1.69.2.2     skrll 					datap += mlen;
   1260  1.69.2.2     skrll 				}
   1261  1.69.2.2     skrll 				m->m_data += mlen;
   1262  1.69.2.2     skrll 				m->m_len -= mlen;
   1263  1.69.2.2     skrll 				eatlen -= mlen;
   1264  1.69.2.2     skrll 				if (m->m_len == 0)
   1265  1.69.2.2     skrll 					*mp = m = m_free(m);
   1266  1.69.2.2     skrll 			}
   1267  1.69.2.2     skrll 			if (eatlen > 0)
   1268  1.69.2.2     skrll 				n->m_len -= eatlen;
   1269  1.69.2.2     skrll 			n->m_next = m;
   1270  1.69.2.2     skrll 			*mp = m = n;
   1271  1.69.2.2     skrll 			continue;
   1272  1.69.2.2     skrll 		}
   1273  1.69.2.2     skrll 		mlen = min(mlen, len);
   1274  1.69.2.2     skrll 		if (flags & M_COPYBACK0_COPYBACK) {
   1275  1.69.2.2     skrll 			memcpy(mtod(m, caddr_t) + off, cp, (unsigned)mlen);
   1276  1.69.2.2     skrll 			cp += mlen;
   1277  1.69.2.2     skrll 		}
   1278      1.18   thorpej 		len -= mlen;
   1279      1.18   thorpej 		mlen += off;
   1280      1.18   thorpej 		off = 0;
   1281      1.18   thorpej 		totlen += mlen;
   1282      1.18   thorpej 		if (len == 0)
   1283      1.18   thorpej 			break;
   1284      1.18   thorpej 		if (m->m_next == 0) {
   1285  1.69.2.2     skrll 			if ((flags & M_COPYBACK0_EXTEND) == 0)
   1286  1.69.2.2     skrll 				goto out;
   1287  1.69.2.2     skrll 			n = m_get(how, m->m_type);
   1288      1.18   thorpej 			if (n == 0)
   1289      1.18   thorpej 				break;
   1290      1.18   thorpej 			n->m_len = min(MLEN, len);
   1291      1.18   thorpej 			m->m_next = n;
   1292      1.18   thorpej 		}
   1293  1.69.2.2     skrll 		mp = &m->m_next;
   1294      1.18   thorpej 		m = m->m_next;
   1295      1.18   thorpej 	}
   1296  1.69.2.2     skrll out:	if (((m = *mp0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen))
   1297      1.18   thorpej 		m->m_pkthdr.len = totlen;
   1298  1.69.2.2     skrll 
   1299  1.69.2.2     skrll 	return 0;
   1300  1.69.2.2     skrll 
   1301  1.69.2.2     skrll enobufs:
   1302  1.69.2.2     skrll 	return ENOBUFS;
   1303      1.66   thorpej }
   1304      1.66   thorpej 
   1305      1.66   thorpej /*
   1306      1.66   thorpej  * Apply function f to the data in an mbuf chain starting "off" bytes from the
   1307      1.66   thorpej  * beginning, continuing for "len" bytes.
   1308      1.66   thorpej  */
   1309      1.66   thorpej int
   1310      1.66   thorpej m_apply(struct mbuf *m, int off, int len,
   1311      1.66   thorpej     int (*f)(void *, caddr_t, unsigned int), void *arg)
   1312      1.66   thorpej {
   1313      1.66   thorpej 	unsigned int count;
   1314      1.66   thorpej 	int rval;
   1315      1.66   thorpej 
   1316      1.66   thorpej 	KASSERT(len >= 0);
   1317      1.66   thorpej 	KASSERT(off >= 0);
   1318      1.66   thorpej 
   1319      1.66   thorpej 	while (off > 0) {
   1320      1.66   thorpej 		KASSERT(m != NULL);
   1321      1.66   thorpej 		if (off < m->m_len)
   1322      1.66   thorpej 			break;
   1323      1.66   thorpej 		off -= m->m_len;
   1324      1.66   thorpej 		m = m->m_next;
   1325      1.66   thorpej 	}
   1326      1.66   thorpej 	while (len > 0) {
   1327      1.66   thorpej 		KASSERT(m != NULL);
   1328      1.66   thorpej 		count = min(m->m_len - off, len);
   1329      1.66   thorpej 
   1330      1.66   thorpej 		rval = (*f)(arg, mtod(m, caddr_t) + off, count);
   1331      1.66   thorpej 		if (rval)
   1332      1.66   thorpej 			return (rval);
   1333      1.66   thorpej 
   1334      1.66   thorpej 		len -= count;
   1335      1.66   thorpej 		off = 0;
   1336      1.66   thorpej 		m = m->m_next;
   1337      1.66   thorpej 	}
   1338      1.66   thorpej 
   1339      1.66   thorpej 	return (0);
   1340      1.66   thorpej }
   1341      1.66   thorpej 
   1342      1.66   thorpej /*
   1343      1.66   thorpej  * Return a pointer to mbuf/offset of location in mbuf chain.
   1344      1.66   thorpej  */
   1345      1.66   thorpej struct mbuf *
   1346      1.66   thorpej m_getptr(struct mbuf *m, int loc, int *off)
   1347      1.66   thorpej {
   1348      1.66   thorpej 
   1349      1.66   thorpej 	while (loc >= 0) {
   1350      1.66   thorpej 		/* Normal end of search */
   1351      1.66   thorpej 		if (m->m_len > loc) {
   1352      1.66   thorpej 	    		*off = loc;
   1353      1.66   thorpej 	    		return (m);
   1354      1.66   thorpej 		} else {
   1355      1.66   thorpej 	    		loc -= m->m_len;
   1356      1.66   thorpej 
   1357      1.66   thorpej 	    		if (m->m_next == NULL) {
   1358      1.66   thorpej 				if (loc == 0) {
   1359      1.66   thorpej  					/* Point at the end of valid data */
   1360      1.66   thorpej 		    			*off = m->m_len;
   1361      1.66   thorpej 		    			return (m);
   1362      1.66   thorpej 				} else
   1363      1.66   thorpej 		  			return (NULL);
   1364      1.66   thorpej 	    		} else
   1365      1.66   thorpej 	      			m = m->m_next;
   1366      1.66   thorpej 		}
   1367      1.66   thorpej     	}
   1368      1.66   thorpej 
   1369      1.66   thorpej 	return (NULL);
   1370       1.1       cgd }
   1371